Coverage for apis_core/core/models.py: 30%

46 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-22 07:51 +0000

1from django.db import models 

2 

3from apis_core.utils import DateParser 

4 

5 

6# This is abstract class provides a Mixin for APIS models that need 

7# a temporal component. It provides commonly used felds for start 

8# and end dates, reps. start and end durations. 

9class LegacyDateMixin(models.Model): 

10 start_date = models.DateField(blank=True, null=True, editable=False) 

11 start_start_date = models.DateField(blank=True, null=True, editable=False) 

12 start_end_date = models.DateField(blank=True, null=True, editable=False) 

13 end_date = models.DateField(blank=True, null=True, editable=False) 

14 end_start_date = models.DateField(blank=True, null=True, editable=False) 

15 end_end_date = models.DateField(blank=True, null=True, editable=False) 

16 start_date_written = models.CharField( 

17 max_length=255, 

18 blank=True, 

19 null=True, 

20 verbose_name="Start", 

21 ) 

22 end_date_written = models.CharField( 

23 max_length=255, 

24 blank=True, 

25 null=True, 

26 verbose_name="End", 

27 ) 

28 

29 class Meta: 

30 abstract = True 

31 

32 def save(self, *args, **kwargs): 

33 skip_date_parsing = getattr(self, "skip_date_parsing", False) 

34 if not skip_date_parsing: 

35 start = None 

36 start_start = None 

37 start_end = None 

38 end = None 

39 end_start = None 

40 end_end = None 

41 

42 if self.start_date_written: 

43 start, start_start, start_end = DateParser.parse_date( 

44 self.start_date_written 

45 ) 

46 # DateParser returns datetime, but we want dates without time 

47 if start: 

48 start = start.date() 

49 if start_start: 

50 start_start = start_start.date() 

51 if start_end: 

52 start_end = start_end.date() 

53 

54 if self.end_date_written: 

55 end, end_start, end_end = DateParser.parse_date(self.end_date_written) 

56 # DateParser returns datetime, but we want dates without time 

57 if end: 

58 end = end.date() 

59 if end_start: 

60 end_start = end_start.date() 

61 if end_end: 

62 end_end = end_end.date() 

63 

64 self.start_date = start 

65 self.start_start_date = start_start 

66 self.start_end_date = start_end 

67 self.end_date = end 

68 self.end_start_date = end_start 

69 self.end_end_date = end_end 

70 

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

72 

73 return self