campus.py 1.93 KB
Newer Older
Florent Chehab's avatar
Florent Chehab committed
1
from django.db import models
2
from backend.models.abstract.my_model import MyModelVersionned, MyModelVersionnedSerializer, MyModelVersionnedViewSet
3
from backend.models.city import City
4
from backend.models.university import University
Florent Chehab's avatar
Florent Chehab committed
5
from django.core.validators import MinValueValidator, MaxValueValidator
Florent Chehab's avatar
Florent Chehab committed
6
from backend.utils import get_model_config, get_viewset_permissions
Florent Chehab's avatar
Florent Chehab committed
7 8


9
class Campus(MyModelVersionned):
Florent Chehab's avatar
Florent Chehab committed
10
    model_config = get_model_config("Campus")
11 12

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

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

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

36 37 38 39
    @classmethod
    def get_serializer(cls):
        return CampusSerializer

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


44
class CampusSerializer(MyModelVersionnedSerializer):
Florent Chehab's avatar
Florent Chehab committed
45 46

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


51
class CampusViewSet(MyModelVersionnedViewSet):
52
    permission_classes = get_viewset_permissions("CampusViewSet")
53
    queryset = Campus.objects.all()  # pylint: disable=E1101
54
    serializer_class = CampusSerializer
55 56


57
class MainCampusViewSet(MyModelVersionnedViewSet):
58
    permission_classes = get_viewset_permissions("MainCampusViewSet")
59 60
    queryset = Campus.objects.filter(is_main_campus=True)
    serializer_class = CampusSerializer