From 72489ddd45b054e7808f19de50e8772e813770e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Dlouh=C3=BD?= Date: Thu, 12 Oct 2023 13:47:26 +0200 Subject: [PATCH] add functionality with queryset modifiers --- admin_tools_stats/admin.py | 2 +- .../0022_dashboardstats_queryset_modifiers.py | 32 +++++++++++++++++++ admin_tools_stats/models.py | 24 ++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 admin_tools_stats/migrations/0022_dashboardstats_queryset_modifiers.py diff --git a/admin_tools_stats/admin.py b/admin_tools_stats/admin.py index 1712c2a8..1e2729e6 100644 --- a/admin_tools_stats/admin.py +++ b/admin_tools_stats/admin.py @@ -105,7 +105,7 @@ class DashboardStatsAdmin(admin.ModelAdmin): "fields": ( "graph_key", "graph_title", - ("model_app_name", "model_name", "date_field_name"), + ("model_app_name", "model_name", "date_field_name", "queryset_modifiers"), ("operation_field_name", "distinct"), ("user_field_name", "show_to_users"), ("allowed_type_operation_field_name", "type_operation_field_name"), diff --git a/admin_tools_stats/migrations/0022_dashboardstats_queryset_modifiers.py b/admin_tools_stats/migrations/0022_dashboardstats_queryset_modifiers.py new file mode 100644 index 00000000..10c799df --- /dev/null +++ b/admin_tools_stats/migrations/0022_dashboardstats_queryset_modifiers.py @@ -0,0 +1,32 @@ +# Generated by Django 4.2.3 on 2023-10-12 11:26 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("admin_tools_stats", "0021_auto_20230210_1102"), + ] + + operations = [ + migrations.AddField( + model_name="dashboardstats", + name="queryset_modifiers", + field=models.JSONField( + blank=True, + help_text=( + "Additional queryset modifiers in JSON format:
" + "
"
+                    "[
" + ' {"filter": {"status": "active"}},
' + ' {"exclude": {"status": "deleted"}}
' + ' {"my_annotion_function": {}}
' + "]" + "
" + "Ensure the format is a valid JSON array of objects." + ), + null=True, + verbose_name="Queryset modifiers", + ), + ), + ] diff --git a/admin_tools_stats/models.py b/admin_tools_stats/models.py index f181759b..cef22fe1 100644 --- a/admin_tools_stats/models.py +++ b/admin_tools_stats/models.py @@ -315,6 +315,22 @@ class DashboardStats(models.Model): "Can contain multiple fields divided by comma.", ), ) + queryset_modifiers = JSONField( + verbose_name=_("Queryset modifiers"), + null=True, + blank=True, + help_text=mark_safe( + "Additional queryset modifiers in JSON format:
" + "
"
+            "[
" + ' {"filter": {"status": "active"}},
' + ' {"exclude": {"status": "deleted"}}
' + ' {"my_annotion_function": {}}
' + "]" + "
" + "Ensure the format is a valid JSON array of objects." + ), + ) distinct = models.BooleanField( default=False, null=False, @@ -439,6 +455,14 @@ def get_model(self): def get_queryset(self): qs = self.get_model().objects + if self.queryset_modifiers: + for modifier in self.queryset_modifiers: + method_name = list(modifier.keys())[0] + method_args = modifier[method_name] + if isinstance(method_args, dict): + qs = getattr(qs, method_name)(**method_args) + else: + qs = getattr(qs, method_name)(*method_args) return qs def get_operation_field(self, operation):