Commit a231f56e authored by luxcem's avatar luxcem
Browse files

updates legislatures models : no colisions with representatives

parent 305aaeee
...@@ -2,3 +2,16 @@ ...@@ -2,3 +2,16 @@
%li %li
%a{href: "{% url 'legislature:representatives_index' %}"} %a{href: "{% url 'legislature:representatives_index' %}"}
Representatives Representatives
%li
%a{href: "{% url 'legislature:groups_by_kind' 'country' %}"}
Countries
%li
%a{href: "{% url 'legislature:groups_by_kind' 'group' %}"}
Parties
%li
%a{href: "{% url 'legislature:groups_by_kind' 'delegation' %}"}
Delegations
%li
%a{href: "{% url 'legislature:groups_by_kind' 'committee' %}"}
Committees
from django.contrib import admin from django.contrib import admin
from representatives.models import Email, WebSite, Address, Phone, Country, Constituency
from legislature.models import MMandate, MRepresentative, MGroup
class EmailInline(admin.TabularInline):
model = Email
extra = 0
class WebsiteInline(admin.TabularInline):
model = WebSite
extra = 0
class AdressInline(admin.StackedInline):
model = Address
extra = 0
class PhoneInline(admin.TabularInline):
model = Phone
extra = 0
class RepresentativeAdmin(admin.ModelAdmin):
list_display = ('full_name', 'gender', 'birth_place')
search_fields = ('first_name', 'last_name', 'birth_place')
list_filter = ('gender', )
inlines = [
PhoneInline,
EmailInline,
WebsiteInline,
AdressInline,
]
class MandateAdmin(admin.ModelAdmin):
list_display = ('representative', 'group', 'role', 'constituency', 'begin_date', 'end_date', 'active')
search_fields = ('representative', 'group', 'constituency')
# list_filter = ('role',)
admin.site.register(MRepresentative, RepresentativeAdmin)
# admin.site.register(Country)
admin.site.register(MMandate, MandateAdmin)
admin.site.register(MGroup)
# admin.site.register(Constituency)
# import datetime # import datetime
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from representatives import models from representatives import models
from legislature.models import Representative, Mandate from legislature.models import MRepresentative, MMandate, MGroup
from django.db import transaction from django.db import transaction
...@@ -14,21 +14,37 @@ class Command(BaseCommand): ...@@ -14,21 +14,37 @@ class Command(BaseCommand):
print('Representatives') print('Representatives')
n = models.Representative.objects.all().count() n = models.Representative.objects.all().count()
for i, representative in enumerate(models.Representative.objects.all()): for i, representative in enumerate(models.Representative.objects.all()):
legislature_representative = Representative(representative_ptr=representative) mrepresentative = MRepresentative(representative_ptr=representative)
legislature_representative.__dict__.update(representative.__dict__) mrepresentative.__dict__.update(representative.__dict__)
legislature_representative.update_country() mrepresentative.save()
legislature_representative.save()
print("%s/%s\r" % (i, n)), print("%s/%s\r" % (i, n)),
print('Mandates') print('Mandates')
for i, representative in enumerate(Representative.objects.all()): for i, mrepresentative in enumerate(MRepresentative.objects.all()):
legislature_mandates = [] representative = mrepresentative.representative_ptr
for mandate in representative.mandate_set.all(): for mandate in representative.mandate_set.all():
legislature_mandate = Mandate(mandate_ptr=mandate) mmandate = MMandate(mandate_ptr=mandate)
legislature_mandate.__dict__.update(mandate.__dict__) mmandate.__dict__.update(mandate.__dict__)
legislature_mandate.update_active() mmandate.mrepresentative = mrepresentative
legislature_mandate.save()
legislature_mandates.append(legislature_mandate) # Group creation
representative.update_active() try:
representative.save() mgroup = MGroup(group_ptr=mandate.group)
except MGroup.DoesNotExist:
mgroup = MGroup(group_ptr=mandate.group)
mgroup.__dict__.update(mandate.group.__dict__)
mgroup.save()
mmandate.mgroup = mgroup
mmandate.save()
mmandate.update_active()
mrepresentative.update_country()
mrepresentative.update_active()
mrepresentative.save()
print("%s/%s\r" % (i, n)), print("%s/%s\r" % (i, n)),
print('Groups')
for i, mgroup in enumerate(MGroup.objects.all()):
mgroup.update_active()
...@@ -7,44 +7,46 @@ from django.db import models, migrations ...@@ -7,44 +7,46 @@ from django.db import models, migrations
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [ dependencies = [
('representatives', '0004_representative_country'), ('representatives', '0007_auto_20150323_1017'),
] ]
operations = [ operations = [
migrations.CreateModel( migrations.CreateModel(
name='Constituency', name='MGroup',
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=[ fields=[
('group_ptr', models.OneToOneField(parent_link=True, auto_created=True, primary_key=True, serialize=False, to='representatives.Group')), ('group_ptr', models.OneToOneField(parent_link=True, auto_created=True, primary_key=True, serialize=False, to='representatives.Group')),
('active', models.BooleanField(default=False)),
], ],
options={ options={
}, },
bases=('representatives.group',), bases=('representatives.group',),
), ),
migrations.CreateModel( migrations.CreateModel(
name='Mandate', name='MMandate',
fields=[ fields=[
('mandate_ptr', models.OneToOneField(parent_link=True, auto_created=True, primary_key=True, serialize=False, to='representatives.Mandate')), ('mandate_ptr', models.OneToOneField(parent_link=True, auto_created=True, primary_key=True, serialize=False, to='representatives.Mandate')),
('active', models.BooleanField(default=False)),
('mgroup', models.ForeignKey(to='legislature.MGroup')),
], ],
options={ options={
}, },
bases=('representatives.mandate',), bases=('representatives.mandate',),
), ),
migrations.CreateModel( migrations.CreateModel(
name='Representative', name='MRepresentative',
fields=[ fields=[
('representative_ptr', models.OneToOneField(parent_link=True, auto_created=True, primary_key=True, serialize=False, to='representatives.Representative')), ('representative_ptr', models.OneToOneField(parent_link=True, auto_created=True, primary_key=True, serialize=False, to='representatives.Representative')),
('active', models.BooleanField(default=False)),
('country', models.ForeignKey(to='representatives.Country', null=True)),
], ],
options={ options={
}, },
bases=('representatives.representative',), bases=('representatives.representative',),
), ),
migrations.AddField(
model_name='mmandate',
name='mrepresentative',
field=models.ForeignKey(to='legislature.MRepresentative'),
preserve_default=True,
),
] ]
# -*- 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,
),
]
...@@ -3,18 +3,18 @@ import representatives ...@@ -3,18 +3,18 @@ import representatives
import datetime import datetime
class Representative(representatives.models.Representative): class MRepresentative(representatives.models.Representative):
active = models.BooleanField(default=False) active = models.BooleanField(default=False)
country = models.ForeignKey(representatives.models.Country, null=True) country = models.ForeignKey(representatives.models.Country, null=True)
def active_mandates(self): def active_mandates(self):
return self.mandate_set.filter(active=True) return self.mmandate_set.filter(active=True)
def former_mandates(self): def former_mandates(self):
return self.mandate_set.filter(active=False) return self.mmandate_set.filter(active=False)
def current_group(self): def current_group_mandate(self):
return self.mandate_set.get( return self.mmandate_set.get(
active=True, active=True,
group__kind='group' group__kind='group'
) )
...@@ -22,10 +22,10 @@ class Representative(representatives.models.Representative): ...@@ -22,10 +22,10 @@ class Representative(representatives.models.Representative):
def update_active(self): def update_active(self):
# If a representative has at least one active manadate # If a representative has at least one active manadate
self.active = False self.active = False
for mandate in self.mandate_set.all(): for mandate in self.mmandate_set.all():
if mandate.active: if mandate.active:
self.active = True self.active = True
continue break
self.save() self.save()
...@@ -33,7 +33,7 @@ class Representative(representatives.models.Representative): ...@@ -33,7 +33,7 @@ class Representative(representatives.models.Representative):
# Create a country if it does not exist # Create a country if it does not exist
# The representative's country is the one associated # The representative's country is the one associated
# with the last 'country' mandate # with the last 'country' mandate
country_mandate = self.mandate_set.filter( country_mandate = self.mmandate_set.filter(
group__kind='country' group__kind='country'
).order_by('-begin_date')[0:1].get() ).order_by('-begin_date')[0:1].get()
...@@ -45,7 +45,22 @@ class Representative(representatives.models.Representative): ...@@ -45,7 +45,22 @@ class Representative(representatives.models.Representative):
self.save() self.save()
class Mandate(representatives.models.Mandate): class MGroup(representatives.models.Group):
active = models.BooleanField(default=False)
def update_active(self):
self.active = False
for mandate in self.mmandate_set.all():
if mandate.active:
self.active = True
break
self.save()
class MMandate(representatives.models.Mandate):
active = models.BooleanField(default=False)
mgroup = models.ForeignKey(MGroup)
mrepresentative = models.ForeignKey(MRepresentative)
def update_active(self): def update_active(self):
date = datetime.datetime.now().date() date = datetime.datetime.now().date()
......
- extends 'base.html'
- block content
- for group in groups
{{ group.name }}
%br
- extends 'base.html'
- load by_mandate_url
- block content
%ul
- for group in groups
%li
%a{'href': '{{ group | by_group_url }}'}
- if group.abbreviation
{{ group.abbreviation }} .
{{ group.name }}
...@@ -9,9 +9,9 @@ ...@@ -9,9 +9,9 @@
={representative.full_name} ={representative.full_name}
%td %td
%a{'href': "{% url 'legislature:representatives_by_mandate' mandate_kind='country' search=representative.country.code %}"} %a{'href': "{% url 'legislature:representatives_by_group' group_kind='country' search=representative.country.code %}"}
={representative.country.name} ={representative.country.name}
%td %td
%a{'href': "{{ representative.current_group|by_mandate_url }}"} %a{'href': "{{ representative.current_group_mandate|by_mandate_url }}"}
={representative.current_group.group.abbreviation} ={representative.current_group_mandate.mgroup.abbreviation}
{% extends 'base.html' %} {% extends 'base.html' %}
{% load by_mandate_url %} {% load by_group_url %}
{% block content %} {% block content %}
...@@ -10,8 +10,8 @@ ...@@ -10,8 +10,8 @@
<h1>{{ representative.full_name }}</h1> <h1>{{ representative.full_name }}</h1>
<p> <p>
<strong> <strong>
<a href="{{ representative.current_group|by_mandate_url }}"> <a href="{{ representative.current_group_mandate|by_mandate_url }}">
{{ representative.current_group.role }} of {{ representative.current_group.group.name }} {{ representative.current_group_mandate.role }} of {{ representative.current_group_mandate.mgroup.name }}
</a> </a>
</strong> </strong>
</p> </p>
......
from django import template
from django.core.urlresolvers import reverse
register = template.Library()
@register.filter
def by_group_url(group):
kwargs = {'group_kind': group.kind}
if group.abbreviation:
kwargs['search'] = group.abbreviation
else:
kwargs['search'] = group.name
return reverse(
'legislature:representatives_by_group',
kwargs=kwargs
)
@register.filter
def by_mandate_url(mandate):
return by_group_url(mandate.group)
...@@ -5,15 +5,20 @@ register = template.Library() ...@@ -5,15 +5,20 @@ register = template.Library()
@register.filter @register.filter
def by_mandate_url(mandate): def by_group_url(group):
kwargs = {'mandate_kind': mandate.group.kind} kwargs = {'group_kind': group.kind}
if mandate.group.abbreviation: if group.abbreviation:
kwargs['search'] = mandate.group.abbreviation kwargs['search'] = group.abbreviation
else: else:
kwargs['search'] = mandate.group.name kwargs['search'] = group.name
return reverse( return reverse(
'legislature:representatives_by_mandate', 'legislature:representatives_by_group',
kwargs=kwargs kwargs=kwargs
) )
@register.filter
def by_mandate_url(mandate):
return by_group_url(mandate.group)
...@@ -15,9 +15,9 @@ urlpatterns = patterns( ...@@ -15,9 +15,9 @@ urlpatterns = patterns(
name='representative_view' name='representative_view'
), ),
url( url(
r'^representatives/(?P<mandate_kind>\w+)/(?P<search>.+)$', r'^representatives/(?P<group_kind>\w+)/(?P<search>.+)$',
views.representatives_by_mandate, views.representatives_by_group,
name='representatives_by_mandate' name='representatives_by_group'
), ),
url( url(
r'^representatives/(?P<name>.+)$', r'^representatives/(?P<name>.+)$',
...@@ -25,8 +25,8 @@ urlpatterns = patterns( ...@@ -25,8 +25,8 @@ urlpatterns = patterns(
name='representative_view_by_name' name='representative_view_by_name'
), ),
url( url(
r'^group/(?P<kind>\w+)$', r'^groups/(?P<kind>\w+)$',
views.group_by_kind, views.groups_by_kind,
name='group_by_kind' name='groups_by_kind'
) )
) )
...@@ -2,14 +2,13 @@ from django.shortcuts import render, get_object_or_404 ...@@ -2,14 +2,13 @@ from django.shortcuts import render, get_object_or_404
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.db.models import Q from django.db.models import Q
from legislature.models import Representative from legislature.models import MRepresentative, MGroup
from representatives.models import Group
def representatives_index(request): def representatives_index(request):
representative_list = _filter_by_search( representative_list = _filter_by_search(
request, request,
Representative.objects.all() MRepresentative.objects.all()
) )
return _render_list(request, representative_list) return _render_list(request, representative_list)
...@@ -17,7 +16,7 @@ def representatives_index(request): ...@@ -17,7 +16,7 @@ def representatives_index(request):
def representative_by_name(request, name): def representative_by_name(request, name):
representative = get_object_or_404( representative = get_object_or_404(
Representative, full_name=name) MRepresentative, full_name=name)
return render( return render(
request, request,
'legislature/representative_view.html', 'legislature/representative_view.html',
...@@ -26,7 +25,7 @@ def representative_by_name(request, name): ...@@ -26,7 +25,7 @@ def representative_by_name(request, name):
def representative_view(request, num): def representative_view(request, num):
representative = get_object_or_404(Representative, pk=num) representative = get_object_or_404(MRepresentative, pk=num)
return render( return render(
request, request,
...@@ -35,36 +34,36 @@ def representative_view(request, num): ...@@ -35,36 +34,36 @@ def representative_view(request, num):
) )
def representatives_by_mandate(request, mandate_kind, mandate_abbr=None, def representatives_by_group(request, group_kind, group_abbr=None,
mandate_name=None, search=None): group_name=None, search=None):
if mandate_abbr: if group_abbr:
representative_list = Representative.objects.filter( representative_list = MRepresentative.objects.filter(
mandate__group__abbreviation=mandate_abbr, mmandate__mgroup__abbreviation=group_abbr,
mandate__group__kind=mandate_kind, mmandate__mgroup__kind=group_kind,
mandate__active=True mmandate__active=True
) )
elif mandate_name: elif group_name:
representative_list = Representative.objects.filter( representative_list = MRepresentative.objects.filter(
Q(mandate__group__name__icontains=mandate_name), Q(mmandate__mgroup__name__icontains=group_name),
mandate__group__kind=mandate_kind, mmandate__mgroup__kind=group_kind,
mandate__active=True mmandate__active=True
) )
elif search: elif search:
try: try:
Group.objects.get(abbreviation=search, kind=mandate_kind) MGroup.objects.get(abbreviation=search, kind=group_kind)
representative_list = Representative.objects.filter( representative_list = MRepresentative.objects.filter(
mandate__group__abbreviation=search, mmandate__mgroup__abbreviation=search,
mandate__group__kind=mandate_kind, mmandate__mgroup__kind=group_kind,
mandate__active=True mmandate__active=True
) )
except Group.DoesNotExist: except MGroup.DoesNotExist:
representative_list = Representative.objects.filter(