offer.py 2.86 KB
Newer Older
1
2
import logging

Florent Chehab's avatar
Florent Chehab committed
3
from django.db import models
4

5
6
7
8
from backend_app.models.abstract.base import (
    BaseModel,
    BaseModelSerializer,
    BaseModelViewSet,
9
)
10
from backend_app.models.partner import Partner
11
from backend_app.models.shared import SEMESTER_OPTIONS
12
13
from backend_app.models.university import University
from backend_app.permissions.app_permissions import ReadOnly
Florent Chehab's avatar
Florent Chehab committed
14

15
16
logger = logging.getLogger("django")

Florent Chehab's avatar
Florent Chehab committed
17

18
class Offer(BaseModel):
19
20
21
    utc_partner_id = models.IntegerField(null=False)
    year = models.PositiveIntegerField(default=2018, null=True)
    semester = models.CharField(
22
        max_length=2, choices=SEMESTER_OPTIONS, default="A", null=True
23
24
25
26
27
    )

    comment = models.TextField(max_length=500, null=True)
    double_degree = models.BooleanField(default=False)
    is_master_offered = models.BooleanField(default=False, null=True)
28

29
    nb_seats_offered = models.PositiveIntegerField(null=True)
30
31
    # null => exchange not possible

32
33
34
35
36
37
38
39
    specialties = models.CharField(max_length=4000, null=True)

    # A bit of denormalization
    partner = models.ForeignKey(Partner, on_delete=models.PROTECT, null=False)
    university = models.ForeignKey(University, on_delete=models.PROTECT, null=True)

    def save(self, *args, **kwargs):
        """
40
        Custom handling of denormalization and force character
41
        """
42
43
44
45
46

        # make sure the semester is upper case
        if self.semester is not None:
            self.semester = self.semester.upper()

47
48
49
50
51
52
53
        if self.utc_partner_id is not None:
            try:
                self.partner = Partner.objects.get(utc_id=self.utc_partner_id)
                self.university = self.partner.university
            except Partner.DoesNotExist:
                self.partner = None
                self.university = None
54
                logger.error(
55
56
57
58
59
60
61
62
63
                    "Trying to find partner {}"
                    "when updating offer {} but it doesn't exist".format(
                        self.utc_partner_id, self.pk
                    )
                )
        else:
            self.partner = None
            self.university = None
        super().save(*args, **kwargs)
Florent Chehab's avatar
Florent Chehab committed
64

65
66
67
    class Meta:
        unique_together = ("utc_partner_id", "year", "semester")

Florent Chehab's avatar
Florent Chehab committed
68

69
class OfferSerializer(BaseModelSerializer):
Florent Chehab's avatar
Florent Chehab committed
70
71
    class Meta:
        model = Offer
72
73
74
        fields = BaseModelSerializer.Meta.fields + (
            "year",
            "semester",
75
            "comment",
76
77
78
79
            "double_degree",
            "is_master_offered",
            "specialties",
            "university",
80
            "nb_seats_offered",
81
        )
Florent Chehab's avatar
Florent Chehab committed
82
83


84
class OfferViewSet(BaseModelViewSet):
85
86
87
    queryset = Offer.objects.all().order_by(
        "-year", "semester"
    )  # pylint: disable=E1101
Florent Chehab's avatar
Florent Chehab committed
88
    serializer_class = OfferSerializer
89
90
    permission_classes = (ReadOnly,)
    end_point_route = "offers"
91
92
    required_filterset_fields = ("university",)
    filterset_fields = ("university",)