From 5bc994fd857336185bc89fc2c548ed362001dec5 Mon Sep 17 00:00:00 2001
From: jpic <jamespic@gmail.com>
Date: Sat, 19 Dec 2015 19:39:49 +0100
Subject: [PATCH] Added Constituency.country

---
 .../parltrack/import_representatives.py       | 24 +++++++++++++++----
 .../tests/representatives_expected.json       |  2 ++
 .../migrations/0008_constituency_country.py   | 19 +++++++++++++++
 representatives/models.py                     |  1 +
 4 files changed, 42 insertions(+), 4 deletions(-)
 create mode 100644 representatives/migrations/0008_constituency_country.py

diff --git a/representatives/contrib/parltrack/import_representatives.py b/representatives/contrib/parltrack/import_representatives.py
index 3e9b843..5b4def1 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 cc7fd72..9ec5f92 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 0000000..d9d0e4b
--- /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 ac02d8c..ad02f14 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']
 
-- 
GitLab