Commit e5dad7d7 authored by Nicolas Joyard's avatar Nicolas Joyard
Browse files

Integrate views removed from django-representatives[-votes]

parent 31deff01
......@@ -23,14 +23,16 @@ from representatives_recommendations.api import (
router = routers.DefaultRouter()
router.register(r'constituencies', ConstituencyViewSet)
router.register(r'dossiers', DossierViewSet)
router.register(r'dossier_scores', DossierScoreViewSet)
router.register(r'groups', GroupViewSet)
router.register(r'mandates', MandateViewSet)
router.register(r'proposals', ProposalViewSet)
router.register(r'recommendations', RecommendationViewSet)
router.register(r'representatives', RepresentativeViewSet)
router.register(r'scores', RepresentativeScoreViewSet)
router.register(r'vote_scores', VoteScoreViewSet)
router.register(r'votes', VoteViewSet)
router.register(r'constituencies', ConstituencyViewSet, 'api-constituency')
router.register(r'dossiers', DossierViewSet, 'api-dossier')
router.register(r'dossier_scores', DossierScoreViewSet, 'api-dossierscore')
router.register(r'groups', GroupViewSet, 'api-group')
router.register(r'mandates', MandateViewSet, 'api-mandate')
router.register(r'proposals', ProposalViewSet, 'api-proposal')
router.register(r'recommendations', RecommendationViewSet,
'api-recommendation')
router.register(r'representatives', RepresentativeViewSet,
'api-representative')
router.register(r'scores', RepresentativeScoreViewSet, 'api-score')
router.register(r'vote_scores', VoteScoreViewSet, 'api-votescore')
router.register(r'votes', VoteViewSet, 'api-vote')
# coding: utf-8
import re
from django import template
from django.core.urlresolvers import reverse
from django.utils.safestring import mark_safe
import re
from django.utils.html import escape
register = template.Library()
link = '<a class="{network}-link" href="{url}" target="_blank">{label}</a>'
......@@ -30,3 +34,34 @@ def website_link(url):
def email_link(address):
return mark_safe(link.format(network='email', url='mailto:%s' % address,
label=address))
@register.simple_tag
def group_url(group):
if group.kind == 'chamber' or group.chamber is None:
return escape(reverse('representative-list', kwargs={
'group_kind': group.kind,
'group': group.name
}))
else:
return escape(reverse('representative-list', kwargs={
'group_kind': group.kind,
'chamber': group.chamber.name,
'group': group.name
}))
@register.simple_tag
def chamber_url(chamber):
return escape(reverse('representative-list', kwargs={
'group_kind': 'chamber',
'group': chamber.name
}))
@register.simple_tag
def country_url(country):
return escape(reverse('representative-list', kwargs={
'group_kind': 'country',
'group': country.name
}))
......@@ -43,27 +43,27 @@
</a>
</li>
<li>
<a href='/legislature/groups/country/'>
<a href='/legislature/group/country/'>
Countries
</a>
</li>
<li>
<a href='/legislature/groups/chamber/'>
<a href='/legislature/group/chamber/'>
Chambers
</a>
</li>
<li>
<a href='/legislature/groups/group/'>
<a href='/legislature/group/group/'>
Parties
</a>
</li>
<li>
<a href='/legislature/groups/delegation/'>
<a href='/legislature/group/delegation/'>
Delegations
</a>
</li>
<li>
<a href='/legislature/groups/committee/'>
<a href='/legislature/group/committee/'>
Committees
</a>
</li>
......
......@@ -43,27 +43,27 @@
</a>
</li>
<li>
<a href='/legislature/groups/country/'>
<a href='/legislature/group/country/'>
Countries
</a>
</li>
<li>
<a href='/legislature/groups/chamber/'>
<a href='/legislature/group/chamber/'>
Chambers
</a>
</li>
<li>
<a href='/legislature/groups/group/'>
<a href='/legislature/group/group/'>
Parties
</a>
</li>
<li>
<a href='/legislature/groups/delegation/'>
<a href='/legislature/group/delegation/'>
Delegations
</a>
</li>
<li>
<a href='/legislature/groups/committee/'>
<a href='/legislature/group/committee/'>
Committees
</a>
</li>
......
......@@ -43,27 +43,27 @@
</a>
</li>
<li>
<a href='/legislature/groups/country/'>
<a href='/legislature/group/country/'>
Countries
</a>
</li>
<li>
<a href='/legislature/groups/chamber/'>
<a href='/legislature/group/chamber/'>
Chambers
</a>
</li>
<li>
<a href='/legislature/groups/group/'>
<a href='/legislature/group/group/'>
Parties
</a>
</li>
<li>
<a href='/legislature/groups/delegation/'>
<a href='/legislature/group/delegation/'>
Delegations
</a>
</li>
<li>
<a href='/legislature/groups/committee/'>
<a href='/legislature/group/committee/'>
Committees
</a>
</li>
......
......@@ -43,27 +43,27 @@
</a>
</li>
<li>
<a href='/legislature/groups/country/'>
<a href='/legislature/group/country/'>
Countries
</a>
</li>
<li>
<a href='/legislature/groups/chamber/'>
<a href='/legislature/group/chamber/'>
Chambers
</a>
</li>
<li>
<a href='/legislature/groups/group/'>
<a href='/legislature/group/group/'>
Parties
</a>
</li>
<li>
<a href='/legislature/groups/delegation/'>
<a href='/legislature/group/delegation/'>
Delegations
</a>
</li>
<li>
<a href='/legislature/groups/committee/'>
<a href='/legislature/group/committee/'>
Committees
</a>
</li>
......
......@@ -43,27 +43,27 @@
</a>
</li>
<li>
<a href='/legislature/groups/country/'>
<a href='/legislature/group/country/'>
Countries
</a>
</li>
<li>
<a href='/legislature/groups/chamber/'>
<a href='/legislature/group/chamber/'>
Chambers
</a>
</li>
<li>
<a href='/legislature/groups/group/'>
<a href='/legislature/group/group/'>
Parties
</a>
</li>
<li>
<a href='/legislature/groups/delegation/'>
<a href='/legislature/group/delegation/'>
Delegations
</a>
</li>
<li>
<a href='/legislature/groups/committee/'>
<a href='/legislature/group/committee/'>
Committees
</a>
</li>
......
# -*- coding: utf8 -*-
from django.test import TestCase
from responsediff.response import Response
from .base import UrlGetTestMixin
from representatives.models import Representative
from ..views.representative_mixin import RepresentativeViewMixin
class RepresentativeListTest(UrlGetTestMixin, TestCase):
fixtures = ['smaller_sample.json']
url = '/legislature/representative/'
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 == 'GB'
assert reps[0].main_mandate.pk == 3318
assert reps[1].country.code == 'FI'
assert reps[1].main_mandate.pk == 5545
def functional_test(self, page, paginate_by, display, search=''):
url = '%s?page=%s&search=%s' % (self.url, page, search)
......
......@@ -3,7 +3,13 @@ from django.conf.urls import include, url
from django.contrib import admin
from django.views import generic
import views
from views.dossier_ac import DossierAutocomplete, ProposalAutocomplete
from views.dossier_detail import DossierDetail
from views.dossier_list import DossierList
from views.group_list import GroupList
from views.representative_detail import RepresentativeDetail
from views.representative_list import RepresentativeList
import api
admin.autodiscover()
......@@ -11,32 +17,58 @@ admin.autodiscover()
urlpatterns = [
# Project-specific overrides
url(
r'^legislature/representative/(?P<group_kind>\w+)/(?P<chamber>.+)/' +
r'^legislature/representatives?/(?P<group_kind>\w+)/(?P<chamber>.+)/' +
r'(?P<group>.+)/$',
views.RepresentativeList.as_view(),
RepresentativeList.as_view(),
name='representative-list'
),
url(
r'^legislature/representatives?/(?P<group_kind>\w+)/(?P<group>.+)/$',
RepresentativeList.as_view(),
name='representative-list'
),
url(
r'^legislature/representatives?/(?P<slug>[-\w]+)/$',
RepresentativeDetail.as_view(),
name='representative-detail'
),
url(
r'^legislature/groups?/$',
GroupList.as_view(),
name='group-list'
),
url(
r'^legislature/groups?/(?P<kind>\w+)/$',
GroupList.as_view(),
name='group-list'
),
url(
r'^legislature/representatives?/$',
RepresentativeList.as_view(),
name='representative-list'
),
url(
r'^legislature/representative/(?P<group_kind>\w+)/(?P<group>.+)/$',
views.RepresentativeList.as_view(),
r'^votes/dossiers?/$',
DossierList.as_view(),
name='dossier-list'
),
url(
r'^legislature/representative/(?P<slug>[-\w]+)/$',
views.RepresentativeDetail.as_view(),
r'^votes/dossiers?/(?P<pk>\d+)/$',
DossierDetail.as_view(),
name='dossier-detail'
),
url(
r'^legislature/representative/$',
views.RepresentativeList.as_view(),
r'^votes/autocomplete/dossier/$',
DossierAutocomplete.as_view(),
name='dossier-autocomplete',
),
url(
r'^votes/dossier/$',
views.DossierList.as_view(),
r'^votes/autocomplete/proposal/$',
ProposalAutocomplete.as_view(),
name='proposal-autocomplete',
),
url(r'^admin/', include(admin.site.urls)),
url(r'^legislature/', include('representatives.urls',
namespace='representatives')),
url(r'^votes/', include('representatives_votes.urls',
namespace='representatives_votes')),
url(r'^positions/', include('representatives_positions.urls',
namespace='representatives_positions')),
url(r'^api/', include(api.router.urls)),
......
# coding: utf-8
from dal import autocomplete
from django.db.models import Q
from representatives_votes.models import Dossier, Proposal
class DossierAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self):
qs = Dossier.objects.all()
if self.q:
qs = qs.filter(
Q(title__icontains=self.q) |
Q(reference__icontains=self.q)
)
return qs
class ProposalAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self):
qs = Proposal.objects.all()
if self.q:
qs = qs.filter(
Q(dossier__title__icontains=self.q) |
Q(title__icontains=self.q) |
Q(reference__icontains=self.q)
)
return qs
# coding: utf-8
from django.views import generic
from representatives_votes.models import Dossier
class DossierDetail(generic.DetailView):
model = Dossier
# coding: utf-8
from core.views import PaginationMixin
from django.db.models import Count
from django.views import generic
from representatives_votes.models import Dossier
class DossierList(PaginationMixin, generic.ListView):
queryset = Dossier.objects.prefetch_related(
'proposals',
'proposals__recommendation'
).annotate(
nb_recomm=Count('proposals__recommendation')
).order_by('-nb_recomm', '-reference')
# coding: utf-8
import datetime
from django.db import models
from django.views import generic
from representatives.models import Group
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')
# Project specific "glue" coupling of all apps
# coding: utf-8
from django.db import models
from django.db.models import Count
from django.views import generic
from core.views import GridListMixin, PaginationMixin, CSVDownloadMixin
from representatives import views as representatives_views
from representatives.models import (Representative, Address, Phone, WebSite)
from representatives_votes import views as representatives_votes_views
from representatives_votes.models import Dossier, Proposal
from representatives.models import Representative, Address, Phone, WebSite
from representatives_positions.forms import PositionForm
from representatives_recommendations.models import VoteScore
from representatives_votes.models import Proposal
from .representative_mixin import RepresentativeViewMixin
class RepresentativeList(
CSVDownloadMixin,
GridListMixin,
PaginationMixin,
representatives_views.RepresentativeList
):
csv_name = 'meps.csv'
def get_csv_results(self, context, **kwargs):
qs = super(RepresentativeList, self).get_queryset()
qs = qs.prefetch_related('email_set')
return [self.add_representative_country_and_main_mandate(r)
for r in qs]
def get_csv_row(self, obj):
return (
obj.full_name,
u', '.join([e.email for e in obj.email_set.all()]),
obj.main_mandate.group.abbreviation,
obj.country,
)
queryset = Representative.objects.filter(
active=True).select_related('score')
class RepresentativeDetail(RepresentativeViewMixin, generic.DetailView):
class RepresentativeDetail(representatives_views.RepresentativeDetail):
queryset = Representative.objects.select_related('score')
def get_queryset(self):
qs = super(RepresentativeDetail, self).get_queryset()
qs = self.prefetch_for_representative_country_and_main_mandate(qs)
social = ['twitter', 'facebook']
qs = super(RepresentativeDetail, self).get_queryset().prefetch_related(
qs = qs.prefetch_related(
'email_set',
models.Prefetch(
'website_set',
......@@ -71,21 +49,21 @@ class RepresentativeDetail(representatives_views.RepresentativeDetail):
'-proposal__datetime')
)
)
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')
c['position_form'] = PositionForm(
initial={'representative': self.object.pk})
self.add_representative_country_and_main_mandate(c['object'])
return c
class DossierList(PaginationMixin, representatives_votes_views.DossierList):
queryset = Dossier.objects.prefetch_related(
'proposals',
'proposals__recommendation'
).annotate(
nb_recomm=Count('proposals__recommendation')
).order_by('-nb_recomm', '-reference')
# coding: utf-8
from core.views import GridListMixin, PaginationMixin, CSVDownloadMixin
import datetime
from django.db import models
from django.utils.text import slugify
from django.views import generic
from representatives.models import Group, Representative
from .representative_mixin import RepresentativeViewMixin
class RepresentativeList(CSVDownloadMixin, GridListMixin, PaginationMixin,
RepresentativeViewMixin, generic.ListView):
csv_name = 'meps.csv'
queryset = Representative.objects.filter(
active=True).select_related('score')
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(