Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

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

import logging 

 

from django.db import models 

 

from backend_app.models.abstract.base import BaseModel 

from backend_app.models.exchange import Exchange 

 

logger = logging.getLogger("django") 

 

 

class Course(BaseModel): 

# 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, blank=True) 

title = models.CharField(default="", null=False, blank=True, max_length=200) 

link = models.URLField(null=True, blank=True, max_length=500) 

ects = models.DecimalField(default=0, null=True, decimal_places=2, max_digits=7) 

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) 

 

# Field to tell that for some reason there is no corresponding exchange in the UTC DB 

unlinked = models.BooleanField(default=False, null=False) 

 

# 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.error( 

"Trying to find exchange {} " 

"when updating course {} but it doesn't exist".format( 

self.utc_exchange_id, self.utc_id 

) 

) 

 

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