Commit 74a999b8 authored by njoyard's avatar njoyard

Merge pull request #32 from political-memory/remove-views

Remove features that don't belong here
parents 1e30571f 6fad3d03
......@@ -4,7 +4,6 @@ import hashlib
from datetime import datetime
from django.db import models
from django.core.urlresolvers import reverse
from django.utils.encoding import smart_str, smart_unicode
from django.utils.functional import cached_property
......@@ -82,10 +81,6 @@ class Country(models.Model):
def __unicode__(self):
return u'{} [{}]'.format(self.name, self.code)
def get_absolute_url(self):
return reverse('representatives:representative-list',
kwargs=dict(group_kind='country', group=self.name))
class Representative(HashableModel, TimeStampedModel):
"""
......@@ -121,10 +116,6 @@ 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
......@@ -201,15 +192,6 @@ class Group(HashableModel, TimeStampedModel):
class Meta:
ordering = ('name',)
def get_absolute_url(self):
if self.chamber:
return reverse('representatives:representative-list',
kwargs=dict(group_kind=self.kind, chamber=self.chamber.name,
group=self.name))
else:
return reverse('representatives:representative-list',
kwargs=dict(group_kind=self.kind, group=self.name))
class Constituency(HashableModel, TimeStampedModel):
"""
......
......@@ -2,30 +2,10 @@ from django import test
from responsediff.response import Response
from representatives.models import Representative
from representatives.views import RepresentativeViewMixin
class RepresentativeManagerTest(test.TestCase):
fixtures = ['representatives_test.json']
def test_prefetch_profile(self):
test = RepresentativeViewMixin()
reps = test.prefetch_for_representative_country_and_main_mandate(
Representative.objects.order_by('pk'))
with self.assertNumQueries(2):
# Cast to list to avoid [index] to cast a select with an offset
# below !
reps = [test.add_representative_country_and_main_mandate(r)
for r in reps]
assert reps[0].country.code == 'AT'
assert reps[0].main_mandate is None
assert reps[1].country.code == 'SE'
assert reps[1].main_mandate.pk == 15
def functional_test(self, queries, url):
with self.assertNumQueries(queries):
result = test.client.Client().get(url)
......
from django.conf.urls import url
from . import views
urlpatterns = [
url(
r'^representative/(?P<group_kind>\w+)/(?P<chamber>.+)/(?P<group>.+)/$',
views.RepresentativeList.as_view(),
name='representative-list'
),
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, Group
class RepresentativeViewMixin(object):
"""
A view mixin to add pre-fetched main_mandate and country to Representative
If a Representative was fetched from a QuerySet that have been through
prefetch_for_representative_country_and_main_mandate(), then
add_representative_country_and_main_mandate(representative) adds the
``.country`` and ``.main_mandate`` properties "for free" - the prefetch
methods adds an extra query, but gets all.
"""
def prefetch_for_representative_country_and_main_mandate(self, queryset):
"""
Prefetch Mandates with their Group and Constituency with Country.
"""
mandates = Mandate.objects.order_by(
'-end_date').select_related('constituency__country', 'group',
'group__chamber')
return queryset.prefetch_related(
models.Prefetch('mandates', queryset=mandates))
def add_representative_country_and_main_mandate(self, representative):
"""
Set representative country, main_mandate and chamber.
Note that this will butcher your database if you don't use
self.prefetch_related.
"""
today = datetime.date.today()
representative.country = None
representative.main_mandate = None
for m in representative.mandates.all():
if m.constituency.country_id and not representative.country:
representative.country = m.constituency.country
if ((m.end_date is None or m.end_date > today) and
m.group.kind == 'group' and
not representative.main_mandate):
representative.main_mandate = m
if representative.country and representative.main_mandate:
break
if representative.main_mandate:
representative.chamber = representative.main_mandate.group.chamber
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)
chamber = self.kwargs.get('chamber', None)
group = self.kwargs.get('group', None)
today = datetime.date.today()
if group_kind and group:
if group.isnumeric():
group_qs = Group.objects.filter(
id=int(group)
)
else:
group_qs = Group.objects.filter(
name=group,
kind=group_kind
)
if chamber:
group_qs = group_qs.filter(chamber__name=chamber)
qs = qs.filter(
models.Q(mandates__end_date__gte=today) |
models.Q(mandates__end_date__isnull=True),
mandates__group__in=group_qs
)
return qs
def get_queryset(self):
qs = super(RepresentativeList, self).get_queryset()
qs = self.group_filter(qs)
qs = self.search_filter(qs)
qs = self.prefetch_for_representative_country_and_main_mandate(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(
models.Q(mandates__end_date__gte=datetime.date.today()) |
models.Q(mandates__end_date__isnull=True)
)
kind = self.kwargs.get('kind', None)
if kind:
qs = qs.filter(kind=kind).distinct()
return qs.select_related('chamber').order_by('chamber__name', 'name')
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