campus.py 1.69 KB
Newer Older
Florent Chehab's avatar
Florent Chehab committed
1
from django.db import models
2
3
4
from backend.models.module import BasicModule, BasicModuleSerializer
from backend.models.location import City
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 rest_framework import viewsets, permissions
Florent Chehab's avatar
Florent Chehab committed
7
8


Florent Chehab's avatar
Florent Chehab committed
9
class Campus(BasicModule):
10
11
12
13
14
    module_icon = "You forget to override the icon in the sub-class : I know"
    module_name = "Campus principal"
    module_description = "Campus de l'université."

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

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

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

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(viewsets.ModelViewSet):  # TODO switch to dict
Florent Chehab's avatar
Florent Chehab committed
50
    permission_classes = (permissions.DjangoModelPermissions,)
51
52
    queryset = Campus.objects.all()  # pylint: disable=E1101
    serializer_class = CampusSerializer