diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2025-03-27 14:47:12 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2025-06-13 18:08:06 +0200 |
commit | 84ed3cfb40a64e64ddc32cc0df943cb12e198717 (patch) | |
tree | d4220a3a19ee6ae31fbf281d7e0d35fb5b452cd1 /ishtar_common/models_common.py | |
parent | db174e7f376a3687cb6cd84339592231ceae0ee0 (diff) | |
download | Ishtar-84ed3cfb40a64e64ddc32cc0df943cb12e198717.tar.bz2 Ishtar-84ed3cfb40a64e64ddc32cc0df943cb12e198717.zip |
♻️ django: fix warnings for v5 - migrate to timezone aware dates
Diffstat (limited to 'ishtar_common/models_common.py')
-rw-r--r-- | ishtar_common/models_common.py | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/ishtar_common/models_common.py b/ishtar_common/models_common.py index d1a9a521b..31a02580f 100644 --- a/ishtar_common/models_common.py +++ b/ishtar_common/models_common.py @@ -43,6 +43,7 @@ from django.db.models import JSONField, Q, Count, Max from django.db.models.signals import post_save, post_delete, m2m_changed from django.template import loader from django.template.defaultfilters import slugify +from django.utils import timezone from django.utils.safestring import SafeText, mark_safe from django.utils.translation import activate, deactivate from ishtar_common.utils import ( @@ -1544,7 +1545,7 @@ class HistoricalRecords(BaseHistoricalRecords): return if not history_modifier: return - history_date = getattr(instance, "_history_date", datetime.datetime.now()) + history_date = getattr(instance, "_history_date", timezone.now()) history_change_reason = getattr(instance, "changeReason", None) force = getattr(instance, "_force_history", False) manager = getattr(instance, self.manager_name) @@ -1572,7 +1573,7 @@ class HistoricalRecords(BaseHistoricalRecords): old_instance = q_history.all()[0] # multiple saving by the same user in a very short time are generaly # caused by post_save signals it is not relevant to keep them - min_history_date = datetime.datetime.now() - datetime.timedelta(seconds=5) + min_history_date = timezone.now() - datetime.timedelta(seconds=5) q = q_history.filter( history_date__isnull=False, history_date__gt=min_history_date ).order_by("-history_date", "-history_id") @@ -1634,8 +1635,8 @@ class BaseHistorizedItem( blank=True, null=True, ) - last_modified = models.DateTimeField(blank=True, default=datetime.datetime.now) - created = models.DateTimeField(blank=True, default=datetime.datetime.now) + last_modified = models.DateTimeField(blank=True, default=timezone.now) + created = models.DateTimeField(blank=True, default=timezone.now) history_m2m = JSONField(default=dict, blank=True) need_update = models.BooleanField(verbose_name=_("Need update"), default=False) locked = models.BooleanField( @@ -1763,11 +1764,13 @@ class BaseHistorizedItem( else: item = historized[step] else: + date_match = False for step, item in enumerate(historized): - if item.history_date == date: + if item.history_date == timezone.make_aware(date, item.history_date.tzinfo): + date_match = True break # ended with no match - if item.history_date != date: + if not date_match: return item._step = step if len(historized) != (step + 1): @@ -1906,7 +1909,7 @@ class BaseHistorizedItem( if (not getattr(self, "skip_history_when_saving", False) and not getattr(self, "_no_last_modified_update", False)) \ or not self.last_modified: - self.last_modified = datetime.datetime.now() + self.last_modified = timezone.now() if not getattr(self, "skip_history_when_saving", False): if not hasattr(self, "history_modifier"): raise NotImplementedError("Should have a history_modifier field.") @@ -1934,7 +1937,7 @@ class BaseHistorizedItem( class LightHistorizedItem(BaseHistorizedItem): - history_date = models.DateTimeField(default=datetime.datetime.now) + history_date = models.DateTimeField(default=timezone.now) class Meta: abstract = True @@ -4445,7 +4448,7 @@ def _update_stats(app, model, model_pk, funcname): current_values[funcname] = value sc.values = current_values sc.update_requested = None - sc.updated = datetime.datetime.now() + sc.updated = timezone.now() sc.save() @@ -4457,11 +4460,11 @@ def update_stats(statscache, item, funcname): value = getattr(item, funcname)() current_values[funcname] = value statscache.values = current_values - statscache.updated = datetime.datetime.now() + statscache.updated = timezone.now() statscache.save() return current_values - now = datetime.datetime.now() + now = timezone.now() app_name = item._meta.app_label model_name = item._meta.model_name statscache.update_requested = now.isoformat() @@ -4822,7 +4825,7 @@ class ImageContainerModel: return "{}/{}".format(self._get_base_image_path(), filename) def _get_base_image_path(self): - n = datetime.datetime.now() + n = timezone.now() return "upload/{}/{:02d}/{:02d}".format(n.year, n.month, n.day) |