Commit a231f56e authored by luxcem's avatar luxcem

updates legislatures models : no colisions with representatives

parent 305aaeee
......@@ -2,3 +2,16 @@
%li
%a{href: "{% url 'legislature:representatives_index' %}"}
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 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
from django.core.management.base import BaseCommand
from representatives import models
from legislature.models import Representative, Mandate
from legislature.models import MRepresentative, MMandate, MGroup
from django.db import transaction
......@@ -14,21 +14,37 @@ class Command(BaseCommand):
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()
mrepresentative = MRepresentative(representative_ptr=representative)
mrepresentative.__dict__.update(representative.__dict__)
mrepresentative.save()
print("%s/%s\r" % (i, n)),
print('Mandates')
for i, representative in enumerate(Representative.objects.all()):
legislature_mandates = []
for i, mrepresentative in enumerate(MRepresentative.objects.all()):
representative = mrepresentative.representative_ptr
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()
mmandate = MMandate(mandate_ptr=mandate)
mmandate.__dict__.update(mandate.__dict__)
mmandate.mrepresentative = mrepresentative
# Group creation
try:
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('Groups')
for i, mgroup in enumerate(MGroup.objects.all()):
mgroup.update_active()
......@@ -7,44 +7,46 @@ from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('representatives', '0004_representative_country'),
('representatives', '0007_auto_20150323_1017'),
]
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',
name='MGroup',
fields=[
('group_ptr', models.OneToOneField(parent_link=True, auto_created=True, primary_key=True, serialize=False, to='representatives.Group')),
('active', models.BooleanField(default=False)),
],
options={
},
bases=('representatives.group',),
),
migrations.CreateModel(
name='Mandate',
name='MMandate',
fields=[
('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={
},
bases=('representatives.mandate',),
),
migrations.CreateModel(
name='Representative',
name='MRepresentative',
fields=[
('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={
},
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
import datetime
class Representative(representatives.models.Representative):
class MRepresentative(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)
return self.mmandate_set.filter(active=True)
def former_mandates(self):
return self.mandate_set.filter(active=False)
return self.mmandate_set.filter(active=False)
def current_group(self):
return self.mandate_set.get(
def current_group_mandate(self):
return self.mmandate_set.get(
active=True,
group__kind='group'
)
......@@ -22,10 +22,10 @@ class Representative(representatives.models.Representative):
def update_active(self):
# If a representative has at least one active manadate
self.active = False
for mandate in self.mandate_set.all():
for mandate in self.mmandate_set.all():
if mandate.active:
self.active = True
continue
break
self.save()
......@@ -33,7 +33,7 @@ class Representative(representatives.models.Representative):
# 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(
country_mandate = self.mmandate_set.filter(
group__kind='country'
).order_by('-begin_date')[0:1].get()
......@@ -45,7 +45,22 @@ class Representative(representatives.models.Representative):
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):
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 @@
={representative.full_name}
%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}
%td
%a{'href': "{{ representative.current_group|by_mandate_url }}"}
={representative.current_group.group.abbreviation}
%a{'href': "{{ representative.current_group_mandate|by_mandate_url }}"}
={representative.current_group_mandate.mgroup.abbreviation}
{% extends 'base.html' %}
{% load by_mandate_url %}
{% load by_group_url %}
{% block content %}
......@@ -10,8 +10,8 @@
<h1>{{ representative.full_name }}</h1>
<p>
<strong>
<a href="{{ representative.current_group|by_mandate_url }}">
{{ representative.current_group.role }} of {{ representative.current_group.group.name }}
<a href="{{ representative.current_group_mandate|by_mandate_url }}">
{{ representative.current_group_mandate.role }} of {{ representative.current_group_mandate.mgroup.name }}
</a>
</strong>
</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()
@register.filter
def by_mandate_url(mandate):
kwargs = {'mandate_kind': mandate.group.kind}
def by_group_url(group):
kwargs = {'group_kind': group.kind}
if mandate.group.abbreviation:
kwargs['search'] = mandate.group.abbreviation
if group.abbreviation:
kwargs['search'] = group.abbreviation
else:
kwargs['search'] = mandate.group.name
kwargs['search'] = group.name
return reverse(
'legislature:representatives_by_mandate',
'legislature:representatives_by_group',
kwargs=kwargs
)
@register.filter
def by_mandate_url(mandate):
return by_group_url(mandate.group)
......@@ -15,9 +15,9 @@ urlpatterns = patterns(
name='representative_view'
),
url(
r'^representatives/(?P<mandate_kind>\w+)/(?P<search>.+)$',
views.representatives_by_mandate,
name='representatives_by_mandate'
r'^representatives/(?P<group_kind>\w+)/(?P<search>.+)$',
views.representatives_by_group,
name='representatives_by_group'
),
url(
r'^representatives/(?P<name>.+)$',
......@@ -25,8 +25,8 @@ urlpatterns = patterns(
name='representative_view_by_name'
),
url(
r'^group/(?P<kind>\w+)$',
views.group_by_kind,
name='group_by_kind'
r'^groups/(?P<kind>\w+)$',
views.groups_by_kind,
name='groups_by_kind'
)
)
......@@ -2,14 +2,13 @@ from django.shortcuts import render, get_object_or_404
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.db.models import Q
from legislature.models import Representative
from representatives.models import Group
from legislature.models import MRepresentative, MGroup
def representatives_index(request):
representative_list = _filter_by_search(
request,
Representative.objects.all()
MRepresentative.objects.all()
)
return _render_list(request, representative_list)
......@@ -17,7 +16,7 @@ def representatives_index(request):
def representative_by_name(request, name):
representative = get_object_or_404(
Representative, full_name=name)
MRepresentative, full_name=name)
return render(
request,
'legislature/representative_view.html',
......@@ -26,7 +25,7 @@ def representative_by_name(request, name):
def representative_view(request, num):
representative = get_object_or_404(Representative, pk=num)
representative = get_object_or_404(MRepresentative, pk=num)
return render(
request,
......@@ -35,36 +34,36 @@ def representative_view(request, num):
)
def representatives_by_mandate(request, mandate_kind, mandate_abbr=None,
mandate_name=None, search=None):
if mandate_abbr:
representative_list = Representative.objects.filter(
mandate__group__abbreviation=mandate_abbr,
mandate__group__kind=mandate_kind,
mandate__active=True
def representatives_by_group(request, group_kind, group_abbr=None,
group_name=None, search=None):
if group_abbr:
representative_list = MRepresentative.objects.filter(
mmandate__mgroup__abbreviation=group_abbr,
mmandate__mgroup__kind=group_kind,
mmandate__active=True
)
elif mandate_name:
representative_list = Representative.objects.filter(
Q(mandate__group__name__icontains=mandate_name),
mandate__group__kind=mandate_kind,
mandate__active=True
elif group_name:
representative_list = MRepresentative.objects.filter(
Q(mmandate__mgroup__name__icontains=group_name),
mmandate__mgroup__kind=group_kind,
mmandate__active=True
)
elif search:
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
MGroup.objects.get(abbreviation=search, kind=group_kind)
representative_list = MRepresentative.objects.filter(
mmandate__mgroup__abbreviation=search,
mmandate__mgroup__kind=group_kind,
mmandate__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
except MGroup.DoesNotExist:
representative_list = MRepresentative.objects.filter(
Q(mmandate__mgroup__abbreviation__icontains=search) |
Q(mmandate__mgroup__name__icontains=search),
mmandate__mgroup__kind=group_kind,
mmandate__active=True
)
# Select distinct representatives and filter by search
......@@ -89,7 +88,7 @@ def _filter_by_search(request, representative_list):
return representative_list
def _render_list(request, representative_list, num_by_page=50):
def _render_list(request, representative_list, num_by_page=30):
"""
Render a paginated list of representatives
"""
......@@ -118,13 +117,14 @@ def _render_list(request, representative_list, num_by_page=50):
)
def group_by_kind(request, kind):
groups = Group.objects.filter(
kind=kind
def groups_by_kind(request, kind):
groups = MGroup.objects.filter(
kind=kind,
active=True
)
return render(
request,
'legislature/group_list.html',
'legislature/groups_list.html',
{'groups': groups}
)
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