From 53e6278116da6d32edfc70e2dc33ac2a4d195e68 Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Tue, 13 Sep 2016 16:02:18 +1000 Subject: [PATCH] Adds a Links report type, which can be used to generate a list of links to display with a report. --- registrasion/reporting/reports.py | 40 +++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/registrasion/reporting/reports.py b/registrasion/reporting/reports.py index 2dc35d1d..ae079104 100644 --- a/registrasion/reporting/reports.py +++ b/registrasion/reporting/reports.py @@ -47,13 +47,17 @@ class Report(object): if content_type == "text/html": return Report._html_link(address, text) + else: + return text @staticmethod def _html_link(address, text): return '%s' % (address, text) -class ReportTemplateWrapper(object): +class _ReportTemplateWrapper(object): + ''' Used internally to pass `Report` objects to templates. They effectively + are used to specify the content_type for a report. ''' def __init__(self, content_type, report): self.content_type = content_type @@ -93,14 +97,40 @@ class OldReport(Report): if index > 0 or not self._link_view: return text else: - address = reverse(self._link_view, args=[text]) + address = self.get_link(text) return self._linked_text(content_type, address, text) for row in self._data: yield [cell_text(i, cell) for i, cell in enumerate(row)] - def _get_link(self, argument): - return reverse(self._link_view, argument) + def get_link(self, argument): + return reverse(self._link_view, args=[argument]) + + +class Links(Report): + + def __init__(self, title, links): + ''' + Arguments: + links ([tuple, ...]): a list of 2-tuples: + (url, link_text) + + ''' + self._title = title + self._links = links + + def title(self): + return self._title + + def headings(self): + return [] + + def rows(self, content_type): + print self._links + for url, link_text in self._links: + yield [ + self._linked_text(content_type, url, link_text) + ] def report_view(title, form_type=None): ''' Decorator that converts a report view function into something that @@ -133,7 +163,7 @@ def report_view(title, form_type=None): reports = [reports] reports = [ - ReportTemplateWrapper("text/html", report) + _ReportTemplateWrapper("text/html", report) for report in reports ]