campus.py 1.66 KB
Newer Older
1
from django.core.validators import MinValueValidator, MaxValueValidator
Florent Chehab's avatar
Florent Chehab committed
2
from django.db import models
3

4
from backend_app.models.abstract.module import Module, ModuleSerializer, ModuleViewSet
Florent Chehab's avatar
Florent Chehab committed
5 6
from backend_app.models.city import City
from backend_app.models.university import University
7
from backend_app.permissions.app_permissions import ReadOnly
Florent Chehab's avatar
Florent Chehab committed
8 9


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

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

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

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

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


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


46
class CampusViewSet(ModuleViewSet):
47
    queryset = Campus.objects.all()  # pylint: disable=E1101
48
    serializer_class = CampusSerializer
49
    end_point_route = "campuses"
50 51


52
class MainCampusViewSet(ModuleViewSet):
53 54
    queryset = Campus.objects.filter(is_main_campus=True)
    serializer_class = CampusSerializer
55 56
    permission_classes = (ReadOnly,)
    end_point_route = "mainCampuses"