location.py 3.01 KB
Newer Older
1
from django.db import models
Florent Chehab's avatar
Florent Chehab committed
2
from rest_framework import serializers, viewsets, permissions
3

Florent Chehab's avatar
Florent Chehab committed
4
5
6
# Data model based on : https://unstats.un.org/unsd/methodology/m49/overview/


7
class Country(models.Model):
8
    name = models.CharField(max_length=200)
9
    iso_alpha2_code = models.CharField(primary_key=True, max_length=2)
10
11
12
13
14
15
16
    iso_alpha3_code = models.CharField(unique=True, max_length=3, blank=False, null=False)
    region_name = models.CharField(max_length=200)
    region_un_code = models.CharField(max_length=3)
    sub_region_name = models.CharField(max_length=200, null=True, blank=True)
    sub_region_un_code = models.CharField(max_length=3, null=True, blank=True)
    intermediate_region_name = models.CharField(max_length=200, null=True, blank=True)
    intermediate_region_un_code = models.CharField(max_length=3, null=True, blank=True)
17

Florent Chehab's avatar
Florent Chehab committed
18

Florent Chehab's avatar
Florent Chehab committed
19
20
class City(models.Model):
    name = models.CharField(max_length=200)
Florent Chehab's avatar
Florent Chehab committed
21
    local_name = models.CharField(max_length=200, null=True, blank=True)
Florent Chehab's avatar
Florent Chehab committed
22
23
    # We add an area to distinguish similarly named cities
    # in a country
Florent Chehab's avatar
Florent Chehab committed
24
    area = models.CharField(max_length=200, null=True, blank=True)
Florent Chehab's avatar
Florent Chehab committed
25
26
27
    country = models.ForeignKey(Country, on_delete=models.PROTECT)


Florent Chehab's avatar
Florent Chehab committed
28
29
30
"""
API RELATED STUFF BELLOW
"""
Florent Chehab's avatar
Florent Chehab committed
31

32
# TODO move somewhere else
Florent Chehab's avatar
Florent Chehab committed
33
34


Florent Chehab's avatar
Florent Chehab committed
35
36
37
38
39
40
41
class DictModeViewSet(viewsets.ModelViewSet):
    """
    ViewSet that renders data as dict with keys corresponding to the model
    primary key. Instead of list.
    """

    def list(self, request, *args, **kwargs):
Florent Chehab's avatar
Florent Chehab committed
42
        response = super(viewsets.ModelViewSet, self).list(  # pylint: disable=E1003
Florent Chehab's avatar
Florent Chehab committed
43
44
45
46
47
48
49
50
            request, *args, **kwargs)  # call the original 'list'
        pk_attr_name = self.serializer_class.Meta.model._meta.pk.name
        response.data = {
            d[pk_attr_name]: d for d in response.data
        }
        return response


51
class CountrySerializer(serializers.ModelSerializer):
52

Florent Chehab's avatar
Florent Chehab committed
53
    class Meta:
54
55
56
57
58
        model = Country
        fields = ('name', 'iso_alpha2_code', 'iso_alpha3_code',
                  'region_name', 'region_un_code',
                  'sub_region_name', 'sub_region_un_code',
                  'intermediate_region_name', 'intermediate_region_un_code')
Florent Chehab's avatar
Florent Chehab committed
59
60


61
class CountryViewSet(DictModeViewSet):
Florent Chehab's avatar
Florent Chehab committed
62
    permission_classes = (permissions.DjangoModelPermissions,)
63
64
    queryset = Country.objects.all()  # pylint: disable=E1101
    serializer_class = CountrySerializer
Florent Chehab's avatar
Florent Chehab committed
65
66
67
68
69
70
71
72
73


class CitySerializer(serializers.ModelSerializer):
    country_url = serializers.SerializerMethodField()

    def get_country_url(self, obj):
        return serializers.HyperlinkedRelatedField(
            view_name='country-detail',
            read_only=True) \
Florent Chehab's avatar
Florent Chehab committed
74
            .get_url(obj.country, view_name='country-detail',
Florent Chehab's avatar
Florent Chehab committed
75
76
77
78
                     request=self.context['request'], format=None)

    class Meta:
        model = City
Florent Chehab's avatar
Florent Chehab committed
79
        fields = ('name', 'local_name', 'area', 'country', 'country_url', 'id')
Florent Chehab's avatar
Florent Chehab committed
80
81


Florent Chehab's avatar
Florent Chehab committed
82
class CityViewSet(DictModeViewSet):
Florent Chehab's avatar
Florent Chehab committed
83
    permission_classes = (permissions.DjangoModelPermissions,)
Florent Chehab's avatar
Florent Chehab committed
84
    queryset = City.objects.all()  # pylint: disable=E1101
Florent Chehab's avatar
Florent Chehab committed
85
    serializer_class = CitySerializer