universitySemestersDates.py 2.08 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
from backend_app.models.abstract.basic_module import BasicModule, BasicModuleSerializer, BasicModuleViewSet
from backend_app.models.university import University
4
from rest_framework import serializers
Florent Chehab's avatar
Florent Chehab committed
5
from backend_app.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)
34

Florent Chehab's avatar
Florent Chehab committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
        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')

49
        return attrs
Florent Chehab's avatar
Florent Chehab committed
50
51

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


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