offer.py 2.49 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
22
23
24
25
26
27
    utc_partner_id = models.IntegerField(null=False)
    year = models.PositiveIntegerField(default=2018, null=True)
    semester = models.CharField(
        max_length=2, choices=SEMESTER_OPTIONS, default="a", null=True
    )

    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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    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):
        """
        Custom handling of denormalization
        """
        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
                logger.warning(
                    "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
59
60


61
class OfferSerializer(BaseModelSerializer):
Florent Chehab's avatar
Florent Chehab committed
62
63
    class Meta:
        model = Offer
64
65
66
67
68
69
70
71
72
73
        fields = BaseModelSerializer.Meta.fields + (
            "year",
            "semester",
            "commend",
            "double_degree",
            "is_master_offered",
            "specialties",
            "partner",
            "university",
        )
Florent Chehab's avatar
Florent Chehab committed
74
75


76
class OfferViewSet(BaseModelViewSet):
Florent Chehab's avatar
Florent Chehab committed
77
78
    queryset = Offer.objects.all()  # pylint: disable=E1101
    serializer_class = OfferSerializer
79
80
    permission_classes = (ReadOnly,)
    end_point_route = "offers"