Commit 3850f5fe authored by Jamesie Pic's avatar Jamesie Pic

Added default pagination for web browser

parent 6bbbd2aa
......@@ -7,8 +7,8 @@ python:
before_install:
- pip install codecov
install:
- pip install $DJANGO pep8 flake8 pytest-django pytest-cov codecov
- pip install -e .
- pip install $DJANGO pep8 flake8 pytest-django pytest-cov codecov django-responsediff
- pip install -e .[api]
script:
- pep8 representatives/ --exclude migrations --ignore E128
- flake8 representatives/ --exclude migrations --ignore E128
......
......@@ -2,6 +2,8 @@ from django.db import models
from rest_framework import (
filters,
pagination,
renderers,
viewsets,
)
......@@ -23,6 +25,20 @@ from .models import (
)
class DefaultWebPagination(pagination.PageNumberPagination):
default_web_page_size = 10
def get_page_size(self, request):
web = isinstance(request.accepted_renderer,
renderers.BrowsableAPIRenderer)
size = pagination.PageNumberPagination.get_page_size(self, request)
if web and not size:
return self.default_web_page_size
return size
class RepresentativeViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint that allows representatives to be viewed.
......@@ -78,7 +94,8 @@ class MandateViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint that allows mandates to be viewed.
"""
queryset = Mandate.objects.all()
pagination_class = DefaultWebPagination
queryset = Mandate.objects.select_related('representative')
serializer_class = MandateSerializer
filter_backends = (
......@@ -93,17 +110,19 @@ class MandateViewSet(viewsets.ReadOnlyModelViewSet):
}
search_fields = ('group__name', 'group__abbreviation')
def get_queryset(self):
qs = super(MandateViewSet, self).get_queryset()
qs = qs.select_related('representative')
return qs
@property
def paginator(self):
paginator = super(MandateViewSet, self).paginator
return paginator
class ConstituencyViewSet(viewsets.ReadOnlyModelViewSet):
pagination_class = DefaultWebPagination
queryset = Constituency.objects.all()
serializer_class = ConstituencySerializer
class GroupViewSet(viewsets.ReadOnlyModelViewSet):
pagination_class = DefaultWebPagination
queryset = Group.objects.all()
serializer_class = GroupSerializer
[{"id":1,"name":"European Parliament","fingerprint":"bc0c3668435accfa5da4479c80478fe68d15ddb1"},{"id":2,"name":"Österreichische Volkspartei","fingerprint":"74e9c77e0664716d098cb1194927b86f2aa55f7e"},{"id":3,"name":"Arbetarepartiet- Socialdemokraterna","fingerprint":"a8fa2ff595aedee63954c3b5ad6e1dcd5dfac910"}]
\ No newline at end of file
[{"id":6,"name":"Austria","abbreviation":"AT","kind":"country","fingerprint":"7b37fb29f750421d82ee36539c2cb5bca9cef938"},{"id":8,"name":"Committee on Economic and Monetary Affairs","abbreviation":"ECON","kind":"committee","fingerprint":"2da973401cd76bb56b36f3a9162cda13d2f2d8f3"},{"id":1,"name":"Committee on Employment and Social Affairs","abbreviation":"EMPL","kind":"committee","fingerprint":"7f0e6d2c3342dde2c6077e127a418a19e0e891bb"},{"id":9,"name":"Committee on Industry, Research and Energy","abbreviation":"ITRE","kind":"committee","fingerprint":"17ff1a9fe036e400612c9153c1647a8258c34d63"},{"id":7,"name":"Conference of Delegation Chairs","abbreviation":"","kind":"organization","fingerprint":"eb99949bb40fd2e906d738eef276a912c5412f67"},{"id":12,"name":"Delegation for relations with Australia and New Zealand","abbreviation":"","kind":"delegation","fingerprint":"1e5d79317501c2fdf7f36846ad1d5491a244a103"},{"id":11,"name":"Delegation for relations with Bosnia and Herzegovina, and Kosovo","abbreviation":"","kind":"delegation","fingerprint":"3d2d9370cc501b1208a4692baf69deeab1e65d0a"},{"id":3,"name":"Delegation for relations with the Member States of ASEAN, South-east Asia and the Republic of Korea","abbreviation":"","kind":"delegation","fingerprint":"3d0bc2c9506b90bb22e02e3866c1d14c9be1867e"},{"id":2,"name":"Delegation for relations with the countries of Southeast Asia and the Association of Southeast Asian Nations (ASEAN)","abbreviation":"","kind":"delegation","fingerprint":"23bb5f3e808b2ea6a02ddbe01705714ecfe7a82a"},{"id":10,"name":"Delegation to the EU-Serbia Stabilisation and Association Parliamentary Committee","abbreviation":"","kind":"delegation","fingerprint":"a24b4e184cf25f0e300c07a1402b65243f5ba93d"},{"id":4,"name":"Group of the European People's Party (Christian Democrats) and European Democrats","abbreviation":"PPE-DE","kind":"group","fingerprint":"cab4a9a406aa114716967e6fd6e698dfd2421794"},{"id":5,"name":"Group of the European People's Party (Christian-Democratic Group)","abbreviation":"EPP","kind":"group","fingerprint":"669373eca233e4101a0af25debda58fdfd59741f"},{"id":13,"name":"Group of the Progressive Alliance of Socialists and Democrats in the European Parliament","abbreviation":"SD","kind":"group","fingerprint":"729890eba3ce20f90808b32110871d1fe41b53b6"},{"id":14,"name":"Sweden","abbreviation":"SE","kind":"country","fingerprint":"ea4cacc64af470d01e5b4da150b8b9d3a9dc4bfe"}]
\ No newline at end of file
[{"id":9,"representative":"314d0f4c25af31bfa2a6b286838367994b902615","group":"2da973401cd76bb56b36f3a9162cda13d2f2d8f3","constituency":"bc0c3668435accfa5da4479c80478fe68d15ddb1","role":"Member","begin_date":"2014-07-01","end_date":"9999-12-31","fingerprint":"5323fd311cf0cb24122be532666fabb7ef23fa0c"},{"id":10,"representative":"314d0f4c25af31bfa2a6b286838367994b902615","group":"17ff1a9fe036e400612c9153c1647a8258c34d63","constituency":"bc0c3668435accfa5da4479c80478fe68d15ddb1","role":"Substitute","begin_date":"2014-07-08","end_date":"9999-12-31","fingerprint":"9d57bd7941ddda428728446908f8dd7aa642c764"},{"id":12,"representative":"314d0f4c25af31bfa2a6b286838367994b902615","group":"a24b4e184cf25f0e300c07a1402b65243f5ba93d","constituency":"bc0c3668435accfa5da4479c80478fe68d15ddb1","role":"Member","begin_date":"2014-07-14","end_date":"9999-12-31","fingerprint":"20a93b100a172b29f9ec6c6f94a8faeb1754a075"},{"id":13,"representative":"314d0f4c25af31bfa2a6b286838367994b902615","group":"3d2d9370cc501b1208a4692baf69deeab1e65d0a","constituency":"bc0c3668435accfa5da4479c80478fe68d15ddb1","role":"Substitute","begin_date":"2015-05-18","end_date":"9999-12-31","fingerprint":"b65a685d8e4144e36aff12b64bf6d835af1b95a5"},{"id":15,"representative":"314d0f4c25af31bfa2a6b286838367994b902615","group":"729890eba3ce20f90808b32110871d1fe41b53b6","constituency":"bc0c3668435accfa5da4479c80478fe68d15ddb1","role":"Member","begin_date":"2014-07-01","end_date":"9999-12-31","fingerprint":"f5c73825add38bb2c5f8de70d88b9effa6d6527f"},{"id":17,"representative":"314d0f4c25af31bfa2a6b286838367994b902615","group":"ea4cacc64af470d01e5b4da150b8b9d3a9dc4bfe","constituency":"a8fa2ff595aedee63954c3b5ad6e1dcd5dfac910","role":"","begin_date":"2014-07-01","end_date":"9999-12-31","fingerprint":"6058eddb2a4542a23de70a2528993eba09b5817a"},{"id":2,"representative":"2a3c90346d40e9c540050534d832ceb3e0d25a49","group":"23bb5f3e808b2ea6a02ddbe01705714ecfe7a82a","constituency":"bc0c3668435accfa5da4479c80478fe68d15ddb1","role":"Member","begin_date":"2013-10-09","end_date":"2014-06-30","fingerprint":"18613b505dc43a9a3055bf7aed2b2651246838f8"},{"id":16,"representative":"314d0f4c25af31bfa2a6b286838367994b902615","group":"729890eba3ce20f90808b32110871d1fe41b53b6","constituency":"bc0c3668435accfa5da4479c80478fe68d15ddb1","role":"Member","begin_date":"2009-07-14","end_date":"2014-06-30","fingerprint":"5765a17282ede4b110bedd2af0b1e5cc87673e6e"},{"id":18,"representative":"314d0f4c25af31bfa2a6b286838367994b902615","group":"ea4cacc64af470d01e5b4da150b8b9d3a9dc4bfe","constituency":"a8fa2ff595aedee63954c3b5ad6e1dcd5dfac910","role":"","begin_date":"2009-07-14","end_date":"2014-06-30","fingerprint":"4d9c58327c6b94bdf8a01e5c4d1fbcf751abaf8f"},{"id":14,"representative":"314d0f4c25af31bfa2a6b286838367994b902615","group":"1e5d79317501c2fdf7f36846ad1d5491a244a103","constituency":"bc0c3668435accfa5da4479c80478fe68d15ddb1","role":"Substitute","begin_date":"2009-09-17","end_date":"2013-01-10","fingerprint":"681dac891286446feb02863db19826f079cfed12"},{"id":11,"representative":"314d0f4c25af31bfa2a6b286838367994b902615","group":"2da973401cd76bb56b36f3a9162cda13d2f2d8f3","constituency":"bc0c3668435accfa5da4479c80478fe68d15ddb1","role":"Substitute","begin_date":"2009-07-16","end_date":"2012-01-18","fingerprint":"5953eb229dd3c0993c1af3654fa7dfc052614002"},{"id":8,"representative":"2a3c90346d40e9c540050534d832ceb3e0d25a49","group":"eb99949bb40fd2e906d738eef276a912c5412f67","constituency":"bc0c3668435accfa5da4479c80478fe68d15ddb1","role":"Member","begin_date":"2006-03-21","end_date":"2009-07-13","fingerprint":"0926a8c1dbc827362c3eabfdca87a5b595706120"},{"id":4,"representative":"2a3c90346d40e9c540050534d832ceb3e0d25a49","group":"cab4a9a406aa114716967e6fd6e698dfd2421794","constituency":"bc0c3668435accfa5da4479c80478fe68d15ddb1","role":"Member","begin_date":"1999-07-20","end_date":"2004-07-19","fingerprint":"1d054cac567dd5bc523ddf2063dd05971d7b9c88"},{"id":6,"representative":"2a3c90346d40e9c540050534d832ceb3e0d25a49","group":"7b37fb29f750421d82ee36539c2cb5bca9cef938","constituency":"74e9c77e0664716d098cb1194927b86f2aa55f7e","role":"","begin_date":"1999-07-20","end_date":"2004-07-19","fingerprint":"d761586241fb47f9fae7035365f5253cd8a363fb"},{"id":1,"representative":"2a3c90346d40e9c540050534d832ceb3e0d25a49","group":"7f0e6d2c3342dde2c6077e127a418a19e0e891bb","constituency":"bc0c3668435accfa5da4479c80478fe68d15ddb1","role":"Substitute","begin_date":"1997-01-16","end_date":"1999-07-19","fingerprint":"9d2cdb45658e7b535e4b39de233b8e2f747bc1d1"},{"id":5,"representative":"2a3c90346d40e9c540050534d832ceb3e0d25a49","group":"669373eca233e4101a0af25debda58fdfd59741f","constituency":"bc0c3668435accfa5da4479c80478fe68d15ddb1","role":"Member","begin_date":"1996-11-11","end_date":"1999-07-19","fingerprint":"0cfeaa28bfa4036f385bd7d81f29be83120fc985"},{"id":7,"representative":"2a3c90346d40e9c540050534d832ceb3e0d25a49","group":"7b37fb29f750421d82ee36539c2cb5bca9cef938","constituency":"74e9c77e0664716d098cb1194927b86f2aa55f7e","role":"","begin_date":"1996-11-11","end_date":"1999-07-19","fingerprint":"5e7d170fc24b161457a09baebcc388edcbddc954"},{"id":3,"representative":"2a3c90346d40e9c540050534d832ceb3e0d25a49","group":"3d0bc2c9506b90bb22e02e3866c1d14c9be1867e","constituency":"bc0c3668435accfa5da4479c80478fe68d15ddb1","role":"Member","begin_date":"1996-11-14","end_date":"1997-01-15","fingerprint":"fd6db1f22987be42ba290e1f8fbfda6cc109dd7e"}]
\ No newline at end of file
[{"id":2,"slug":"olle-ludvigsson","remote_id":"96673","first_name":"Olle","last_name":"LUDVIGSSON","full_name":"Olle LUDVIGSSON","gender":2,"birth_place":"Hälsö","birth_date":"1948-10-28","photo":"http://www.europarl.europa.eu/mepphoto/96673.jpg","active":true,"cv":"","contacts":{"emails":[{"email":"olle.ludvigsson@europarl.europa.eu","kind":"official"}],"phones":[{"number":"+322 28 45442","kind":"office phone"},{"number":"+333 88 1 75442","kind":"office phone"}],"websites":[{"url":"http://www.sap.se/olle","kind":""},{"url":"http://twitter.com/olleludvigsson","kind":"twitter"},{"url":"https://www.facebook.com/olle.ludvigsson","kind":"facebook"}],"address":[{"country":{"name":"Belgium","code":"BE"},"city":"Brussels","street":"rue Wiertz / Wiertzstraat","number":"60","postcode":"1047","floor":"14G","office_number":"257","kind":"official"},{"country":{"name":"France","code":"FR"},"city":"Strasbourg","street":"Av. du Président Robert Schuman - CS 91024","number":"1","postcode":"67070","floor":"T07","office_number":"070","kind":"official"}]},"fingerprint":"314d0f4c25af31bfa2a6b286838367994b902615","url":"http://testserver/api/representatives/2/?format=json"},{"id":1,"slug":"hubert-pirker","remote_id":"2307","first_name":"Hubert","last_name":"PIRKER","full_name":"Hubert PIRKER","gender":2,"birth_place":"Gries","birth_date":"1948-10-03","photo":"http://www.europarl.europa.eu/mepphoto/2307.jpg","active":false,"cv":"Transport and security spokesman, ÖVP Delegation, European Parliament;\nsecurity spokesman, ÖVP Delegation, European Parliament (2006-2009); security spokesman (coordinator), EPP Group (1999-2004); Deputy Head of ÖVP Delegation, European Parliament (1996-2004);","contacts":{"emails":[],"phones":[],"websites":[],"address":[]},"fingerprint":"2a3c90346d40e9c540050534d832ceb3e0d25a49","url":"http://testserver/api/representatives/1/?format=json"}]
\ No newline at end of file
......@@ -14,9 +14,12 @@ DATABASES = {
}
INSTALLED_APPS = (
'django.contrib.staticfiles',
'rest_framework',
'representatives',
)
STATIC_URL = '/static/'
DEBUG = True
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
......
from django.test import TestCase
from django import test
from responsediff.response import Response
from representatives.models import Representative
from representatives.views import RepresentativeViewMixin
class RepresentativeManagerTest(TestCase):
class RepresentativeManagerTest(test.TestCase):
fixtures = ['representatives_test.json']
def test_prefetch_profile(self):
......@@ -23,3 +25,30 @@ class RepresentativeManagerTest(TestCase):
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)
Response.for_test(self).assertNoDiff(result)
def test_constituencies_api(self):
self.functional_test(1, '/api/constituencies/?format=json')
def test_groups_api(self):
self.functional_test(1, '/api/groups/?format=json')
def test_mandates_api(self):
self.functional_test(1, '/api/mandates/?format=json')
def test_representatives_api(self):
"""
Queries:
- representatives,
- emails,
- websites,
- addresses,
- phones,
- mandates.
"""
self.functional_test(6, '/api/representatives/?format=json')
urlpatterns = []
from django.conf.urls import include, url
from rest_framework import routers
from representatives.api import (
ConstituencyViewSet,
GroupViewSet,
MandateViewSet,
RepresentativeViewSet,
)
router = routers.DefaultRouter()
router.register(r'constituencies', ConstituencyViewSet)
router.register(r'groups', GroupViewSet)
router.register(r'mandates', MandateViewSet)
router.register(r'representatives', RepresentativeViewSet)
urlpatterns = [
url(r'api/', include(router.urls)),
]
......@@ -16,7 +16,10 @@ setup(
],
extras_require={
'celery': 'celery',
'api': 'djangorestframework',
'api': [
'django-filter',
'djangorestframework',
]
},
entry_points={
'console_scripts': [
......
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