membershipworks: Move table-specific queryset operations into get_table_data

This commit is contained in:
Adam Goldsmith 2024-01-29 14:13:00 -05:00
parent 29e87c4468
commit 334f8d4bf8

View File

@ -178,6 +178,15 @@ class EventIndexReport(
export_formats = ("csv", "xlsx", "ods") export_formats = ("csv", "xlsx", "ods")
export_name = "mw_events_index" 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): def get_table_kwargs(self):
year_column = tables.DateColumn( year_column = tables.DateColumn(
"Y", "Y",
@ -191,15 +200,6 @@ class EventIndexReport(
"extra_columns": (("year", year_column),), "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( class EventYearReport(
ExportMixin, SingleTableMixin, PermissionRequiredMixin, YearArchiveView ExportMixin, SingleTableMixin, PermissionRequiredMixin, YearArchiveView
@ -212,6 +212,15 @@ class EventYearReport(
table_class = EventSummaryTable table_class = EventSummaryTable
export_formats = ("csv", "xlsx", "ods") 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): def get_export_filename(self, export_format):
return f"mw_events_{self.get_year()}.{export_format}" return f"mw_events_{self.get_year()}.{export_format}"
@ -228,25 +237,19 @@ class EventYearReport(
"extra_columns": (("month", month_column),), "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( class EventMonthReport(
ExportMixin, SingleTableMixin, PermissionRequiredMixin, MonthArchiveView ExportMixin, SingleTableMixin, PermissionRequiredMixin, MonthArchiveView
): ):
permission_required = "membershipworks.view_eventext" permission_required = "membershipworks.view_eventext"
queryset = EventExt.objects.select_related("category", "instructor").all() queryset = EventExt.objects.all()
date_field = "start" date_field = "start"
template_name = "membershipworks/event_month_report.dj.html" template_name = "membershipworks/event_month_report.dj.html"
table_class = EventTable table_class = EventTable
export_formats = ("csv", "xlsx", "ods") 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): def get_export_filename(self, export_format):
return f"mw_events_{self.get_year()}-{self.get_month():02}.{export_format}" return f"mw_events_{self.get_year()}-{self.get_month():02}.{export_format}"