Skip to content

Commit

Permalink
Fixed the API
Browse files Browse the repository at this point in the history
It wasn't clear which URL would be what resource, ie. the proposal list
view displayed votes and not the proposal detail view.

Also changed the tests so that they requested indented responses which
will be easier to diff.
  • Loading branch information
jpic committed May 5, 2016
1 parent 1274e0d commit a8b4474
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 18 deletions.
9 changes: 3 additions & 6 deletions representatives_votes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class DossierViewSet(viewsets.ReadOnlyModelViewSet):
"""

pagination_class = DefaultWebPagination
queryset = Dossier.objects.prefetch_related('proposals__votes__representative')
queryset = Dossier.objects.all()
serializer_class = DossierSerializer

filter_backends = (
Expand Down Expand Up @@ -58,7 +58,7 @@ class ProposalViewSet(viewsets.ReadOnlyModelViewSet):
"""

pagination_class = DefaultWebPagination
queryset = Proposal.objects.select_related('dossier')
queryset = Proposal.objects.all()
serializer_class = ProposalSerializer

filter_backends = (
Expand All @@ -85,10 +85,7 @@ class ProposalViewSet(viewsets.ReadOnlyModelViewSet):
'dossier__reference'
)

ordering_fields = ('id', 'reference')

def list(self, request):
return super(ProposalViewSet, self).list(request)
ordering_fields = ('reference')

