Coverage for src / mesh / models / base_models.py: 97%
35 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-02-23 15:44 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-02-23 15:44 +0000
1from __future__ import annotations
3from typing import TYPE_CHECKING
5from django.conf import settings
6from django.contrib.auth import get_user_model
7from django.db import models
8from django.utils import timezone
9from django.utils.translation import gettext_lazy as _
11if TYPE_CHECKING:
12 from .user_models import User
15class BaseChangeTrackingModel(models.Model):
16 """
17 Base model used to perform simple "change tracking" on models.
18 It adds 4 fields to the model:
19 - `date_created` The creation date of the object.
20 - `created_by` The user creating the object.
21 - `date_last_modified` The date of the last modification performed.
22 - `last_modified_by` The user who performed the last modification.
24 The data is automatically updated when calling the model `save()` method.
25 The code looks in the `_user` attribute to update the tracking fields. The views
26 must inject it in the instance before saving it.
27 """
29 # Instance of the user creating / modifying the model used to automatically fill
30 # the `created_by` and `last_modified_by` fields.
31 _user: User | None = None
32 # Whether to prevent the update of the last modification fields.
33 do_not_update = False
34 date_created = models.DateTimeField(
35 verbose_name=_("Creation date"),
36 default=timezone.now,
37 null=True,
38 editable=False,
39 )
40 created_by = models.ForeignKey(
41 settings.AUTH_USER_MODEL,
42 verbose_name=_("Created by"),
43 on_delete=models.SET_NULL,
44 null=True,
45 help_text=_("Automatically filled on save."),
46 editable=False,
47 related_name="+",
48 )
49 date_last_modified = models.DateTimeField(
50 verbose_name=_("Last modification date"),
51 default=timezone.now,
52 null=True,
53 editable=False,
54 )
55 last_modified_by = models.ForeignKey(
56 settings.AUTH_USER_MODEL,
57 verbose_name=_("Last modified by"),
58 on_delete=models.SET_NULL,
59 null=True,
60 help_text=_("Automatically filled on save."),
61 editable=False,
62 related_name="+",
63 )
65 class Meta:
66 abstract = True
68 def save(self, *args, **kwargs) -> None:
69 """
70 Update the tracking data before save:
71 - Update the creation data if the object is new
72 - Update the last modification data if the `_request` attribute is set
73 """
74 UserModel = get_user_model()
75 if self._state.adding:
76 self.date_created = timezone.now()
77 if self._user and isinstance(self._user, UserModel):
78 self.created_by_id = self._user.pk
80 if not self.do_not_update:
81 self.date_last_modified = timezone.now()
82 if self._user and isinstance(self._user, UserModel):
83 self.last_modified_by_id = self._user.pk
85 super().save(*args, **kwargs)
88class BaseSubmittableModel(models.Model):
89 """
90 Base model "mixin" for model to be effectively submitted
91 (different action from just saving the model).
92 """
94 submitted = models.BooleanField(verbose_name=_("Submitted"), default=False, editable=False)
95 date_submitted = models.DateTimeField(
96 verbose_name=_("Submitted on"), editable=False, null=True
97 )
99 class Meta:
100 abstract = True
102 @property
103 def is_submittable(self) -> bool:
104 return True