campus.py 1.87 KB
Newer Older
Florent Chehab's avatar
Florent Chehab committed
1
from django.db import models
Florent Chehab's avatar
Florent Chehab committed
2
3
4
from backend_app.models.abstract.basic_module import BasicModule, BasicModuleSerializer, BasicModuleViewSet
from backend_app.models.city import City
from backend_app.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_app.utils import get_model_config, get_viewset_permissions
Florent Chehab's avatar
Florent Chehab committed
7
8


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

    is_main_campus = models.BooleanField(null=False)
13
    name = models.CharField(max_length=200, default='', blank=True)
Florent Chehab's avatar
Florent Chehab committed
14
    city = models.ForeignKey(City, on_delete=models.PROTECT, null=False)
15
    university = models.ForeignKey(
16
        University, on_delete=models.PROTECT, null=False, related_name="university_campuses")
Florent Chehab's avatar
Florent Chehab committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

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

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

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

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


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

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


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


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