course.py 1.63 KB
Newer Older
1
2
import logging

3
4
from django.db import models

5
from backend_app.models.abstract.base import BaseModel
6
7
from backend_app.models.exchange import Exchange

8
9
logger = logging.getLogger("django")

10
11

class Course(BaseModel):
12
13
14
15
16
    # Not using utc_id as Primary Key here for our model to be more resilient
    utc_id = models.IntegerField(null=False, unique=True)
    utc_exchange_id = models.IntegerField(null=False)
    code = models.CharField(max_length=10, null=True)
    title = models.CharField(default="", null=False, blank=True, max_length=200)
17
    link = models.URLField(null=True, blank=True, max_length=500)
18
    ects = models.DecimalField(default=0, null=True, decimal_places=2, max_digits=7)
19
20
21
22
    category = models.CharField(null=True, blank=True, max_length=5)
    profile = models.CharField(null=True, blank=True, max_length=10)
    tsh_profile = models.CharField(null=True, blank=True, max_length=21)
    student_login = models.CharField(null=True, blank=True, max_length=8)
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

    # a bit of denormalization
    exchange = models.ForeignKey(
        Exchange, on_delete=models.CASCADE, related_name="exchange_courses", null=True
    )

    def save(self, *args, **kwargs):
        """
        Custom handling of denormalization
        """
        try:
            exchange = Exchange.objects.get(utc_id=self.utc_exchange_id)
            self.exchange = exchange
        except Exchange.DoesNotExist:
            logger.warning(
                "Trying to find exchange {} "
                "when updating course {} but it doesn't exist".format(
                    self.utc_exchange_id, self.utc_id
                )
            )

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