universitySemestersDates.py 2.18 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
5
from backend.utils import get_moderation_level, get_viewset_permissions
Florent Chehab's avatar
Florent Chehab committed
6
7
8
9
10
11
12
13
14
15
16
17


def check_nones(val1, val2):
    if val1 is None and val2 is not None:
        raise serializers.ValidationError("Check your dates")

    if val2 is None and val1 is not None:
        raise serializers.ValidationError("Check your dates")

    return val1 is None and val2 is None


18
class UniversitySemestersDates(BasicModule):
19
    moderation_level = get_moderation_level("UniversitySemestersDates")
Florent Chehab's avatar
Florent Chehab committed
20
21
22
23
24
25
26
27
28

    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)

29
30
31
    @classmethod
    def get_serializer(cls):
        return UniversitySemestersDatesSerializer
Florent Chehab's avatar
Florent Chehab committed
32

33
34
35
36
37
38

class UniversitySemestersDatesSerializer(BasicModuleSerializer):

    def validate(self, attrs):
        spring_ok = check_nones(attrs['spring_begin'], attrs['spring_end'])
        autumn_ok = check_nones(attrs['autumn_begin'], attrs['autumn_end'])
Florent Chehab's avatar
Florent Chehab committed
39
40

        if not spring_ok:
41
            spring_ok = attrs['spring_begin'] < attrs['spring_end']
Florent Chehab's avatar
Florent Chehab committed
42
43

        if not autumn_ok:
44
            autumn_ok = attrs['autumn_begin'] < attrs['autumn_end']
Florent Chehab's avatar
Florent Chehab committed
45
46
47
        # no check if spring_end < autumn_begin in case info is not available

        if not (spring_ok and autumn_ok):
Florent Chehab's avatar
Florent Chehab committed
48
49
            raise serializers.ValidationError(
                "Check your dates !")  # TODO better error
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