def retrieve(self, request, pk=None):
self.serializer_class = ProposalDetailSerializer
Expand Down
11 changes: 6 additions & 5 deletions representatives_votes/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class VoteSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = models.Vote
fields = (
'id',
'proposal',
'representative',
'representative_name',
Expand All @@ -24,7 +23,6 @@ class ProposalSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = models.Proposal
fields = (
'id',
'dossier',
'title',
'description',
Expand All @@ -41,26 +39,29 @@ class Meta:
class ProposalDetailSerializer(ProposalSerializer):
""" Proposal serializer that includes votes """

votes = VoteSerializer(many=True)

class Meta:
model = models.Proposal


class DossierSerializer(serializers.HyperlinkedModelSerializer):
""" Base dossier serializer """
proposals = ProposalSerializer(many=True)

class Meta:
model = models.Dossier
fields = (
'id',
'title',
'reference',
'text',
'link',
'url',
'proposals',
)


class DossierDetailSerializer(DossierSerializer):
"""
Dossier serializer that includes proposals and votes.
"""

proposals = ProposalDetailSerializer(many=True)
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
[{"id":1,"fingerprint":"9e2cccdc5f6d22afd008af8b5b55dc193c27c5d6","title":"Agenda for change: the future of EU development policy","reference":"2012/2002(INI)","text":"","link":"http://www.europarl.europa.eu/oeil/popups/ficheprocedure.do?reference=2012/2002(INI)","url":"http://testserver/api/dossiers/1/?format=json"},{"id":2,"fingerprint":"e6856e0880e701c1022f23d595cc37a9a1cdcca8","title":"2016 general budget: all sections","reference":"2015/2132(BUD)","text":"","link":"http://www.europarl.europa.eu/oeil/popups/ficheprocedure.do?reference=2015/2132(BUD)","url":"http://testserver/api/dossiers/2/?format=json"}]
[
{
"title": "Agenda for change: the future of EU development policy",
"reference": "2012/2002(INI)",
"text": "",
"link": "http://www.europarl.europa.eu/oeil/popups/ficheprocedure.do?reference=2012/2002(INI)",
"url": "http://testserver/api/dossiers/1/"
},
{
"title": "2016 general budget: all sections",
"reference": "2015/2132(BUD)",
"text": "",
"link": "http://www.europarl.europa.eu/oeil/popups/ficheprocedure.do?reference=2015/2132(BUD)",
"url": "http://testserver/api/dossiers/2/"
}
]
Original file line number Diff line number Diff line change
@@ -1 +1,74 @@
[{"id":1,"fingerprint":"6c1fb67feac3ab2ea567b37aa7dfd0d578bc04b4","dossier":"9e2cccdc5f6d22afd008af8b5b55dc193c27c5d6","dossier_title":"Agenda for change: the future of EU development policy","dossier_reference":"2012/2002(INI)","title":"A7-0234/2012 - Charles Goerens - § 31","description":"","reference":"A7-0234/2012","datetime":"2012-10-23T16:31:10Z","kind":"§ 31","total_abstain":2,"total_against":0,"total_for":0,"url":"http://testserver/api/proposals/1/?format=json"},{"id":2,"fingerprint":"18df8a97581832a95f3bdfb8c14ba2b05abb91e9","dossier":"9e2cccdc5f6d22afd008af8b5b55dc193c27c5d6","dossier_title":"Agenda for change: the future of EU development policy","dossier_reference":"2012/2002(INI)","title":"A7-0234/2012 - Charles Goerens - Résolution","description":"","reference":"A7-0234/2012","datetime":"2012-10-23T16:34:32Z","kind":"Résolution","total_abstain":0,"total_against":0,"total_for":2,"url":"http://testserver/api/proposals/2/?format=json"},{"id":3,"fingerprint":"4e575a1bc17602d23d2f0acf8ee482ebe08de79c","dossier":"e6856e0880e701c1022f23d595cc37a9a1cdcca8","dossier_title":"2016 general budget: all sections","dossier_reference":"2015/2132(BUD)","title":"A8-0298/2015 - José Manuel Fernandes et Gérard Deprez - Am 4","description":"","reference":"A8-0298/2015","datetime":"2015-10-28T11:59:35Z","kind":"Am 4","total_abstain":0,"total_against":2,"total_for":0,"url":"http://testserver/api/proposals/3/?format=json"},{"id":4,"fingerprint":"fc786534e832d9af7e2cdcd0b0952d7d60bb4331","dossier":"e6856e0880e701c1022f23d595cc37a9a1cdcca8","dossier_title":"2016 general budget: all sections","dossier_reference":"2015/2132(BUD)","title":"A8-0298/2015 - José Manuel Fernandes et Gérard Deprez - Am 29","description":"","reference":"A8-0298/2015","datetime":"2015-10-28T12:00:12Z","kind":"Am 29","total_abstain":0,"total_against":1,"total_for":1,"url":"http://testserver/api/proposals/4/?format=json"},{"id":5,"fingerprint":"90c022cf70dc6ceff796b876d3e1b814620c8637","dossier":"e6856e0880e701c1022f23d595cc37a9a1cdcca8","dossier_title":"2016 general budget: all sections","dossier_reference":"2015/2132(BUD)","title":"A8-0298/2015 - José Manuel Fernandes et Gérard Deprez - Am 31","description":"","reference":"A8-0298/2015","datetime":"2015-10-28T12:00:42Z","kind":"Am 31","total_abstain":1,"total_against":1,"total_for":0,"url":"http://testserver/api/proposals/5/?format=json"},{"id":6,"fingerprint":"5b3aa46182803c11d70ccfec666234b9fe2f44a7","dossier":"e6856e0880e701c1022f23d595cc37a9a1cdcca8","dossier_title":"2016 general budget: all sections","dossier_reference":"2015/2132(BUD)","title":"A8-0298/2015 - José Manuel Fernandes et Gérard Deprez - Am 30","description":"","reference":"A8-0298/2015","datetime":"2015-10-28T12:01:09Z","kind":"Am 30","total_abstain":0,"total_against":0,"total_for":2,"url":"http://testserver/api/proposals/6/?format=json"}]
[
{
"dossier": "http://testserver/api/dossiers/1/",
"title": "A7-0234/2012 - Charles Goerens - § 31",
"description": "",
"reference": "A7-0234/2012",
"datetime": "2012-10-23T16:31:10Z",
"kind": "§ 31",
"total_abstain": 2,
"total_against": 0,
"total_for": 0,
"url": "http://testserver/api/proposals/1/"
},
{
"dossier": "http://testserver/api/dossiers/1/",
"title": "A7-0234/2012 - Charles Goerens - Résolution",
"description": "",
"reference": "A7-0234/2012",
"datetime": "2012-10-23T16:34:32Z",
"kind": "Résolution",
"total_abstain": 0,
"total_against": 0,
"total_for": 2,
"url": "http://testserver/api/proposals/2/"
},
{
"dossier": "http://testserver/api/dossiers/2/",
"title": "A8-0298/2015 - José Manuel Fernandes et Gérard Deprez - Am 4",
"description": "",
"reference": "A8-0298/2015",
"datetime": "2015-10-28T11:59:35Z",
"kind": "Am 4",
"total_abstain": 0,
"total_against": 2,
"total_for": 0,
"url": "http://testserver/api/proposals/3/"
},
{
"dossier": "http://testserver/api/dossiers/2/",
"title": "A8-0298/2015 - José Manuel Fernandes et Gérard Deprez - Am 29",
"description": "",
"reference": "A8-0298/2015",
"datetime": "2015-10-28T12:00:12Z",
"kind": "Am 29",
"total_abstain": 0,
"total_against": 1,
"total_for": 1,
"url": "http://testserver/api/proposals/4/"
},
{
"dossier": "http://testserver/api/dossiers/2/",
"title": "A8-0298/2015 - José Manuel Fernandes et Gérard Deprez - Am 31",
"description": "",
"reference": "A8-0298/2015",
"datetime": "2015-10-28T12:00:42Z",
"kind": "Am 31",
"total_abstain": 1,
"total_against": 1,
"total_for": 0,
"url": "http://testserver/api/proposals/5/"
},
{
"dossier": "http://testserver/api/dossiers/2/",
"title": "A8-0298/2015 - José Manuel Fernandes et Gérard Deprez - Am 30",
"description": "",
"reference": "A8-0298/2015",
"datetime": "2015-10-28T12:01:09Z",
"kind": "Am 30",
"total_abstain": 0,
"total_against": 0,
"total_for": 2,
"url": "http://testserver/api/proposals/6/"
}
]
Original file line number Diff line number Diff line change
@@ -1 +1,68 @@
[{"id":1,"proposal":"6c1fb67feac3ab2ea567b37aa7dfd0d578bc04b4","representative":"314d0f4c25af31bfa2a6b286838367994b902615","representative_name":"","position":"abstain"},{"id":2,"proposal":"6c1fb67feac3ab2ea567b37aa7dfd0d578bc04b4","representative":"2a3c90346d40e9c540050534d832ceb3e0d25a49","representative_name":"","position":"abstain"},{"id":3,"proposal":"18df8a97581832a95f3bdfb8c14ba2b05abb91e9","representative":"314d0f4c25af31bfa2a6b286838367994b902615","representative_name":"","position":"for"},{"id":4,"proposal":"18df8a97581832a95f3bdfb8c14ba2b05abb91e9","representative":"2a3c90346d40e9c540050534d832ceb3e0d25a49","representative_name":"","position":"for"},{"id":5,"proposal":"4e575a1bc17602d23d2f0acf8ee482ebe08de79c","representative":"314d0f4c25af31bfa2a6b286838367994b902615","representative_name":"","position":"against"},{"id":6,"proposal":"4e575a1bc17602d23d2f0acf8ee482ebe08de79c","representative":"2a3c90346d40e9c540050534d832ceb3e0d25a49","representative_name":"","position":"against"},{"id":7,"proposal":"fc786534e832d9af7e2cdcd0b0952d7d60bb4331","representative":"2a3c90346d40e9c540050534d832ceb3e0d25a49","representative_name":"","position":"for"},{"id":8,"proposal":"fc786534e832d9af7e2cdcd0b0952d7d60bb4331","representative":"314d0f4c25af31bfa2a6b286838367994b902615","representative_name":"","position":"against"},{"id":9,"proposal":"90c022cf70dc6ceff796b876d3e1b814620c8637","representative":"2a3c90346d40e9c540050534d832ceb3e0d25a49","representative_name":"","position":"abstain"},{"id":10,"proposal":"90c022cf70dc6ceff796b876d3e1b814620c8637","representative":"314d0f4c25af31bfa2a6b286838367994b902615","representative_name":"","position":"against"},{"id":11,"proposal":"5b3aa46182803c11d70ccfec666234b9fe2f44a7","representative":"2a3c90346d40e9c540050534d832ceb3e0d25a49","representative_name":"","position":"for"}]
[
{
"proposal": "http://testserver/api/proposals/1/",
"representative": "http://testserver/api/representatives/2/",
"representative_name": "",
"position": "abstain"
},
{
"proposal": "http://testserver/api/proposals/1/",
"representative": "http://testserver/api/representatives/1/",
"representative_name": "",
"position": "abstain"
},
{
"proposal": "http://testserver/api/proposals/2/",
"representative": "http://testserver/api/representatives/2/",
"representative_name": "",
"position": "for"
},
{
"proposal": "http://testserver/api/proposals/2/",
"representative": "http://testserver/api/representatives/1/",
"representative_name": "",
"position": "for"
},
{
"proposal": "http://testserver/api/proposals/3/",
"representative": "http://testserver/api/representatives/2/",
"representative_name": "",
"position": "against"
},
{
"proposal": "http://testserver/api/proposals/3/",
"representative": "http://testserver/api/representatives/1/",
"representative_name": "",
"position": "against"
},
{
"proposal": "http://testserver/api/proposals/4/",
"representative": "http://testserver/api/representatives/1/",
"representative_name": "",
"position": "for"
},
{
"proposal": "http://testserver/api/proposals/4/",
"representative": "http://testserver/api/representatives/2/",
"representative_name": "",
"position": "against"
},
{
"proposal": "http://testserver/api/proposals/5/",
"representative": "http://testserver/api/representatives/1/",
"representative_name": "",
"position": "abstain"
},
{
"proposal": "http://testserver/api/proposals/5/",
"representative": "http://testserver/api/representatives/2/",
"representative_name": "",
"position": "against"
},
{
"proposal": "http://testserver/api/proposals/6/",
"representative": "http://testserver/api/representatives/1/",
"representative_name": "",
"position": "for"
}
]
11 changes: 7 additions & 4 deletions representatives_votes/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ class RepresentativeManagerTest(test.TestCase):

def functional_test(self, queries, url):
with self.assertNumQueries(queries):
result = test.client.Client().get(url)
result = test.client.Client().get(
url,
HTTP_ACCEPT='application/json; indent=4',
)
Response.for_test(self).assertNoDiff(result)

def test_dossiers(self):
self.functional_test(1, '/api/dossiers/?format=json')
self.functional_test(1, '/api/dossiers/')

def test_proposals(self):
self.functional_test(1, '/api/proposals/?format=json')
self.functional_test(1, '/api/proposals/')

def test_votes(self):
self.functional_test(1, '/api/votes/?format=json')
self.functional_test(1, '/api/votes/')
11 changes: 11 additions & 0 deletions representatives_votes/tests/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,18 @@
VoteViewSet,
)

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)
router.register(r'dossiers', DossierViewSet)
router.register(r'proposals', ProposalViewSet)
router.register(r'votes', VoteViewSet)
Expand Down

0 comments on commit a8b4474

Please sign in to comment.