-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathserializers.py
More file actions
44 lines (37 loc) · 1.2 KB
/
serializers.py
File metadata and controls
44 lines (37 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from map.models import CommunityArea, RestaurantPermit
class CommunityAreaSerializer(serializers.ModelSerializer):
class Meta:
model = CommunityArea
fields = []
def to_representation(self, obj: CommunityArea):
"""
Supplement each community area object with the number
of permits issued in the given year.
e.g. The endpoint /map-data/?year=2017 should return something like:
[
{
"ROGERS PARK": {
area_id: 17,
num_permits: 2
},
"BEVERLY": {
area_id: 72,
num_permits: 2
},
...
}
]
"""
year = self.context.get("year")
if year:
query = RestaurantPermit.objects.filter(
community_area_id=obj.area_id
)
return {obj.name : {
'area_id' : obj.area_id,
'num_permits' : query.count()
}}
else:
raise(ValidationError('Year not specified.'))