Skip to content

Commit a8b4474

Browse files
committed
Fixed the API
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.
1 parent 1274e0d commit a8b4474

File tree

7 files changed

+185
-18
lines changed

7 files changed

+185
-18
lines changed

representatives_votes/api.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class DossierViewSet(viewsets.ReadOnlyModelViewSet):
2626
"""
2727

2828
pagination_class = DefaultWebPagination
29-
queryset = Dossier.objects.prefetch_related('proposals__votes__representative')
29+
queryset = Dossier.objects.all()
3030
serializer_class = DossierSerializer
3131

3232
filter_backends = (
@@ -58,7 +58,7 @@ class ProposalViewSet(viewsets.ReadOnlyModelViewSet):
5858
"""
5959

6060
pagination_class = DefaultWebPagination
61-
queryset = Proposal.objects.select_related('dossier')
61+
queryset = Proposal.objects.all()
6262
serializer_class = ProposalSerializer
6363

6464
filter_backends = (
@@ -85,10 +85,7 @@ class ProposalViewSet(viewsets.ReadOnlyModelViewSet):
8585
'dossier__reference'
8686
)
8787

88-
ordering_fields = ('id', 'reference')
89-
90-
def list(self, request):
91-
return super(ProposalViewSet, self).list(request)
88+
ordering_fields = ('reference')
9289

9390
def retrieve(self, request, pk=None):
9491
self.serializer_class = ProposalDetailSerializer

