update_memopol_representatives.py 1.57 KB
Newer Older
Arnaud Fabre's avatar
Arnaud Fabre committed
1
from __future__ import absolute_import
luxcem's avatar
luxcem committed
2
3

from django.db import transaction
Arnaud Fabre's avatar
Arnaud Fabre committed
4
from django.core.management.base import BaseCommand
luxcem's avatar
luxcem committed
5

Arnaud Fabre's avatar
Arnaud Fabre committed
6
import pyprind
luxcem's avatar
luxcem committed
7

Arnaud Fabre's avatar
Arnaud Fabre committed
8
9
import representatives.models
import legislature.models
luxcem's avatar
luxcem committed
10

Arnaud Fabre's avatar
Arnaud Fabre committed
11
12
13

class Command(BaseCommand):
    
luxcem's avatar
luxcem committed
14
15
    @transaction.atomic
    def handle(self, *args, **options):
Arnaud Fabre's avatar
Arnaud Fabre committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
        bar = pyprind.ProgBar(representatives.models.Representative.objects.all().count())
        for i, representative in enumerate(representatives.models.Representative.objects.all()):
            try:
                memopol_representative = legislature.models.MemopolRepresentative.objects.get(
                    representative_remote_id = representative.remote_id
                )
            except legislature.models.MemopolRepresentative.DoesNotExist:
                memopol_representative = legislature.models.MemopolRepresentative(
                    representative_remote_id = representative.remote_id
                )
            memopol_representative.representative_ptr_id = representative.pk
            
            memopol_representative.__dict__.update(representative.__dict__)
            memopol_representative.save()
            memopol_representative.update_country()
            
            bar.update()
        
        for i, group_item in enumerate(representatives.models.Group.objects.all()):
            memopol_group, _ = legislature.models.MemopolGroup.objects.get_or_create(
                group = group_item
            )
            memopol_group.__dict__.update(group_item.__dict__)
            memopol_group.update_active()
            memopol_group.save()