From 907c4f5dafe3b2083ccb71ed1aacaf99bab63a59 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Tue, 28 May 2019 13:08:58 +0200 Subject: Statistics cache: rpovide an expected type in order to fix bad cache init --- ishtar_common/models.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'ishtar_common') diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 8d300c64a..bb19b0913 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -2945,19 +2945,28 @@ class DashboardFormItem(object): """ def _get_or_set_stats(self, funcname, update, - timeout=settings.CACHE_TIMEOUT): + timeout=settings.CACHE_TIMEOUT, + expected_type=None): model_name = self._meta.app_label + "." + self._meta.model_name sc, __ = StatsCache.objects.get_or_create( model=model_name, model_pk=self.pk ) now = datetime.datetime.now() - if sc.values and ( + if not update and sc.values and ( sc.updated + datetime.timedelta(seconds=timeout)) > now: values = sc.values + from_cache = True else: values = update_stats(sc, self, funcname) + from_cache = False if "_raw_" in values: - return values["_raw_"] + values = values["_raw_"] + if expected_type is not None and not isinstance(values, expected_type): + if from_cache: + return self._get_or_set_stats(funcname, True, + expected_type=expected_type) + else: + return expected_type() return values @classmethod -- cgit v1.2.3