country.py 2.93 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
7
8
9
10
11
12
# Data model based on : https://unstats.un.org/unsd/methodology/m49/overview/


class Region(models.Model):
    name = models.CharField(max_length=200)
    un_code = models.CharField(primary_key=True, max_length=3)
    parent = models.ForeignKey(
        'self', on_delete=models.SET_NULL, null=True)

Florent Chehab's avatar
Florent Chehab committed
13

14
class Country(models.Model):
15
    name = models.CharField(max_length=200)
Florent Chehab's avatar
Florent Chehab committed
16
17
18
19
20
21
22
    iso_alpha3_code = models.CharField(primary_key=True, max_length=3)
    region = models.ForeignKey(Region, on_delete=models.PROTECT, null=True)


"""
API RELATED STUFF BELLOW
"""
Florent Chehab's avatar
Florent Chehab committed
23
24


Florent Chehab's avatar
Florent Chehab committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def get_regions(country):
    regions = [None] * 3
    regions.append(country.region)
    while regions[-1] is not None and regions[-1].parent is not None:
        regions.append(regions[-1].parent)
    regions = regions[::-1]
    return regions[0:3]


class CountrySerializer(serializers.ModelSerializer):
    regions = serializers.SerializerMethodField()
    regions_url = serializers.SerializerMethodField()

    def get_regions(self, obj):
        return [r.un_code if r is not None else None
                for r in get_regions(obj)]

    def get_regions_url(self, obj):
        return [self.get_region_url(r) for r in get_regions(obj)]

    def get_region_url(self, region):
        if region is None:
            return None

        return serializers.HyperlinkedRelatedField(
            view_name='country-detail',
            read_only=True) \
            .get_url(region, view_name='region-detail',
                     request=self.context['request'], format=None)

Florent Chehab's avatar
Florent Chehab committed
55
56
    class Meta:
        model = Country
Florent Chehab's avatar
Florent Chehab committed
57
58
        fields = ('name', 'iso_alpha3_code',
                  'region', 'regions', 'regions_url')
Florent Chehab's avatar
Florent Chehab committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74


class CountryViewSet(viewsets.ModelViewSet):
    """
    retrieve:
    Retourne un pays.

    list:
    Retourne une liste de pays.

    create:
    Créée un nouveau pays.
    """
    permission_classes = (permissions.DjangoModelPermissions,)
    queryset = Country.objects.all()  # noqa: E1101
    serializer_class = CountrySerializer
Florent Chehab's avatar
Florent Chehab committed
75
76


Florent Chehab's avatar
Florent Chehab committed
77
78
79
80
81
82
83
84
85
86
87
88
89
class RegionSerializer(serializers.ModelSerializer):
    parent_url = serializers.SerializerMethodField()

    def get_parent_url(self, obj):
        if obj.parent is None:
            return None

        return serializers.HyperlinkedRelatedField(
            view_name='country-detail',
            read_only=True) \
            .get_url(obj.parent, view_name='region-detail',
                     request=self.context['request'], format=None)

Florent Chehab's avatar
Florent Chehab committed
90
91
    class Meta:
        model = Region
Florent Chehab's avatar
Florent Chehab committed
92
        fields = ('name', 'un_code', 'parent', 'parent_url')
Florent Chehab's avatar
Florent Chehab committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108


class RegionViewSet(viewsets.ModelViewSet):
    """
    retrieve:
    Retourne un pays.

    list:
    Retourne une liste de pays.

    create:
    Créée un nouveau pays.
    """
    permission_classes = (permissions.DjangoModelPermissions,)
    queryset = Region.objects.all()  # noqa: E1101
    serializer_class = RegionSerializer