diff options
Diffstat (limited to 'ishtar_common/views_item.py')
| -rw-r--r-- | ishtar_common/views_item.py | 42 |
1 files changed, 32 insertions, 10 deletions
diff --git a/ishtar_common/views_item.py b/ishtar_common/views_item.py index 62de9b8b4..83c84ae78 100644 --- a/ishtar_common/views_item.py +++ b/ishtar_common/views_item.py @@ -46,6 +46,7 @@ from django.db.models import ( ExpressionWrapper, FloatField, FileField, + Value ) from django.db.models.functions import ExtractYear from django.db.utils import ProgrammingError @@ -82,8 +83,9 @@ from ishtar_common.utils import ( get_current_profile, HistoryError, PRIVATE_FIELDS, - SearchAltName, Round, + SearchAltName, + SplitPart, ) from .menus import Menu @@ -2102,10 +2104,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 = [] @@ -2813,6 +2824,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) @@ -2860,15 +2891,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 ( |
