Skip to content

Commit

Permalink
add the relationship between the MPs and constituencies closes #13
Browse files Browse the repository at this point in the history
  • Loading branch information
timini committed Apr 6, 2016
1 parent b9d4c75 commit 85bf2b4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
3 changes: 3 additions & 0 deletions api/api/MPs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ class MP(DateMixin):
given_name = models.CharField(max_length=1024, blank=True)
party = models.CharField(max_length=512, blank=True)
twitter = models.URLField(blank=True)

def __str__(self):
return self.full_name
8 changes: 8 additions & 0 deletions api/api/MPs/serializers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
from rest_framework_json_api import serializers, relations

from MPs.models import MP
from constituencies.models import Constituency

class MPsSerializer(serializers.ModelSerializer):
constituency = relations.ResourceRelatedField(
queryset=Constituency.objects.all(),
pk_field=serializers.UUIDField(format='hex_verbose'),
many=True,
read_only=False
)
class Meta:
model = MP
fields = (
'url',
'constituency',
'additional_name',
'home_page',
'family_name',
Expand Down
7 changes: 7 additions & 0 deletions api/api/constituencies/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@ class Constituency(DateMixin):
ended_date = models.DateField(blank=True, null=True)
gss_code = models.CharField(max_length=512)
os_name = models.CharField(max_length=512)

def __str__(self):
return '{} - {} - {}'.format(
self.name,
self.started_date,
self.ended_date
)
28 changes: 28 additions & 0 deletions api/api/main/management/commands/init_data.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from django.core.management.base import BaseCommand, CommandError

from pprint import pprint

from MPs.models import MP
from constituencies.models import Constituency
from main.models import Scraper
Expand All @@ -9,15 +11,41 @@ class Command(BaseCommand):
help = 'Retrevies MP data from the parlimentary website and loads into the database'

def handle(self, *args, **options):
print('fetching member data from data.gov.uk')
members = Scraper(dataset='members')
print('fetching constituency data from data.gov.uk')
constituencies = Scraper(dataset='constituencies', label_key='name')

print('commiting members to db')
MP.objects.all().delete()
MP.objects.bulk_create([
MP(**item) for item in members.cleaned_items
])

print('commiting constituencies to db')
Constituency.objects.all().delete()
Constituency.objects.bulk_create([
Constituency(**item) for item in constituencies.cleaned_items
])

print('updating members with links to constituencies')

def get_mp_id_from_item(item):
_id = int(item['_about'].split('/')[-1])
return MP.objects.get(source_id=_id).id

def get_constituency_id_from_item(item):
_id = int(item['constituency']['_about'].split('/')[-1])
return Constituency.objects.get(source_id=_id).id

Through = MP.constituency.through
Through.objects.all().delete()
Through.objects.bulk_create([
Through(
mp_id = get_mp_id_from_item(item),
constituency_id = get_constituency_id_from_item(item)
)
for item in members.items
if 'constituency' in item.keys()
])
print('complete')

0 comments on commit 85bf2b4

Please sign in to comment.