campus.py 1.89 KB
Newer Older
Florent Chehab's avatar
Florent Chehab committed
1
2
from django.db import models
from rex.models.module import Module
3
from rex.models.location import City
Florent Chehab's avatar
Florent Chehab committed
4
from django.core.validators import MinValueValidator, MaxValueValidator
Florent Chehab's avatar
Florent Chehab committed
5
from rest_framework import serializers, viewsets, permissions
Florent Chehab's avatar
Florent Chehab committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26


class Campus(Module):
    name = models.CharField(max_length=200, null=True)
    comment = models.TextField(null=True, blank=True)
    city = models.ForeignKey(City, on_delete=models.PROTECT, null=False)

    lat = models.DecimalField(
        max_digits=10,
        decimal_places=6,
        validators=[MinValueValidator(-85.05112878),
                    MaxValueValidator(85.05112878)]
    )

    lon = models.DecimalField(
        max_digits=10,
        decimal_places=6,
        validators=[MinValueValidator(-180),
                    MaxValueValidator(180)]
    )

27
    def location(self):
Florent Chehab's avatar
Florent Chehab committed
28
29
        return {'lat': self.lat, 'lon': self.lon}

Florent Chehab's avatar
Florent Chehab committed
30
31
32
33
34
35
36
37
    class Meta:
        abstract = True


class MainCampus(Campus):
    module_icon = "You forget to override the icon in the sub-class : I know"
    module_name = "Campus principal"
    module_description = "Campus principal de l'université."
Florent Chehab's avatar
Florent Chehab committed
38
39
40
41
42
43
44
45


class MainCampusSerializer(serializers.ModelSerializer):
    gps_coordinates = serializers.SerializerMethodField()
    modified_by = serializers.SerializerMethodField()
    modified_date = serializers.SerializerMethodField()

    def get_gps_coordinates(self, obj):
46
        return obj.location()
Florent Chehab's avatar
Florent Chehab committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60

    def get_modified_by(self, obj):
        return obj.modified_by()

    def get_modified_date(self, obj):
        return obj.modified_date()

    class Meta:
        model = City
        fields = ('gps_coordinates', 'modified_by', 'modified_date', 'id')


class MainCampusViewSet(viewsets.ModelViewSet):  # TODO switch to dict
    permission_classes = (permissions.DjangoModelPermissions,)
Florent Chehab's avatar
Florent Chehab committed
61
    queryset = MainCampus.objects.all()  # pylint: disable=E1101
Florent Chehab's avatar
Florent Chehab committed
62
    serializer_class = MainCampusSerializer