From bbce369a38f25facf19e35fca826bb8b629b31de Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Tue, 13 Sep 2016 18:44:13 +1000 Subject: [PATCH] Allows for callable attributes to be specified in QuerysetReports. --- registrasion/reporting/reports.py | 7 +++++++ registrasion/reporting/views.py | 20 +++++++------------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/registrasion/reporting/reports.py b/registrasion/reporting/reports.py index 1061a903..d533ecf1 100644 --- a/registrasion/reporting/reports.py +++ b/registrasion/reporting/reports.py @@ -128,6 +128,13 @@ class QuerysetReport(BasicReport): def rgetattr(item, attr): for i in attr.split("__"): item = getattr(item, i) + + if callable(item): + try: + return item() + except TypeError: + pass + return item for row in self._queryset: diff --git a/registrasion/reporting/views.py b/registrasion/reporting/views.py index 934705ce..0149c987 100644 --- a/registrasion/reporting/views.py +++ b/registrasion/reporting/views.py @@ -285,21 +285,16 @@ def attendee(request, form, user_id=None): )) # Invoices - # TODO make this a querysetreport - headings = ["Invoice ID", "Status", "Value"] - data = [] - invoices = commerce.Invoice.objects.filter( user=attendee.user, ) - for invoice in invoices: - data.append([ - invoice.id, invoice.get_status_display(), invoice.value, - ]) - - reports.append( - ListReport("Invoices", headings, data, link_view=views.invoice) - ) + reports.append(QuerysetReport( + "Invoices", + ["Invoice ID", "Status", "Value"], + ["id", "get_status_display", "value"], + invoices, + link_view=views.invoice, + )) # Credit Notes credit_notes = commerce.CreditNote.objects.filter( @@ -325,7 +320,6 @@ def attendee(request, form, user_id=None): link_view=views.invoice, )) - return reports