Skip to content

Commit

Permalink
add functionality with queryset modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrDlouhy committed Aug 1, 2024
1 parent 1f8edcb commit 4a2f619
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion admin_tools_stats/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,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"),
Expand Down
Original file line number Diff line number Diff line change
@@ -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:<br>"
"<pre>"
"[<br>"
' {"filter": {"status": "active"}},<br>'
' {"exclude": {"status": "deleted"}}<br>'
' {"my_annotion_function": {}}<br>'
"]"
"</pre>"
"Ensure the format is a valid JSON array of objects."
),
null=True,
verbose_name="Queryset modifiers",
),
),
]
24 changes: 24 additions & 0 deletions admin_tools_stats/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:<br>"
"<pre>"
"[<br>"
' {"filter": {"status": "active"}},<br>'
' {"exclude": {"status": "deleted"}}<br>'
' {"my_annotion_function": {}}<br>'
"]"
"</pre>"
"Ensure the format is a valid JSON array of objects."
),
)
distinct = models.BooleanField(
default=False,
null=False,
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 4a2f619

Please sign in to comment.