-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbase.py
More file actions
59 lines (44 loc) · 1.83 KB
/
base.py
File metadata and controls
59 lines (44 loc) · 1.83 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
from django.db import models
from lung_cancer_screening.questions.services.metrics import Metrics
import logging
logger = logging.getLogger(__name__)
class BaseQuerySet(models.QuerySet):
def get_or_build(self, **kwargs):
"""
Get an existing object matching the kwargs, or build a new unsaved instance.
Returns a tuple of (object, created) where created is True if a new instance was built.
"""
# Check if any kwargs are unsaved model instances
for key, value in kwargs.items():
if isinstance(value, models.Model) and value.pk is None:
# If we have an unsaved instance, just build a new one
return (self.model(**kwargs), True)
try:
return (self.get(**kwargs), False)
except self.model.DoesNotExist:
return (self.model(**kwargs), True)
class BaseModel(models.Model):
class Meta:
abstract = True
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
objects = BaseQuerySet.as_manager()
@property
def model_name(self) -> str:
return self._meta.label_lower
def save(self, *args, **kwargs):
is_create = self.pk is None
old_status = None
if not is_create and hasattr(self, "status"):
old_status = (
self.__class__.objects.filter(pk=self.pk)
.values_list("status", flat=True)
.first()
)
self.full_clean() # Validate before saving
super().save(*args, **kwargs)
metrics = Metrics()
if is_create:
metrics.record_request_created(self.model_name)
if hasattr(self, "status") and self.status == "submitted" and old_status != "submitted":
metrics.record_request_submitted(self.model_name)