universitySemestersDates.py 2.12 KB
Newer Older
Florent Chehab's avatar
Florent Chehab committed
1
from django.db import models
2
from backend.models.module import BasicModule, BasicModuleSerializer, BasicModuleViewSet
3
from backend.models.university import University
4
from rest_framework import serializers
Florent Chehab's avatar
Florent Chehab committed
5
from backend.utils import get_model_config, get_viewset_permissions
Florent Chehab's avatar
Florent Chehab committed
6
7


Florent Chehab's avatar
Florent Chehab committed
8
9
10
def semester_error(semester_name):
    raise serializers.ValidationError(
        "Inconsistent {} semester dates".format(semester_name))
Florent Chehab's avatar
Florent Chehab committed
11
12


13
class UniversitySemestersDates(BasicModule):
Florent Chehab's avatar
Florent Chehab committed
14
    model_config = get_model_config("UniversitySemestersDates")
Florent Chehab's avatar
Florent Chehab committed
15
16
17
18
19
20
21
22
23

    university = models.OneToOneField(
        University, on_delete=models.CASCADE, related_name='university_semesters_dates', primary_key=True, null=False)

    spring_begin = models.DateField(null=True, blank=True)
    spring_end = models.DateField(null=True, blank=True)
    autumn_begin = models.DateField(null=True, blank=True)
    autumn_end = models.DateField(null=True, blank=True)

24
25
26
    @classmethod
    def get_serializer(cls):
        return UniversitySemestersDatesSerializer
Florent Chehab's avatar
Florent Chehab committed
27

28
29
30

class UniversitySemestersDatesSerializer(BasicModuleSerializer):

Florent Chehab's avatar
Florent Chehab committed
31
    def my_validate(self, attrs):
32
33
        attrs = super(UniversitySemestersDatesSerializer, self).my_validate(attrs)

Florent Chehab's avatar
Florent Chehab committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
        s_b, s_e = attrs['spring_begin'], attrs['spring_end']
        a_b, a_e = attrs['autumn_begin'], attrs['autumn_end']

        def test(begin, end, name):
            ok = (begin is None and end is None) or (
                begin is not None and end is not None)
            if not ok:
                semester_error(name)
            if begin is not None and (begin > end):
                semester_error(name)

        test(s_b, s_e, 'spring')
        test(a_b, a_e, 'autumn')

        if s_b is None and a_b is None:
            semester_error('autaumn and spring')
Florent Chehab's avatar
Florent Chehab committed
50

51
        return attrs
Florent Chehab's avatar
Florent Chehab committed
52
53

    class Meta:
54
        model = UniversitySemestersDates
Florent Chehab's avatar
Florent Chehab committed
55
        fields = '__all__'
Florent Chehab's avatar
Florent Chehab committed
56
57


58
class UniversitySemestersDatesViewSet(BasicModuleViewSet):
59
60
    permission_classes = get_viewset_permissions(
        "UniversitySemestersDatesViewSet")
61
62
    queryset = UniversitySemestersDates.objects.all()  # pylint: disable=E1101
    serializer_class = UniversitySemestersDatesSerializer