Skip to content
Extraits de code Groupes Projets
Valider 305aaeee rédigé par luxcem's avatar luxcem
Parcourir les fichiers

adds methods to legislatures models

parent d2564c1e
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
# 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)),
# -*- 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',),
),
]
# -*- 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,
),
]
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()
%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}
- extends "base.html"
- block content
- include 'legislature/search.html'
%p
......
......@@ -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',
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter