diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-03-18 19:48:55 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-06-17 13:21:27 +0200 |
commit | e2ec8a3bf016f1cc1e86a9ddd8cfc6e4eadbc35f (patch) | |
tree | 45e9ef5617a75ecdbe9f3dab0e6d8be095b69e79 | |
parent | e50b4030deb722c3c7d2ba79a7b7d0545ccd24f8 (diff) | |
download | Ishtar-e2ec8a3bf016f1cc1e86a9ddd8cfc6e4eadbc35f.tar.bz2 Ishtar-e2ec8a3bf016f1cc1e86a9ddd8cfc6e4eadbc35f.zip |
Fix stats cache for integer values
-rw-r--r-- | archaeological_context_records/tests.py | 1 | ||||
-rw-r--r-- | archaeological_finds/models_treatments.py | 1 | ||||
-rw-r--r-- | archaeological_operations/models.py | 11 | ||||
-rw-r--r-- | archaeological_operations/tests.py | 6 | ||||
-rw-r--r-- | ishtar_common/models.py | 24 | ||||
-rw-r--r-- | ishtar_common/tests.py | 1 |
6 files changed, 27 insertions, 17 deletions
diff --git a/archaeological_context_records/tests.py b/archaeological_context_records/tests.py index 32e97c730..aa98d83a8 100644 --- a/archaeological_context_records/tests.py +++ b/archaeological_context_records/tests.py @@ -292,6 +292,7 @@ class ContextRecordTest(ContextRecordInit, TestCase): cr.label = "Label label" cr.location = "I am heeere" cr.save() + cr = models.ContextRecord.objects.get(pk=cr.pk) for key in ('label', 'heeer'): self.assertIn(key, cr.search_vector) cr.operation.code_patriarche = "PATRIARCHE" diff --git a/archaeological_finds/models_treatments.py b/archaeological_finds/models_treatments.py index fb5143c7f..572f002f2 100644 --- a/archaeological_finds/models_treatments.py +++ b/archaeological_finds/models_treatments.py @@ -1067,7 +1067,6 @@ class TreatmentFile(DashboardFormItem, ClosedItem, DocumentItem, super(TreatmentFile, self).save(*args, **kwargs) - m2m_changed.connect(document_attached_changed, sender=TreatmentFile.documents.through) diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py index d900913e1..fbc36dcef 100644 --- a/archaeological_operations/models.py +++ b/archaeological_operations/models.py @@ -302,10 +302,7 @@ class ArchaeologicalSite(DocumentItem, BaseHistorizedItem, QRCodeItem, ) def __str__(self): - if self.cached_label: - return self.cached_label - self.save() - return self.cached_label + return self.cached_label or '' @property def short_class_name(self): @@ -984,11 +981,7 @@ class Operation(ClosedItem, DocumentItem, BaseHistorizedItem, QRCodeItem, return cls._return_get_owns(owns, values, get_short_menu_class) def __str__(self): - if self.cached_label or getattr(self, "_label_checked", False): - return self.cached_label - self._label_checked = True - self.save() - return self.cached_label + return self.cached_label or "" def get_values(self, prefix='', no_values=False): values = super(Operation, self).get_values(prefix=prefix, diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py index 7867116fb..b773977a8 100644 --- a/archaeological_operations/tests.py +++ b/archaeological_operations/tests.py @@ -514,7 +514,7 @@ class ImportOperationTest(ImportTest, TestCase): sorted([p.parcel_number for p in last_parcels])) self.assertEqual(sections, sorted([p.section for p in last_parcels])) - ope1 = models.Operation.objects.filter(code_patriarche='4200').all()[0] + ope1 = models.Operation.objects.get(code_patriarche='4200') towns_ope = ope1.towns.all() imported = [imp for acc, imp in impt.get_all_imported()] for p in last_parcels: @@ -527,7 +527,7 @@ class ImportOperationTest(ImportTest, TestCase): operation_id=ope1.pk).external_id, '4200-59350-YY55') # cached_label update - ope2 = models.Operation.objects.filter(code_patriarche='4201').all()[0] + ope2 = models.Operation.objects.get(code_patriarche='4201') self.assertIn('LILLE', ope2.cached_label.upper()) # delete associated parcel with the import deletion parcel_count = models.Parcel.objects.count() @@ -1434,6 +1434,7 @@ class OperationTest(TestCase, OperationInitTest): "color": u"Red"}, "frog_number": 32303} operation.save() + operation = models.Operation.objects.get(pk=operation.pk) for key in ('marmott',): self.assertIn(key, operation.search_vector) for key in ('32303', 'red', 'Red'): @@ -1793,6 +1794,7 @@ class OperationSearchTest(TestCase, OperationInitTest): self._test_search(c, search_year_q, '2042";"2020', 2, "Many integer") search_town_q = str(pgettext("key for text search", u"town")) + town = Town.objects.get(pk=town.pk) self._test_search(c, search_town_q, town.cached_label, 1, "String search with parenthesis and minus") diff --git a/ishtar_common/models.py b/ishtar_common/models.py index adad232d5..06d706cdd 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -2782,10 +2782,15 @@ class StatsCache(models.Model): def update_stats(statscache, item, funcname): if not settings.USE_BACKGROUND_TASK: - statscache.values = getattr(item, funcname)() + raw_value = getattr(item, funcname)() + if not isinstance(raw_value, dict): + values = {"_raw_": raw_value} + else: + values = raw_value + statscache.values = values statscache.updated = datetime.datetime.now() statscache.save() - return statscache.values + return values now = datetime.datetime.now() if statscache.update_requested and ( @@ -2810,7 +2815,12 @@ def _update_stats(app, model, model_pk, funcname): item = model.objects.get(pk=model_pk) except model.DoesNotExist: return - sc.values = getattr(item, funcname)() + raw_value = getattr(item, funcname)() + if not isinstance(raw_value, dict): + values = {"_raw_": raw_value} + else: + values = raw_value + sc.values = values sc.update_requested = None sc.updated = datetime.datetime.now() sc.save() @@ -2833,8 +2843,12 @@ class DashboardFormItem(object): now = datetime.datetime.now() if sc.values and ( sc.updated + datetime.timedelta(seconds=timeout)) > now: - return sc.values - return update_stats(sc, self, funcname) + values = sc.values + else: + values = update_stats(sc, self, funcname) + if "_raw_" in values: + return values["_raw_"] + return values @classmethod def get_periods(cls, slice='month', fltr={}, date_source='creation'): diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py index 07fa08750..36df516ec 100644 --- a/ishtar_common/tests.py +++ b/ishtar_common/tests.py @@ -1363,6 +1363,7 @@ class ShortMenuTest(TestCase): # available because is the creator tf.history_creator = self.user tf.save() + tf = TreatmentFile.objects.get(pk=tf.pk) response = c.get(reverse('shortcut-menu')) self.assertEqual(response.status_code, 200) self.assertTrue(str(tf.cached_label) in response.content.decode()) |