campus.py 1.63 KB
Newer Older
Florent Chehab's avatar
Florent Chehab committed
1
from django.db import models
2
from backend.models.module import BasicModule, BasicModuleSerializer, BasicModuleViewSet
3
4
from backend.models.location import City
from backend.models.university import University
Florent Chehab's avatar
Florent Chehab committed
5
6
7
from django.core.validators import MinValueValidator, MaxValueValidator


Florent Chehab's avatar
Florent Chehab committed
8
class Campus(BasicModule):
9
10

    is_main_campus = models.BooleanField(null=False)
Florent Chehab's avatar
Florent Chehab committed
11
12
13
    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)
14
    university = models.ForeignKey(
15
        University, on_delete=models.PROTECT, null=False, related_name="univ_campus")
Florent Chehab's avatar
Florent Chehab committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

    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)]
    )

31
    def location(self):
Florent Chehab's avatar
Florent Chehab committed
32
33
        return {'lat': self.lat, 'lon': self.lon}

34
35
36
37
    @classmethod
    def get_serializer(cls):
        return CampusSerializer

Florent Chehab's avatar
Florent Chehab committed
38
    class Meta:
39
        unique_together = ('is_main_campus', 'university')
Florent Chehab's avatar
Florent Chehab committed
40
41


Florent Chehab's avatar
Florent Chehab committed
42
class CampusSerializer(BasicModuleSerializer):
Florent Chehab's avatar
Florent Chehab committed
43
44

    class Meta:
45
        model = Campus
Florent Chehab's avatar
Florent Chehab committed
46
        fields = '__all__'
Florent Chehab's avatar
Florent Chehab committed
47
48


49
class CampusViewSet(BasicModuleViewSet):
50
    queryset = Campus.objects.all()  # pylint: disable=E1101
51
    serializer_class = CampusSerializer
52
53
54
55
56


class MainCampusViewSet(BasicModuleViewSet):
    queryset = Campus.objects.filter(is_main_campus=True)
    serializer_class = CampusSerializer