summaryrefslogtreecommitdiff
path: root/ishtar_common/models_common.py
diff options
context:
space:
mode:
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 ea7303089..dcb4f05a3 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 (
@@ -1580,7 +1581,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)
@@ -1608,7 +1609,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")
@@ -1670,8 +1671,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(
@@ -1800,11 +1801,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):
@@ -1943,7 +1946,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.")
@@ -1981,7 +1984,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
@@ -4493,7 +4496,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()
@@ -4505,11 +4508,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()
@@ -4870,7 +4873,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)