diff options
| author | Étienne Loks <etienne.loks@iggdrasil.net> | 2025-10-06 20:49:48 +0200 |
|---|---|---|
| committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2025-11-29 17:12:03 +0100 |
| commit | 1fe692f4810f54ddb689e1f9330d6ce9274f4e80 (patch) | |
| tree | f216dc11f7e458af230b1b283bf588d50ba0a255 | |
| parent | 1c14da20874141911751143e87818711319a8b38 (diff) | |
| download | Ishtar-1fe692f4810f54ddb689e1f9330d6ce9274f4e80.tar.bz2 Ishtar-1fe692f4810f54ddb689e1f9330d6ce9274f4e80.zip | |
✨ find - statistics: add top container
| -rw-r--r-- | archaeological_finds/models_finds.py | 1 | ||||
| -rw-r--r-- | ishtar_common/utils.py | 10 | ||||
| -rw-r--r-- | ishtar_common/views_item.py | 42 |
3 files changed, 42 insertions, 11 deletions
diff --git a/archaeological_finds/models_finds.py b/archaeological_finds/models_finds.py index 353d96faf..42725f881 100644 --- a/archaeological_finds/models_finds.py +++ b/archaeological_finds/models_finds.py @@ -1322,6 +1322,7 @@ class Find( "base_finds__context_record__operation__towns__areas__parent__label", _("Extended area"), ), + ("container__cached_division__splitpart_|_1", _("Top container")), ("datings__period__label", _("Chronological period")), ("material_types__label", _("Material type")), ("object_types__label", _("Object type")), diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py index 768c58495..bfe3928d2 100644 --- a/ishtar_common/utils.py +++ b/ishtar_common/utils.py @@ -67,7 +67,7 @@ from django.core.files import File from django.core.files.storage import FileSystemStorage from django.core.validators import EMPTY_VALUES, MaxValueValidator from django.db import models -from django.db.models import Q +from django.db.models import Func, Q from django.db.models.functions import Length from django.http import HttpResponseRedirect from django.urls import reverse, NoReverseMatch @@ -199,6 +199,14 @@ class Round(models.Func): arg_joiner = "::numeric, " +class SplitPart(Func): + """ + PostgreSQL split part annotation + """ + function = 'split_part' + arity = 3 + + CSV_OPTIONS = {"delimiter": ",", "quotechar": '"', "quoting": QUOTE_ALL} diff --git a/ishtar_common/views_item.py b/ishtar_common/views_item.py index 2974ac7c1..155c4c140 100644 --- a/ishtar_common/views_item.py +++ b/ishtar_common/views_item.py @@ -47,6 +47,7 @@ from django.db.models import ( ExpressionWrapper, FloatField, FileField, + Value ) from django.db.models.fields import FieldDoesNotExist from django.db.models.functions import ExtractYear @@ -84,8 +85,9 @@ from ishtar_common.utils import ( get_current_profile, HistoryError, PRIVATE_FIELDS, - SearchAltName, Round, + SearchAltName, + SplitPart, ) from .menus import Menu @@ -2089,10 +2091,19 @@ def _get_json_stats( value_keys = [] for stat in (stats_modality_1, stats_modality_2): if not stat: + value_keys.append(stat) continue if stat.endswith("__year"): q = q.annotate(**{stat: ExtractYear(stat[:-len("__year")])}) + if "__splitpart_" in stat: + st, args = stat.split("__splitpart_") + sep, index = args.split("_") + index = int(index) + stat = f"{st}_modality" + q = q.annotate(**{stat: SplitPart(F(st), Value(sep), index)}) value_keys.append(stat) + stats_modality_1, stats_modality_2 = value_keys + value_keys = [v for v in value_keys if v] value_keys.append(stats_sum_variable) q = q.values(*value_keys) data = [] @@ -2789,6 +2800,26 @@ def get_item( sub_items = sub_items.filter(base_query) if exc_query: sub_items = sub_items.exclude(exc_query) + stats_modality_1, stats_modality_2 = None, None + if data_type == "json-stats": + stats_modality_1 = request_items.get("stats_modality_1", None) + stats_modality_2 = request_items.get("stats_modality_2", None) + if ( + not stats_modality_1 + or stats_modality_1 not in model.STATISTIC_MODALITIES + ): + stats_modality_1 = model.STATISTIC_MODALITIES[0] + if stats_modality_2 not in model.STATISTIC_MODALITIES: + stats_modality_2 = None + if getattr(model, "STATISTIC_MODALITIES_QUERY", False): + if stats_modality_1 in model.STATISTIC_MODALITIES_QUERY and \ + "query" in model.STATISTIC_MODALITIES_QUERY[stats_modality_1]: + sub_items = sub_items.filter( + **model.STATISTIC_MODALITIES_QUERY[stats_modality_1]["query"]) + if stats_modality_2 in model.STATISTIC_MODALITIES_QUERY and \ + "query" in model.STATISTIC_MODALITIES_QUERY[stats_modality_2]: + sub_items = sub_items.filter( + **model.STATISTIC_MODALITIES_QUERY[stats_modality_2]["query"]) for extra in extras: sub_items = sub_items.extra(**extra) @@ -2836,15 +2867,6 @@ def get_item( # print(str(items.values("id").query)) if data_type == "json-stats": - stats_modality_1 = request_items.get("stats_modality_1", None) - stats_modality_2 = request_items.get("stats_modality_2", None) - if ( - not stats_modality_1 - or stats_modality_1 not in model.STATISTIC_MODALITIES - ): - stats_modality_1 = model.STATISTIC_MODALITIES[0] - if stats_modality_2 not in model.STATISTIC_MODALITIES: - stats_modality_2 = None stats_sum_variable = request_items.get("stats_sum_variable", None) stats_sum_variable_keys = list(model.STATISTIC_SUM_VARIABLE.keys()) if ( |
