From 305aaeeebde43a183ac7ba32f56b988fe4fdda00 Mon Sep 17 00:00:00 2001 From: luxcem Date: Fri, 20 Mar 2015 15:55:53 +0100 Subject: [PATCH] adds methods to legislatures models --- .../commands/legislature_updates_models.py | 34 ++++++++++++ legislature/migrations/0001_initial.py | 50 ++++++++++++++++++ .../migrations/0002_auto_20150319_1620.py | 27 ++++++++++ legislature/migrations/__init__.py | 0 legislature/models.py | 52 +++++++++++++++++++ .../legislature/representative_block.haml | 17 ++++-- .../legislature/representatives_list.haml | 1 + legislature/views.py | 24 ++++++--- 8 files changed, 194 insertions(+), 11 deletions(-) create mode 100644 legislature/management/commands/legislature_updates_models.py create mode 100644 legislature/migrations/0001_initial.py create mode 100644 legislature/migrations/0002_auto_20150319_1620.py create mode 100644 legislature/migrations/__init__.py diff --git a/legislature/management/commands/legislature_updates_models.py b/legislature/management/commands/legislature_updates_models.py new file mode 100644 index 0000000..7188559 --- /dev/null +++ b/legislature/management/commands/legislature_updates_models.py @@ -0,0 +1,34 @@ +# import datetime +from django.core.management.base import BaseCommand +from representatives import models +from legislature.models import Representative, Mandate + +from django.db import transaction + + +class Command(BaseCommand): + + @transaction.atomic + def handle(self, *args, **options): + # Representatives + print('Representatives') + n = models.Representative.objects.all().count() + for i, representative in enumerate(models.Representative.objects.all()): + legislature_representative = Representative(representative_ptr=representative) + legislature_representative.__dict__.update(representative.__dict__) + legislature_representative.update_country() + legislature_representative.save() + print("%s/%s\r" % (i, n)), + + print('Mandates') + for i, representative in enumerate(Representative.objects.all()): + legislature_mandates = [] + for mandate in representative.mandate_set.all(): + legislature_mandate = Mandate(mandate_ptr=mandate) + legislature_mandate.__dict__.update(mandate.__dict__) + legislature_mandate.update_active() + legislature_mandate.save() + legislature_mandates.append(legislature_mandate) + representative.update_active() + representative.save() + print("%s/%s\r" % (i, n)), diff --git a/legislature/migrations/0001_initial.py b/legislature/migrations/0001_initial.py new file mode 100644 index 0000000..2bfe68c --- /dev/null +++ b/legislature/migrations/0001_initial.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('representatives', '0004_representative_country'), + ] + + operations = [ + migrations.CreateModel( + name='Constituency', + fields=[ + ('constituency_ptr', models.OneToOneField(parent_link=True, auto_created=True, primary_key=True, serialize=False, to='representatives.Constituency')), + ], + options={ + }, + bases=('representatives.constituency',), + ), + migrations.CreateModel( + name='Group', + fields=[ + ('group_ptr', models.OneToOneField(parent_link=True, auto_created=True, primary_key=True, serialize=False, to='representatives.Group')), + ], + options={ + }, + bases=('representatives.group',), + ), + migrations.CreateModel( + name='Mandate', + fields=[ + ('mandate_ptr', models.OneToOneField(parent_link=True, auto_created=True, primary_key=True, serialize=False, to='representatives.Mandate')), + ], + options={ + }, + bases=('representatives.mandate',), + ), + migrations.CreateModel( + name='Representative', + fields=[ + ('representative_ptr', models.OneToOneField(parent_link=True, auto_created=True, primary_key=True, serialize=False, to='representatives.Representative')), + ], + options={ + }, + bases=('representatives.representative',), + ), + ] diff --git a/legislature/migrations/0002_auto_20150319_1620.py b/legislature/migrations/0002_auto_20150319_1620.py new file mode 100644 index 0000000..b07c163 --- /dev/null +++ b/legislature/migrations/0002_auto_20150319_1620.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('representatives', '0005_auto_20150319_1620'), + ('legislature', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='representative', + name='active', + field=models.BooleanField(default=False), + preserve_default=True, + ), + migrations.AddField( + model_name='representative', + name='country', + field=models.ForeignKey(to='representatives.Country', null=True), + preserve_default=True, + ), + ] diff --git a/legislature/migrations/__init__.py b/legislature/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/legislature/models.py b/legislature/models.py index 137941f..7c7681a 100644 --- a/legislature/models.py +++ b/legislature/models.py @@ -1 +1,53 @@ from django.db import models +import representatives +import datetime + + +class Representative(representatives.models.Representative): + active = models.BooleanField(default=False) + country = models.ForeignKey(representatives.models.Country, null=True) + + def active_mandates(self): + return self.mandate_set.filter(active=True) + + def former_mandates(self): + return self.mandate_set.filter(active=False) + + def current_group(self): + return self.mandate_set.get( + active=True, + group__kind='group' + ) + + def update_active(self): + # If a representative has at least one active manadate + self.active = False + for mandate in self.mandate_set.all(): + if mandate.active: + self.active = True + continue + + self.save() + + def update_country(self): + # Create a country if it does not exist + # The representative's country is the one associated + # with the last 'country' mandate + country_mandate = self.mandate_set.filter( + group__kind='country' + ).order_by('-begin_date')[0:1].get() + + country, created = representatives.models.Country.objects.get_or_create( + name=country_mandate.group.name, + code=country_mandate.group.abbreviation + ) + self.country = country + self.save() + + +class Mandate(representatives.models.Mandate): + + def update_active(self): + date = datetime.datetime.now().date() + self.active = self.end_date > date + self.save() diff --git a/legislature/templates/legislature/representative_block.haml b/legislature/templates/legislature/representative_block.haml index 99d7a2c..990f8a8 100644 --- a/legislature/templates/legislature/representative_block.haml +++ b/legislature/templates/legislature/representative_block.haml @@ -1,8 +1,17 @@ -%td< +- load by_mandate_url + +%td %a{'href': "{% url 'legislature:representative_view_by_name' representative.full_name %}"} - %img{'src':'={representative.photo}'}/ + %img{'src': '={representative.photo}', 'width': '80'}/ -%td< +%td %a{'href': "{% url 'legislature:representative_view_by_name' representative.full_name %}"} - ={representative.full_name} [={representative.country.code}] + ={representative.full_name} + +%td + %a{'href': "{% url 'legislature:representatives_by_mandate' mandate_kind='country' search=representative.country.code %}"} + ={representative.country.name} +%td + %a{'href': "{{ representative.current_group|by_mandate_url }}"} + ={representative.current_group.group.abbreviation} diff --git a/legislature/templates/legislature/representatives_list.haml b/legislature/templates/legislature/representatives_list.haml index 6008a37..d793187 100644 --- a/legislature/templates/legislature/representatives_list.haml +++ b/legislature/templates/legislature/representatives_list.haml @@ -1,6 +1,7 @@ - extends "base.html" - block content + - include 'legislature/search.html' %p diff --git a/legislature/views.py b/legislature/views.py index c00c837..284a272 100644 --- a/legislature/views.py +++ b/legislature/views.py @@ -2,7 +2,8 @@ from django.shortcuts import render, get_object_or_404 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from django.db.models import Q -from representatives.models import Representative, Group +from legislature.models import Representative +from representatives.models import Group def representatives_index(request): @@ -51,12 +52,20 @@ def representatives_by_mandate(request, mandate_kind, mandate_abbr=None, ) elif search: - representative_list = Representative.objects.filter( - Q(mandate__group__abbreviation__icontains=search) | - Q(mandate__group__name__icontains=search), - mandate__group__kind=mandate_kind, - mandate__active=True - ) + try: + Group.objects.get(abbreviation=search, kind=mandate_kind) + representative_list = Representative.objects.filter( + mandate__group__abbreviation=search, + mandate__group__kind=mandate_kind, + mandate__active=True + ) + except Group.DoesNotExist: + representative_list = Representative.objects.filter( + Q(mandate__group__abbreviation__icontains=search) | + Q(mandate__group__name__icontains=search), + mandate__group__kind=mandate_kind, + mandate__active=True + ) # Select distinct representatives and filter by search representative_list = list(set( @@ -101,6 +110,7 @@ def _render_list(request, representative_list, num_by_page=50): context['representatives'] = representatives context['representative_num'] = paginator.count + return render( request, 'legislature/representatives_list.html', -- GitLab