offer.py 2.94 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
14
from backend_app.utils import CustomPagination
Florent Chehab's avatar
Florent Chehab committed
15

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

Florent Chehab's avatar
Florent Chehab committed
18

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

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

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

33 34 35 36 37 38 39 40
    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):
        """
41
        Custom handling of denormalization and force character
42
        """
43 44 45 46 47

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

48 49 50 51 52 53 54
        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
55
                logger.error(
56 57 58 59 60 61 62 63 64
                    "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
65

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

Florent Chehab's avatar
Florent Chehab committed
69

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


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