representatives_votes/serializers.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ class VoteSerializer(serializers.HyperlinkedModelSerializer):
1212
class Meta:
1313
model = models.Vote
1414
fields = (
15-
'id',
1615
'proposal',
1716
'representative',
1817
'representative_name',
@@ -24,7 +23,6 @@ class ProposalSerializer(serializers.HyperlinkedModelSerializer):
2423
class Meta:
2524
model = models.Proposal
2625
fields = (
27-
'id',
2826
'dossier',
2927
'title',
3028
'description',
@@ -41,26 +39,29 @@ class Meta:
4139
class ProposalDetailSerializer(ProposalSerializer):
4240
""" Proposal serializer that includes votes """
4341

42+
votes = VoteSerializer(many=True)
43+
44+
class Meta:
45+
model = models.Proposal
46+
4447

4548
class DossierSerializer(serializers.HyperlinkedModelSerializer):
4649
""" Base dossier serializer """
47-
proposals = ProposalSerializer(many=True)
4850

4951
class Meta:
5052
model = models.Dossier
5153
fields = (
52-
'id',
5354
'title',
5455
'reference',
5556
'text',
5657
'link',
5758
'url',
58-
'proposals',
5959
)
6060

6161

6262
class DossierDetailSerializer(DossierSerializer):
6363
"""
6464
Dossier serializer that includes proposals and votes.
6565
"""
66+
6667
proposals = ProposalDetailSerializer(many=True)
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
1-
[{"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"}]
1+
[
2+
{
3+
"title": "Agenda for change: the future of EU development policy",
4+
"reference": "2012/2002(INI)",
5+
"text": "",
6+
"link": "http://www.europarl.europa.eu/oeil/popups/ficheprocedure.do?reference=2012/2002(INI)",
7+
"url": "http://testserver/api/dossiers/1/"
8+
},
9+
{
10+
"title": "2016 general budget: all sections",
11+
"reference": "2015/2132(BUD)",
12+
"text": "",
13+
"link": "http://www.europarl.europa.eu/oeil/popups/ficheprocedure.do?reference=2015/2132(BUD)",
14+
"url": "http://testserver/api/dossiers/2/"
15+
}
16+
]
Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,74 @@
1-
[{"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"}]
1+
[
2+
{
3+
"dossier": "http://testserver/api/dossiers/1/",
4+
"title": "A7-0234/2012 - Charles Goerens - § 31",
5+
"description": "",
6+
"reference": "A7-0234/2012",
7+
"datetime": "2012-10-23T16:31:10Z",
8+
"kind": "§ 31",
9+
"total_abstain": 2,
10+
"total_against": 0,
11+
"total_for": 0,
12+
"url": "http://testserver/api/proposals/1/"
13+
},
14+
{
15+
"dossier": "http://testserver/api/dossiers/1/",
16+
"title": "A7-0234/2012 - Charles Goerens - Résolution",
17+
"description": "",
18+
"reference": "A7-0234/2012",
19+
"datetime": "2012-10-23T16:34:32Z",
20+
"kind": "Résolution",
21+
"total_abstain": 0,
22+
"total_against": 0,
23+
"total_for": 2,
24+
"url": "http://testserver/api/proposals/2/"
25+
},
26+
{
27+
"dossier": "http://testserver/api/dossiers/2/",
28+
"title": "A8-0298/2015 - José Manuel Fernandes et Gérard Deprez - Am 4",
29+
"description": "",
30+
"reference": "A8-0298/2015",
31+
"datetime": "2015-10-28T11:59:35Z",
32+
"kind": "Am 4",
33+
"total_abstain": 0,
34+
"total_against": 2,
35+
"total_for": 0,
36+
"url": "http://testserver/api/proposals/3/"
37+
},
38+
{
39+
"dossier": "http://testserver/api/dossiers/2/",
40+
"title": "A8-0298/2015 - José Manuel Fernandes et Gérard Deprez - Am 29",
41+
"description": "",
42+
"reference": "A8-0298/2015",
43+
"datetime": "2015-10-28T12:00:12Z",
44+
"kind": "Am 29",
45+
"total_abstain": 0,
46+
"total_against": 1,
47+
"total_for": 1,
48+
"url": "http://testserver/api/proposals/4/"
49+
},
50+
{
51+
"dossier": "http://testserver/api/dossiers/2/",
52+
"title": "A8-0298/2015 - José Manuel Fernandes et Gérard Deprez - Am 31",
53+
"description": "",
54+
"reference": "A8-0298/2015",
55+
"datetime": "2015-10-28T12:00:42Z",
56+
"kind": "Am 31",
57+
"total_abstain": 1,
58+
"total_against": 1,
59+
"total_for": 0,
60+
"url": "http://testserver/api/proposals/5/"
61+
},
62+
{
63+
"dossier": "http://testserver/api/dossiers/2/",
64+
"title": "A8-0298/2015 - José Manuel Fernandes et Gérard Deprez - Am 30",
65+
"description": "",
66+
"reference": "A8-0298/2015",
67+
"datetime": "2015-10-28T12:01:09Z",
68+
"kind": "Am 30",
69+
"total_abstain": 0,
70+
"total_against": 0,
71+
"total_for": 2,
72+
"url": "http://testserver/api/proposals/6/"
73+
}
74+
]
Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,68 @@
1-
[{"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"}]
1+
[
2+
{
3+
"proposal": "http://testserver/api/proposals/1/",
4+
"representative": "http://testserver/api/representatives/2/",
5+
"representative_name": "",
6+
"position": "abstain"
7+
},
8+
{
9+
"proposal": "http://testserver/api/proposals/1/",
10+
"representative": "http://testserver/api/representatives/1/",
11+
"representative_name": "",
12+
"position": "abstain"
13+
},
14+
{
15+
"proposal": "http://testserver/api/proposals/2/",
16+
"representative": "http://testserver/api/representatives/2/",
17+
"representative_name": "",
18+
"position": "for"
19+
},
20+
{
21+
"proposal": "http://testserver/api/proposals/2/",
22+
"representative": "http://testserver/api/representatives/1/",
23+
"representative_name": "",
24+
"position": "for"
25+
},
26+
{
27+
"proposal": "http://testserver/api/proposals/3/",
28+
"representative": "http://testserver/api/representatives/2/",
29+
"representative_name": "",
30+
"position": "against"
31+
},
32+
{
33+
"proposal": "http://testserver/api/proposals/3/",
34+
"representative": "http://testserver/api/representatives/1/",
35+
"representative_name": "",
36+
"position": "against"
37+
},
38+
{
39+
"proposal": "http://testserver/api/proposals/4/",
40+
"representative": "http://testserver/api/representatives/1/",
41+
"representative_name": "",
42+
"position": "for"
43+
},
44+
{
45+
"proposal": "http://testserver/api/proposals/4/",
46+
"representative": "http://testserver/api/representatives/2/",
47+
"representative_name": "",
48+
"position": "against"
49+
},
50+
{
51+
"proposal": "http://testserver/api/proposals/5/",
52+
"representative": "http://testserver/api/representatives/1/",
53+
"representative_name": "",
54+
"position": "abstain"
55+
},
56+
{
57+
"proposal": "http://testserver/api/proposals/5/",
58+
"representative": "http://testserver/api/representatives/2/",
59+
"representative_name": "",
60+
"position": "against"
61+
},
62+
{
63+
"proposal": "http://testserver/api/proposals/6/",
64+
"representative": "http://testserver/api/representatives/1/",
65+
"representative_name": "",
66+
"position": "for"
67+
}
68+
]

representatives_votes/tests/test_views.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@ class RepresentativeManagerTest(test.TestCase):
88

99
def functional_test(self, queries, url):
1010
with self.assertNumQueries(queries):
11-
result = test.client.Client().get(url)
11+
result = test.client.Client().get(
12+
url,
13+
HTTP_ACCEPT='application/json; indent=4',
14+
)
1215
Response.for_test(self).assertNoDiff(result)
1316

1417
def test_dossiers(self):
15-
self.functional_test(1, '/api/dossiers/?format=json')
18+
self.functional_test(1, '/api/dossiers/')
1619

1720
def test_proposals(self):
18-
self.functional_test(1, '/api/proposals/?format=json')
21+
self.functional_test(1, '/api/proposals/')
1922

2023
def test_votes(self):
21-
self.functional_test(1, '/api/votes/?format=json')
24+
self.functional_test(1, '/api/votes/')

representatives_votes/tests/urls.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,18 @@
88
VoteViewSet,
99
)
1010

11+
from representatives.api import (
12+
ConstituencyViewSet,
13+
GroupViewSet,
14+
MandateViewSet,
15+
RepresentativeViewSet,
16+
)
17+
1118
router = routers.DefaultRouter()
19+
router.register(r'constituencies', ConstituencyViewSet)
20+
router.register(r'groups', GroupViewSet)
21+
router.register(r'mandates', MandateViewSet)
22+
router.register(r'representatives', RepresentativeViewSet)
1223
router.register(r'dossiers', DossierViewSet)
1324
router.register(r'proposals', ProposalViewSet)
1425
router.register(r'votes', VoteViewSet)

0 commit comments

Comments
 (0)