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 ( ...@@ -23,14 +23,16 @@ from representatives_recommendations.api import (
router = routers.DefaultRouter() router = routers.DefaultRouter()
router.register(r'constituencies', ConstituencyViewSet) router.register(r'constituencies', ConstituencyViewSet, 'api-constituency')
router.register(r'dossiers', DossierViewSet) router.register(r'dossiers', DossierViewSet, 'api-dossier')
router.register(r'dossier_scores', DossierScoreViewSet) router.register(r'dossier_scores', DossierScoreViewSet, 'api-dossierscore')
router.register(r'groups', GroupViewSet) router.register(r'groups', GroupViewSet, 'api-group')
router.register(r'mandates', MandateViewSet) router.register(r'mandates', MandateViewSet, 'api-mandate')
router.register(r'proposals', ProposalViewSet) router.register(r'proposals', ProposalViewSet, 'api-proposal')
router.register(r'recommendations', RecommendationViewSet) router.register(r'recommendations', RecommendationViewSet,
router.register(r'representatives', RepresentativeViewSet) 'api-recommendation')
router.register(r'scores', RepresentativeScoreViewSet) router.register(r'representatives', RepresentativeViewSet,
router.register(r'vote_scores', VoteScoreViewSet) 'api-representative')
router.register(r'votes', VoteViewSet) 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 # coding: utf-8
import re
from django import template from django import template
from django.core.urlresolvers import reverse
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
import re from django.utils.html import escape
register = template.Library() register = template.Library()
link = '<a class="{network}-link" href="{url}" target="_blank">{label}</a>' link = '<a class="{network}-link" href="{url}" target="_blank">{label}</a>'
...@@ -30,3 +34,34 @@ def website_link(url): ...@@ -30,3 +34,34 @@ def website_link(url):
def email_link(address): def email_link(address):
return mark_safe(link.format(network='email', url='mailto:%s' % address, return mark_safe(link.format(network='email', url='mailto:%s' % address,
label=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 @@ ...@@ -43,27 +43,27 @@
</a> </a>
</li> </li>
<li> <li>
<a href='/legislature/groups/country/'> <a href='/legislature/group/country/'>
Countries Countries
</a> </a>
</li> </li>
<li> <li>
<a href='/legislature/groups/chamber/'> <a href='/legislature/group/chamber/'>
Chambers Chambers
</a> </a>
</li> </li>
<li> <li>
<a href='/legislature/groups/group/'> <a href='/legislature/group/group/'>
Parties Parties
</a> </a>
</li> </li>
<li> <li>
<a href='/legislature/groups/delegation/'> <a href='/legislature/group/delegation/'>
Delegations Delegations
</a> </a>
</li> </li>
<li> <li>
<a href='/legislature/groups/committee/'> <a href='/legislature/group/committee/'>
Committees Committees
</a> </a>
</li> </li>
......
...@@ -43,27 +43,27 @@ ...@@ -43,27 +43,27 @@
</a> </a>
</li> </li>
<li> <li>
<a href='/legislature/groups/country/'> <a href='/legislature/group/country/'>
Countries Countries
</a> </a>
</li> </li>
<li> <li>
<a href='/legislature/groups/chamber/'> <a href='/legislature/group/chamber/'>
Chambers Chambers
</a> </a>
</li> </li>
<li> <li>
<a href='/legislature/groups/group/'> <a href='/legislature/group/group/'>
Parties Parties
</a> </a>
</li> </li>
<li> <li>
<a href='/legislature/groups/delegation/'> <a href='/legislature/group/delegation/'>
Delegations Delegations
</a> </a>
</li> </li>
<li> <li>
<a href='/legislature/groups/committee/'> <a href='/legislature/group/committee/'>
Committees Committees
</a> </a>
</li> </li>
......
...@@ -43,27 +43,27 @@ ...@@ -43,27 +43,27 @@
</a> </a>
</li> </li>
<li> <li>
<a href='/legislature/groups/country/'> <a href='/legislature/group/country/'>
Countries Countries
</a> </a>
</li> </li>
<li> <li>
<a href='/legislature/groups/chamber/'> <a href='/legislature/group/chamber/'>
Chambers Chambers
</a> </a>
</li> </li>
<li> <li>
<a href='/legislature/groups/group/'> <a href='/legislature/group/group/'>
Parties Parties
</a> </a>
</li> </li>
<li> <li>
<a href='/legislature/groups/delegation/'> <a href='/legislature/group/delegation/'>
Delegations Delegations
</a> </a>
</li> </li>
<li> <li>
<a href='/legislature/groups/committee/'> <a href='/legislature/group/committee/'>
Committees Committees
</a> </a>
</li> </li>
......
...@@ -43,27 +43,27 @@ ...@@ -43,27 +43,27 @@
</a> </a>
</li> </li>
<li> <li>
<a href='/legislature/groups/country/'> <a href='/legislature/group/country/'>
Countries Countries
</a> </a>
</li> </li>
<li> <li>
<a href='/legislature/groups/chamber/'> <a href='/legislature/group/chamber/'>
Chambers Chambers
</a> </a>
</li> </li>
<li> <li>
<a href='/legislature/groups/group/'> <a href='/legislature/group/group/'>
Parties Parties
</a> </a>
</li> </li>
<li> <li>
<a href='/legislature/groups/delegation/'> <a href='/legislature/group/delegation/'>
Delegations Delegations
</a> </a>
</li> </li>
<li> <li>
<a href='/legislature/groups/committee/'> <a href='/legislature/group/committee/'>
Committees Committees
</a> </a>
</li> </li>
......
...@@ -43,27 +43,27 @@ ...@@ -43,27 +43,27 @@
</a> </a>
</li> </li>
<li> <li>
<a href='/legislature/groups/country/'> <a href='/legislature/group/country/'>
Countries Countries
</a> </a>
</li> </li>
<li> <li>
<a href='/legislature/groups/chamber/'> <a href='/legislature/group/chamber/'>
Chambers Chambers
</a> </a>
</li> </li>
<li> <li>
<a href='/legislature/groups/group/'> <a href='/legislature/group/group/'>
Parties Parties
</a> </a>
</li> </li>
<li> <li>
<a href='/legislature/groups/delegation/'> <a href='/legislature/group/delegation/'>
Delegations Delegations
</a> </a>
</li> </li>
<li> <li>
<a href='/legislature/groups/committee/'> <a href='/legislature/group/committee/'>
Committees Committees
</a> </a>
</li> </li>
......
# -*- coding: utf8 -*- # -*- coding: utf8 -*-
from django.test import TestCase from django.test import TestCase
from responsediff.response import Response from responsediff.response import Response
from .base import UrlGetTestMixin from .base import UrlGetTestMixin
from representatives.models import Representative
from ..views.representative_mixin import RepresentativeViewMixin
class RepresentativeListTest(UrlGetTestMixin, TestCase): class RepresentativeListTest(UrlGetTestMixin, TestCase):
fixtures = ['smaller_sample.json'] fixtures = ['smaller_sample.json']
url = '/legislature/representative/' 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=''): def functional_test(self, page, paginate_by, display, search=''):
url = '%s?page=%s&search=%s' % (self.url, page, search) url = '%s?page=%s&search=%s' % (self.url, page, search)
......
...@@ -3,7 +3,13 @@ from django.conf.urls import include, url ...@@ -3,7 +3,13 @@ from django.conf.urls import include, url
from django.contrib import admin from django.contrib import admin
from django.views import generic 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 import api
admin.autodiscover() admin.autodiscover()
...@@ -11,32 +17,58 @@ admin.autodiscover() ...@@ -11,32 +17,58 @@ admin.autodiscover()
urlpatterns = [ urlpatterns = [
# Project-specific overrides # Project-specific overrides
url( url(
r'^legislature/representative/(?P<group_kind>\w+)/(?P<chamber>.+)/' + r'^legislature/representatives?/(?P<group_kind>\w+)/(?P<chamber>.+)/' +
r'(?P<group>.+)/$', 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( url(
r'^legislature/representative/(?P<group_kind>\w+)/(?P<group>.+)/$', r'^votes/dossiers?/$',
views.RepresentativeList.as_view(), DossierList.as_view(),
name='dossier-list'
), ),
url( url(
r'^legislature/representative/(?P<slug>[-\w]+)/$', r'^votes/dossiers?/(?P<pk>\d+)/$',
views.RepresentativeDetail.as_view(), DossierDetail.as_view(),
name='dossier-detail'
), ),
url( url(
r'^legislature/representative/$', r'^votes/autocomplete/dossier/$',
views.RepresentativeList.as_view(), DossierAutocomplete.as_view(),
name='dossier-autocomplete',
), ),
url( url(
r'^votes/dossier/$', r'^votes/autocomplete/proposal/$',
views.DossierList.as_view(), ProposalAutocomplete.as_view(),
name='proposal-autocomplete',
), ),
url(r'^admin/', include(admin.site.urls)), 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', url(r'^positions/', include('representatives_positions.urls',
namespace='representatives_positions')), namespace='representatives_positions')),
url(r'^api/', include(api.router.urls)), 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 import models
from django.db.models import Count from django.views import generic
from core.views import GridListMixin, PaginationMixin, CSVDownloadMixin from representatives.models import Representative, Address, Phone, WebSite
from representatives import views as representatives_views
from representatives.models import (Representative, Address, Phone, WebSite)