Commit 5bc994fd authored by Jamesie Pic's avatar Jamesie Pic

Added Constituency.country

parent e454c73d
......@@ -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,
......
......@@ -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",
......
# -*- 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),
),
]
......@@ -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']
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment