diff options
-rw-r--r-- | archaeological_operations/tests.py | 9 | ||||
-rw-r--r-- | ishtar_common/views_item.py | 54 |
2 files changed, 62 insertions, 1 deletions
diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py index 25a561b6f..b9c119826 100644 --- a/archaeological_operations/tests.py +++ b/archaeological_operations/tests.py @@ -3265,6 +3265,15 @@ class OperationSearchTest(TestCase, OperationInitTest, SearchText): values = json.loads(response.content.decode()) self.assertEqual(values["data"], expected_result) + # with criteria + q = {"stats_modality_1": "year", "stats_modality_2": "operation_type__label", + "search_vector": 'patriarche=1'} + response = c.get(reverse("get-operation", args=["json-stats"]), q) + self.assertEqual(response.status_code, 200) + values = json.loads(response.content.decode()) + expected_result = [[2010, [['Diagnostic archéologique', 1]]]] + self.assertEqual(values["data"], expected_result) + class OperationPermissionTest(TestCase, OperationInitTest): fixtures = FILE_FIXTURES diff --git a/ishtar_common/views_item.py b/ishtar_common/views_item.py index ebe9ca07d..ce70559f9 100644 --- a/ishtar_common/views_item.py +++ b/ishtar_common/views_item.py @@ -1522,7 +1522,7 @@ def _format_modality(value): return value -def _get_json_stats( +def _get_json_stats_optimized( items, stats_sum_variable, stats_modality_1, stats_modality_2, multiply=1 ): q = items @@ -1562,6 +1562,58 @@ def _get_json_stats( return HttpResponse(data, content_type="application/json") +def _get_json_stats( + items, stats_sum_variable, stats_modality_1, stats_modality_2, multiply=1 +): + # _get_json_stats_optimized should work + # but problem on container with criteria on CVL + q = items + value_keys = [] + for stat in (stats_modality_1, stats_modality_2): + if not stat: + continue + if stat.endswith("__year"): + q = q.annotate(**{stat: ExtractYear(stat[:-len("__year")])}) + value_keys.append(stat) + value_keys.append(stats_sum_variable) + q = q.values(*value_keys) + data = [] + if stats_modality_2 and stats_modality_2 != stats_modality_1: + q = q.order_by(stats_modality_1, stats_modality_2) + results = {} + for values in q.all(): + value = values[stats_sum_variable] or 0 + if stats_sum_variable == "pk": + value = 1 + modality_1 = _format_modality(values[stats_modality_1]) + modality_2 = _format_modality(values[stats_modality_2]) + key = (modality_1, modality_2) + if key not in results: + results[key] = 0 + results[key] += value * multiply + + for key in results: + modality_1, modality_2 = key + if not data or data[-1][0] != modality_1: + data.append([modality_1, []]) + data[-1][1].append((modality_2, results[key])) + else: + q = q.order_by(stats_modality_1) + results = {} + for values in q.all(): + value = values[stats_sum_variable] or 0 + if stats_sum_variable == "pk": + value = 1 + modality_1 = _format_modality(values[stats_modality_1]) + if modality_1 not in results: + results[modality_1] = 0 + results[modality_1] += value * multiply + for modality_1 in results: + data.append([modality_1, results[modality_1]]) + data = json.dumps({"data": data}) + return HttpResponse(data, content_type="application/json") + + DEFAULT_ROW_NUMBER = 10 # length is used by ajax DataTables requests EXCLUDED_FIELDS = ["length"] |