update_memopol_representatives.py 1.83 KB
Newer Older
luxcem's avatar
luxcem committed
1
2
3
# import datetime
from django.core.management.base import BaseCommand
from representatives import models
4
from legislature.models import MRepresentative, MMandate, MGroup
luxcem's avatar
luxcem committed
5
6
7
8
9
10
11
12
13
14
15
16

from django.db import transaction


class Command(BaseCommand):

    @transaction.atomic
    def handle(self, *args, **options):
        # Representatives
        print('Representatives')
        n = models.Representative.objects.all().count()
        for i, representative in enumerate(models.Representative.objects.all()):
17
18
19
            mrepresentative = MRepresentative(representative_ptr=representative)
            mrepresentative.__dict__.update(representative.__dict__)
            mrepresentative.save()
luxcem's avatar
luxcem committed
20
21
22
            print("%s/%s\r" % (i, n)),

        print('Mandates')
23
24
        for i, mrepresentative in enumerate(MRepresentative.objects.all()):
            representative = mrepresentative.representative_ptr
luxcem's avatar
luxcem committed
25
            for mandate in representative.mandate_set.all():
26
27
28
29
30
31
                mmandate = MMandate(mandate_ptr=mandate)
                mmandate.__dict__.update(mandate.__dict__)
                mmandate.mrepresentative = mrepresentative

                # Group creation
                try:
Arnaud Fabre's avatar
Arnaud Fabre committed
32
                    mgroup = MGroup.objects.get(group_ptr=mandate.group)
33
34
35
                except MGroup.DoesNotExist:
                    mgroup = MGroup(group_ptr=mandate.group)
                    mgroup.__dict__.update(mandate.group.__dict__)
36
                    mgroup.save()                
37
38
39
40
41
42

                mmandate.mgroup = mgroup
                mmandate.save()
                mmandate.update_active()

            mrepresentative.update_country()
43
            # mrepresentative.update_active()
44
            mrepresentative.save()
luxcem's avatar
luxcem committed
45
            print("%s/%s\r" % (i, n)),
46
47
48
49

        print('Groups')
        for i, mgroup in enumerate(MGroup.objects.all()):
            mgroup.update_active()