diff --git a/representatives/contrib/parltrack/import_representatives.py b/representatives/contrib/parltrack/import_representatives.py index 3e9b843b901ebab1025c5d6dc2d98a4466126937..5b4def132e3c3a1ae4458f79ee4f4975b0bba45c 100644 --- a/representatives/contrib/parltrack/import_representatives.py +++ b/representatives/contrib/parltrack/import_representatives.py @@ -62,7 +62,9 @@ class ParltrackImporter(GenericImporter): return _parse_date(date) def __init__(self): - self.cache = {} + self.cache = { + 'countries': {c.name: c.pk for c in Country.objects.all()}, + } @transaction.atomic def manage_mep(self, mep_json): @@ -275,9 +277,23 @@ class ParltrackImporter(GenericImporter): local_party = mandate_data['party'] if mandate_data[ 'party'] and mandate_data['party'] != '-' else 'unknown' - constituency, _ = self.touch_model(model=Constituency, - name=local_party - ) + + country_id = (self.cache['countries'].get(mandate_data['country']) + if 'country' in mandate_data else None) + + save_constituency = False + try: + constituency = Constituency.objects.get(name=local_party) + except Constituency.DoesNotExist: + constituency = Constituency(name=local_party) + save_constituency = True + + if constituency.country_id != country_id: + constituency.country_id = country_id + save_constituency = True + + if save_constituency: + constituency.save() self.mep_cache['constituencies'].append( get_or_create_mandate(mandate_data, representative, group, diff --git a/representatives/contrib/parltrack/tests/representatives_expected.json b/representatives/contrib/parltrack/tests/representatives_expected.json index cc7fd72b7a3ddf7216d715cd3646060207d9308f..9ec5f9254c3c16b34d786e336123e6a2708b8fac 100644 --- a/representatives/contrib/parltrack/tests/representatives_expected.json +++ b/representatives/contrib/parltrack/tests/representatives_expected.json @@ -2256,6 +2256,7 @@ "updated": "2015-12-13T02:07:24.018", "fingerprint": "74e9c77e0664716d098cb1194927b86f2aa55f7e", "name": "\u00d6sterreichische Volkspartei", + "country": 1043, "created": "2015-12-13T02:07:24.018" }, "model": "representatives.constituency", @@ -2266,6 +2267,7 @@ "updated": "2015-12-13T02:07:24.390", "fingerprint": "a8fa2ff595aedee63954c3b5ad6e1dcd5dfac910", "name": "Arbetarepartiet- Socialdemokraterna", + "country": 1202, "created": "2015-12-13T02:07:24.390" }, "model": "representatives.constituency", diff --git a/representatives/migrations/0008_constituency_country.py b/representatives/migrations/0008_constituency_country.py new file mode 100644 index 0000000000000000000000000000000000000000..d9d0e4bcbcacf2f3a9cfd5a14df86a267a7d98a0 --- /dev/null +++ b/representatives/migrations/0008_constituency_country.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('representatives', '0007_auto_20151213_0156'), + ] + + operations = [ + migrations.AddField( + model_name='constituency', + name='country', + field=models.ForeignKey(blank=True, to='representatives.Country', null=True), + ), + ] diff --git a/representatives/models.py b/representatives/models.py index ac02d8c6d4631ea1e2f0ebc7d6e4cd6cb97e3128..ad02f1475073eb1fc332659494aaa1164ccd215d 100644 --- a/representatives/models.py +++ b/representatives/models.py @@ -195,6 +195,7 @@ class Constituency(HashableModel, TimeStampedModel): An authority for which a representative has a mandate """ name = models.CharField(max_length=255) + country = models.ForeignKey('Country', null=True, blank=True) hashable_fields = ['name']