Skip to content
Extraits de code Groupes Projets
Valider 0a6b37c3 rédigé par Jamesie Pic's avatar Jamesie Pic
Parcourir les fichiers

Added views

parent 4d0ccecb
Branches
Étiquettes
Aucune requête de fusion associée trouvée
# coding: utf-8
import hashlib
from datetime import datetime, date
from datetime import datetime
from django.db import models
from django.utils.encoding import smart_str, smart_unicode
......@@ -114,6 +114,9 @@ class Representative(HashableModel, TimeStampedModel):
class Meta:
ordering = ['last_name', 'first_name']
def get_absolute_url(self):
return reverse('representatives:representative-detail', args=(self.slug,))
# Contact related models
......
from django.conf.urls import url
from . import views
urlpatterns = [
url(
r'^representative/(?P<group_kind>\w+)/(?P<group>.+)/$',
views.RepresentativeList.as_view(),
name='representative-list'
),
url(
r'^representative/(?P<slug>[-\w]+)/$',
views.RepresentativeDetail.as_view(),
name='representative-detail'
),
url(
r'representative/',
views.RepresentativeList.as_view(),
name='representative-list'
),
url(
r'^groups/$',
views.GroupList.as_view(),
name='group-list'
),
url(
r'^groups/(?P<kind>\w+)/$',
views.GroupList.as_view(),
name='group-list'
),
]
import datetime
from django.db import models
from django.views import generic
from django.utils.text import slugify
from .models import Mandate
from .models import Mandate, Representative, Group
class RepresentativeViewMixin(object):
......@@ -42,11 +44,93 @@ class RepresentativeViewMixin(object):
representative.country = m.constituency.country
if (m.end_date > today and m.group.kind == 'group' and
not representative.main_mandate):
not representative.main_mandate):
representative.main_mandate = m
if representative.country and not representative.main_mandate:
if representative.country and representative.main_mandate:
break
return representative
class RepresentativeList(RepresentativeViewMixin, generic.ListView):
def get_context_data(self, **kwargs):
c = super(RepresentativeList, self).get_context_data(**kwargs)
c['object_list'] = [
self.add_representative_country_and_main_mandate(r)
for r in c['object_list']
]
return c
def search_filter(self, qs):
search = self.request.GET.get('search', None)
if search:
qs = qs.filter(slug__icontains=slugify(search))
return qs
def group_filter(self, qs):
group_kind = self.kwargs.get('group_kind', None)
group = self.kwargs.get('group', None)
if group_kind and group:
if group.isnumeric():
# Search group based on pk
qs = qs.filter(
mandates__group_id=int(group),
mandates__end_date__gte=datetime.now()
)
else:
# Search group based on abbreviation
qs = qs.filter(
mandates__group__name=group,
mandates__group__kind=group_kind,
mandates__end_date__gte=datetime.date.today()
)
return qs
def prefetch_related(self, qs):
qs = qs.select_related('score')
qs = self.prefetch_for_representative_country_and_main_mandate(qs)
return qs
def get_queryset(self):
qs = Representative.objects.filter(active=True)
qs = self.group_filter(qs)
qs = self.search_filter(qs)
qs = self.prefetch_related(qs)
return qs
class RepresentativeDetail(RepresentativeViewMixin, generic.DetailView):
def get_queryset(self):
qs = super(RepresentativeDetail, self).get_queryset()
qs = self.prefetch_for_representative_country_and_main_mandate(qs)
return qs
def get_context_data(self, **kwargs):
c = super(RepresentativeDetail, self).get_context_data(**kwargs)
self.add_representative_country_and_main_mandate(c['object'])
c['votes'] = c['object'].votes.all()
c['mandates'] = c['object'].mandates.all()
c['positions'] = c['object'].positions.filter(
published=True).prefetch_related('tags')
return c
class GroupList(generic.ListView):
def get_queryset(self):
qs = Group.objects.filter(
mandates__end_date__gte=datetime.date.today()
)
kind = self.kwargs.get('kind', None)
if kind:
qs = qs.filter(kind=kind).distinct()
return qs
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter