From 334f8d4bf8be5be0c847856da11aaa4ce1841562 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Mon, 29 Jan 2024 14:13:00 -0500 Subject: [PATCH] membershipworks: Move table-specific queryset operations into `get_table_data` --- membershipworks/views.py | 41 +++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/membershipworks/views.py b/membershipworks/views.py index eba7198..9f05d48 100644 --- a/membershipworks/views.py +++ b/membershipworks/views.py @@ -178,6 +178,15 @@ class EventIndexReport( export_formats = ("csv", "xlsx", "ods") export_name = "mw_events_index" + def get_table_data(self): + return ( + super() + .get_table_data() + .values(year=TruncYear("start")) + .summarize() + .order_by("year") + ) + def get_table_kwargs(self): year_column = tables.DateColumn( "Y", @@ -191,15 +200,6 @@ class EventIndexReport( "extra_columns": (("year", year_column),), } - def get_dated_queryset(self, **lookup): - return ( - super() - .get_dated_queryset(**lookup) - .values(year=TruncYear("start")) - .summarize() - .order_by("year") - ) - class EventYearReport( ExportMixin, SingleTableMixin, PermissionRequiredMixin, YearArchiveView @@ -212,6 +212,15 @@ class EventYearReport( table_class = EventSummaryTable export_formats = ("csv", "xlsx", "ods") + def get_table_data(self): + return ( + super() + .get_table_data() + .values(month=TruncMonth("start")) + .summarize() + .order_by("month") + ) + def get_export_filename(self, export_format): return f"mw_events_{self.get_year()}.{export_format}" @@ -228,25 +237,19 @@ class EventYearReport( "extra_columns": (("month", month_column),), } - def get_dated_queryset(self, **lookup): - return ( - super() - .get_dated_queryset(**lookup) - .values(month=TruncMonth("start")) - .summarize() - .order_by("month") - ) - class EventMonthReport( ExportMixin, SingleTableMixin, PermissionRequiredMixin, MonthArchiveView ): permission_required = "membershipworks.view_eventext" - queryset = EventExt.objects.select_related("category", "instructor").all() + queryset = EventExt.objects.all() date_field = "start" template_name = "membershipworks/event_month_report.dj.html" table_class = EventTable export_formats = ("csv", "xlsx", "ods") + def get_table_data(self): + return super().get_table_data().select_related("category", "instructor") + def get_export_filename(self, export_format): return f"mw_events_{self.get_year()}-{self.get_month():02}.{export_format}"