exchange.py 2.29 KB
Newer Older
1
2
import logging

3
4
from django.db import models

5
from backend_app.models.abstract.base import BaseModel
6
from backend_app.models.partner import Partner
7
from backend_app.models.shared import SEMESTER_OPTIONS
8
9
from backend_app.models.university import University
from base_app.models import User
10

11
12
logger = logging.getLogger("django")

13
14
15

class Exchange(BaseModel):
    # This model should be filled with data from the ENT
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
59
60
    # Not using utc_id as Primary Key here for our model to be more resilient
    utc_id = models.IntegerField(null=False, unique=True)
    utc_partner_id = models.IntegerField(null=True)
    year = models.PositiveIntegerField(default=2018, null=True)
    semester = models.CharField(
        max_length=5, choices=SEMESTER_OPTIONS, default="a", null=True
    )
    duration = models.PositiveIntegerField(null=False)
    double_degree = models.BooleanField(null=False)
    master_obtained = models.BooleanField(null=False)
    student_major = models.CharField(max_length=20, null=False)
    student_minor = models.CharField(max_length=47, null=True)
    student_option = models.CharField(max_length=7, null=True)

    utc_allow_courses = models.BooleanField(null=False)
    utc_allow_login = models.BooleanField(null=False)

    # a bit of denormalization
    partner = models.ForeignKey(Partner, on_delete=models.PROTECT, null=True)
    university = models.ForeignKey(University, on_delete=models.PROTECT, null=True)
    # (managned by signals on course save)
    student = models.ForeignKey(User, on_delete=models.CASCADE, 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 exchange {} but it doesn't exist".format(
                        self.utc_partner_id, self.utc_id
                    )
                )
        else:
            self.partner = None
            self.university = None

        super().save(*args, **kwargs)