country.py 1.62 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
25
26
27


class CountrySerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Country
Florent Chehab's avatar
Florent Chehab committed
28
        fields = ('name', 'iso_alpha3_code', 'region', 'url')
Florent Chehab's avatar
Florent Chehab committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44


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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66


class RegionSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Region
        fields = ('name', 'un_code', 'parent', 'url')


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