summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--archaeological_operations/models.py9
-rw-r--r--ishtar_common/models.py31
2 files changed, 27 insertions, 13 deletions
diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py
index 3538db2f8..c295e40ac 100644
--- a/archaeological_operations/models.py
+++ b/archaeological_operations/models.py
@@ -1604,12 +1604,13 @@ class Operation(ClosedItem, DocumentItem, BaseHistorizedItem, QRCodeItem,
finds__upstream_treatment_id__isnull=True,
finds__base_finds__context_record__operation=self)]
for q in qs:
- for res in q.values('source_type').distinct():
- st = res['source_type']
+ for st in set(q.values_list('source_type_id',
+ flat=True).distinct()):
if st not in docs:
docs[st] = 0
- docs[st] += q.filter(source_type=st).count()
- docs = [(str(SourceType.objects.get(pk=k)), docs[k]) for k in docs]
+ docs[st] += q.filter(source_type_id=st).count()
+ docs = [(str(SourceType.objects.get(pk=k))
+ if k else str(_("No type")), docs[k]) for k in docs]
return list(sorted(docs, key=lambda x: x[0]))
@property
diff --git a/ishtar_common/models.py b/ishtar_common/models.py
index 945a48ea8..f6ed81f9c 100644
--- a/ishtar_common/models.py
+++ b/ishtar_common/models.py
@@ -3212,23 +3212,36 @@ def update_stats(statscache, item, funcname):
return statscache.values
+def __get_stats_cache_values(model_name, model_pk):
+ q = StatsCache.objects.filter(
+ model=model_name, model_pk=model_pk
+ )
+ nb = q.count()
+ if nb >= 1:
+ sc = q.all()[0]
+ for extra in q.order_by("-id").all()[1:]:
+ extra.delete()
+ else:
+ sc = StatsCache.objects.create(
+ model=model_name, model_pk=model_pk
+ )
+ values = sc.values
+ if not values:
+ values = {}
+ return sc, values
+
+
@task()
def _update_stats(app, model, model_pk, funcname):
model_name = app + "." + model
- sc, __ = StatsCache.objects.get_or_create(
- model=model_name, model_pk=model_pk
- )
model = apps.get_model(app, model)
try:
item = model.objects.get(pk=model_pk)
except model.DoesNotExist:
return
- current_values = sc.values
- if not current_values:
- current_values = {}
value = getattr(item, funcname)()
+ sc, current_values = __get_stats_cache_values(model_name, model_pk)
current_values[funcname] = value
-
sc.values = current_values
sc.update_requested = None
sc.updated = datetime.datetime.now()
@@ -3258,8 +3271,8 @@ class DashboardFormItem(object):
values = update_stats(sc, self, funcname)
if funcname in values:
values = values[funcname]
- elif "_raw_" in values:
- values = values["_raw_"]
+ else:
+ values = 0
if expected_type is not None and not isinstance(values, expected_type):
if from_cache:
return self._get_or_set_stats(funcname, True,