campus.py 1.5 KB
Newer Older
Florent Chehab's avatar
Florent Chehab committed
1
from django.db import models
2
from backend_app.models.abstract.module import Module, ModuleSerializer, ModuleViewSet
Florent Chehab's avatar
Florent Chehab committed
3
4
from backend_app.models.city import City
from backend_app.models.university import University
Florent Chehab's avatar
Florent Chehab committed
5
6
7
from django.core.validators import MinValueValidator, MaxValueValidator


8
class Campus(Module):
9
10

    is_main_campus = models.BooleanField(null=False)
11
    name = models.CharField(max_length=200, default="", blank=True)
Florent Chehab's avatar
Florent Chehab committed
12
    city = models.ForeignKey(City, on_delete=models.PROTECT, null=False)
13
    university = models.ForeignKey(
14
15
16
17
18
        University,
        on_delete=models.PROTECT,
        null=False,
        related_name="university_campuses",
    )
Florent Chehab's avatar
Florent Chehab committed
19
20
21
22

    lat = models.DecimalField(
        max_digits=10,
        decimal_places=6,
23
        validators=[MinValueValidator(-85.05112878), MaxValueValidator(85.05112878)],
Florent Chehab's avatar
Florent Chehab committed
24
25
26
27
28
    )

    lon = models.DecimalField(
        max_digits=10,
        decimal_places=6,
29
        validators=[MinValueValidator(-180), MaxValueValidator(180)],
Florent Chehab's avatar
Florent Chehab committed
30
31
    )

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

Florent Chehab's avatar
Florent Chehab committed
35
    class Meta:
36
        unique_together = ("is_main_campus", "university")
Florent Chehab's avatar
Florent Chehab committed
37
38


39
class CampusSerializer(ModuleSerializer):
Florent Chehab's avatar
Florent Chehab committed
40
    class Meta:
41
        model = Campus
42
        fields = "__all__"
Florent Chehab's avatar
Florent Chehab committed
43
44


45
class CampusViewSet(ModuleViewSet):
46
    queryset = Campus.objects.all()  # pylint: disable=E1101
47
    serializer_class = CampusSerializer
48
49


50
class MainCampusViewSet(ModuleViewSet):
51
52
    queryset = Campus.objects.filter(is_main_campus=True)
    serializer_class = CampusSerializer