summaryrefslogtreecommitdiff
path: root/ishtar_common/models_common.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2025-03-27 14:47:12 +0100
committerÉtienne Loks <etienne.loks@iggdrasil.net>2025-07-21 15:07:41 +0200
commiteda40f3481b6963163a04cc393db2bb95d3c5242 (patch)
treeb8d3c303ed1d7b9f0dd1a754784bddee7a7460f6 /ishtar_common/models_common.py
parent8ecebddbbaccd5f859cf526c07468c6f921a17b9 (diff)
downloadIshtar-eda40f3481b6963163a04cc393db2bb95d3c5242.tar.bz2
Ishtar-eda40f3481b6963163a04cc393db2bb95d3c5242.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.py27
1 files changed, 15 insertions, 12 deletions
diff --git a/ishtar_common/models_common.py b/ishtar_common/models_common.py
index a5e95bf55..efdeda7e0 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 (
@@ -1562,7 +1563,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)
@@ -1590,7 +1591,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")
@@ -1652,8 +1653,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(
@@ -1781,11 +1782,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):
@@ -1924,7 +1927,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.")
@@ -1962,7 +1965,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
@@ -4464,7 +4467,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()
@@ -4476,11 +4479,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()
@@ -4841,7 +4844,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)