2016-10-14 18:11:27 +00:00
|
|
|
import csv
|
|
|
|
import forms
|
|
|
|
|
2016-09-02 00:08:11 +00:00
|
|
|
from django.contrib.auth.decorators import user_passes_test
|
|
|
|
from django.shortcuts import render
|
2016-09-13 05:32:55 +00:00
|
|
|
from django.core.urlresolvers import reverse
|
2016-10-14 18:11:27 +00:00
|
|
|
from django.http import HttpResponse
|
2016-09-02 00:08:11 +00:00
|
|
|
from functools import wraps
|
|
|
|
|
|
|
|
from registrasion import views
|
|
|
|
|
|
|
|
|
|
|
|
''' A list of report views objects that can be used to load a list of
|
|
|
|
reports. '''
|
|
|
|
_all_report_views = []
|
|
|
|
|
|
|
|
|
|
|
|
class Report(object):
|
|
|
|
|
2016-09-13 05:32:55 +00:00
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def title():
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def headings():
|
|
|
|
''' Returns the headings for the report. '''
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def rows(content_type):
|
|
|
|
'''
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
content_type (str): The content-type for the output format of this
|
|
|
|
report.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
An iterator, which yields each row of the data. Each row should
|
|
|
|
be an iterable containing the cells, rendered appropriately for
|
|
|
|
content_type.
|
|
|
|
'''
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def _linked_text(self, content_type, address, text):
|
|
|
|
'''
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
an HTML linked version of text, if the content_type for this report
|
|
|
|
is HTMLish, otherwise, the text.
|
|
|
|
'''
|
|
|
|
|
|
|
|
if content_type == "text/html":
|
|
|
|
return Report._html_link(address, text)
|
2016-09-13 06:02:18 +00:00
|
|
|
else:
|
|
|
|
return text
|
2016-09-13 05:32:55 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _html_link(address, text):
|
|
|
|
return '<a href="%s">%s</a>' % (address, text)
|
|
|
|
|
|
|
|
|
2016-09-13 06:02:18 +00:00
|
|
|
class _ReportTemplateWrapper(object):
|
|
|
|
''' Used internally to pass `Report` objects to templates. They effectively
|
|
|
|
are used to specify the content_type for a report. '''
|
2016-09-13 05:32:55 +00:00
|
|
|
|
|
|
|
def __init__(self, content_type, report):
|
|
|
|
self.content_type = content_type
|
|
|
|
self.report = report
|
|
|
|
|
|
|
|
def title(self):
|
|
|
|
return self.report.title()
|
|
|
|
|
|
|
|
def headings(self):
|
|
|
|
return self.report.headings()
|
|
|
|
|
|
|
|
def rows(self):
|
|
|
|
return self.report.rows(self.content_type)
|
|
|
|
|
|
|
|
|
2016-09-13 06:19:18 +00:00
|
|
|
class BasicReport(Report):
|
2016-09-13 05:32:55 +00:00
|
|
|
|
2016-09-13 06:19:18 +00:00
|
|
|
def __init__(self, title, headings, link_view=None):
|
|
|
|
super(BasicReport, self).__init__()
|
2016-09-02 05:14:54 +00:00
|
|
|
self._title = title
|
2016-09-02 00:08:11 +00:00
|
|
|
self._headings = headings
|
2016-09-02 01:05:38 +00:00
|
|
|
self._link_view = link_view
|
2016-09-02 00:08:11 +00:00
|
|
|
|
|
|
|
def title(self):
|
|
|
|
''' Returns the title for this report. '''
|
|
|
|
return self._title
|
|
|
|
|
|
|
|
def headings(self):
|
|
|
|
''' Returns the headings for the table. '''
|
|
|
|
return self._headings
|
|
|
|
|
2016-09-13 06:19:18 +00:00
|
|
|
def cell_text(self, content_type, index, text):
|
|
|
|
if index > 0 or not self._link_view:
|
|
|
|
return text
|
|
|
|
else:
|
|
|
|
address = self.get_link(text)
|
|
|
|
return self._linked_text(content_type, address, text)
|
|
|
|
|
|
|
|
def get_link(self, argument):
|
|
|
|
return reverse(self._link_view, args=[argument])
|
|
|
|
|
|
|
|
|
|
|
|
class ListReport(BasicReport):
|
|
|
|
|
|
|
|
def __init__(self, title, headings, data, link_view=None):
|
|
|
|
super(ListReport, self).__init__(title, headings, link_view=link_view)
|
|
|
|
self._data = data
|
|
|
|
|
2016-09-13 05:32:55 +00:00
|
|
|
def rows(self, content_type):
|
2016-09-02 00:08:11 +00:00
|
|
|
''' Returns the data rows for the table. '''
|
|
|
|
|
2016-09-13 05:32:55 +00:00
|
|
|
for row in self._data:
|
2016-09-13 06:19:18 +00:00
|
|
|
yield [
|
|
|
|
self.cell_text(content_type, i, cell)
|
|
|
|
for i, cell in enumerate(row)
|
|
|
|
]
|
2016-09-02 01:05:38 +00:00
|
|
|
|
2016-09-13 06:19:18 +00:00
|
|
|
|
|
|
|
class QuerysetReport(BasicReport):
|
|
|
|
|
2016-09-13 08:47:51 +00:00
|
|
|
def __init__(self, title, attributes, queryset, headings=None,
|
|
|
|
link_view=None):
|
2016-09-13 08:54:28 +00:00
|
|
|
super(QuerysetReport, self).__init__(
|
|
|
|
title, headings, link_view=link_view
|
|
|
|
)
|
2016-09-13 06:19:18 +00:00
|
|
|
self._attributes = attributes
|
|
|
|
self._queryset = queryset
|
|
|
|
|
2016-09-13 08:47:51 +00:00
|
|
|
def headings(self):
|
|
|
|
if self._headings is not None:
|
|
|
|
return self._headings
|
|
|
|
|
|
|
|
return [
|
|
|
|
" ".join(i.split("_")).capitalize() for i in self._attributes
|
|
|
|
]
|
|
|
|
|
2016-09-13 06:19:18 +00:00
|
|
|
def rows(self, content_type):
|
|
|
|
|
|
|
|
def rgetattr(item, attr):
|
|
|
|
for i in attr.split("__"):
|
|
|
|
item = getattr(item, i)
|
2016-09-13 08:44:13 +00:00
|
|
|
|
|
|
|
if callable(item):
|
|
|
|
try:
|
|
|
|
return item()
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
|
2016-09-13 06:19:18 +00:00
|
|
|
return item
|
|
|
|
|
|
|
|
for row in self._queryset:
|
|
|
|
yield [
|
|
|
|
self.cell_text(content_type, i, rgetattr(row, attribute))
|
|
|
|
for i, attribute in enumerate(self._attributes)
|
|
|
|
]
|
2016-09-13 06:02:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
]
|
2016-09-02 00:08:11 +00:00
|
|
|
|
2016-09-13 08:54:28 +00:00
|
|
|
|
2016-09-02 00:30:12 +00:00
|
|
|
def report_view(title, form_type=None):
|
2016-09-02 00:08:11 +00:00
|
|
|
''' Decorator that converts a report view function into something that
|
|
|
|
displays a Report.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
title (str):
|
|
|
|
The title of the report.
|
2016-09-02 00:30:12 +00:00
|
|
|
form_type (Optional[forms.Form]):
|
|
|
|
A form class that can make this report display things. If not
|
|
|
|
supplied, no form will be displayed.
|
2016-09-02 00:08:11 +00:00
|
|
|
|
|
|
|
'''
|
|
|
|
|
2016-10-14 17:27:22 +00:00
|
|
|
# Create & return view
|
2016-09-02 00:08:11 +00:00
|
|
|
def _report(view):
|
2016-10-14 23:10:36 +00:00
|
|
|
report_view = ReportView(view, title, form_type)
|
|
|
|
report_view = user_passes_test(views._staff_only)(report_view)
|
|
|
|
report_view = wraps(view)(report_view)
|
2016-10-14 17:27:22 +00:00
|
|
|
|
|
|
|
# Add this report to the list of reports.
|
2016-10-14 23:10:36 +00:00
|
|
|
_all_report_views.append(report_view)
|
|
|
|
|
|
|
|
return report_view
|
2016-10-14 17:27:22 +00:00
|
|
|
|
|
|
|
return _report
|
|
|
|
|
2016-10-14 23:10:36 +00:00
|
|
|
|
2016-10-14 17:27:22 +00:00
|
|
|
class ReportView(object):
|
2016-09-02 00:08:11 +00:00
|
|
|
|
2016-10-14 23:10:36 +00:00
|
|
|
def __init__(self, inner_view, title, form_type):
|
|
|
|
# Consolidate form_type so it has content type and section
|
2016-10-14 18:11:27 +00:00
|
|
|
self.inner_view = inner_view
|
2016-10-14 17:27:22 +00:00
|
|
|
self.title = title
|
|
|
|
self.form_type = form_type
|
2016-10-14 18:11:27 +00:00
|
|
|
|
2016-10-14 23:10:36 +00:00
|
|
|
def __call__(self, request, *a, **k):
|
|
|
|
data = ReportViewRequestData(self, request, *a, **k)
|
|
|
|
return self.render(data)
|
2016-10-14 18:11:27 +00:00
|
|
|
|
2016-10-14 23:10:36 +00:00
|
|
|
def get_form(self, request):
|
2016-10-14 17:27:22 +00:00
|
|
|
|
2016-10-14 18:11:27 +00:00
|
|
|
# Create a form instance
|
2016-10-14 17:28:38 +00:00
|
|
|
if self.form_type is not None:
|
2016-10-14 23:10:36 +00:00
|
|
|
form = self.form_type(request.GET)
|
2016-10-14 18:11:27 +00:00
|
|
|
|
|
|
|
# Pre-validate it
|
2016-10-14 17:28:38 +00:00
|
|
|
form.is_valid()
|
|
|
|
else:
|
|
|
|
form = None
|
2016-09-02 00:28:24 +00:00
|
|
|
|
2016-10-14 23:10:36 +00:00
|
|
|
return form
|
2016-10-14 18:11:27 +00:00
|
|
|
|
2016-10-14 23:10:36 +00:00
|
|
|
@classmethod
|
|
|
|
def wrap_reports(cls, reports, content_type):
|
2016-10-14 17:28:38 +00:00
|
|
|
reports = [
|
2016-10-14 23:10:36 +00:00
|
|
|
_ReportTemplateWrapper(content_type, report)
|
2016-10-14 17:28:38 +00:00
|
|
|
for report in reports
|
|
|
|
]
|
2016-09-13 05:32:55 +00:00
|
|
|
|
2016-10-14 18:11:27 +00:00
|
|
|
return reports
|
|
|
|
|
2016-10-14 23:10:36 +00:00
|
|
|
def render(self, data):
|
|
|
|
renderers = {
|
|
|
|
"text/csv": self._render_as_csv,
|
|
|
|
"text/html": self._render_as_html,
|
2016-10-14 23:26:36 +00:00
|
|
|
None: self._render_as_html,
|
2016-10-14 23:10:36 +00:00
|
|
|
}
|
|
|
|
render = renderers[data.content_type]
|
|
|
|
return render(data)
|
2016-10-14 18:11:27 +00:00
|
|
|
|
2016-10-14 23:10:36 +00:00
|
|
|
def _render_as_html(self, data):
|
2016-10-14 17:28:38 +00:00
|
|
|
ctx = {
|
|
|
|
"title": self.title,
|
2016-10-14 23:10:36 +00:00
|
|
|
"form": data.form,
|
|
|
|
"reports": data.reports,
|
2016-10-14 17:28:38 +00:00
|
|
|
}
|
2016-09-02 00:08:11 +00:00
|
|
|
|
2016-10-14 23:10:36 +00:00
|
|
|
return render(data.request, "registrasion/report.html", ctx)
|
2016-09-02 00:08:11 +00:00
|
|
|
|
2016-10-14 23:10:36 +00:00
|
|
|
def _render_as_csv(self, data):
|
|
|
|
report = data.reports[data.section]
|
2016-10-14 18:11:27 +00:00
|
|
|
|
|
|
|
# Create the HttpResponse object with the appropriate CSV header.
|
|
|
|
response = HttpResponse(content_type='text/csv')
|
|
|
|
|
|
|
|
writer = csv.writer(response)
|
2016-10-14 18:19:10 +00:00
|
|
|
encode = lambda i: i.encode("utf8") if isinstance(i, unicode) else i
|
|
|
|
writer.writerow(list(encode(i) for i in report.headings()))
|
2016-10-14 18:11:27 +00:00
|
|
|
for row in report.rows():
|
2016-10-14 18:19:10 +00:00
|
|
|
writer.writerow(list(encode(i) for i in row))
|
2016-10-14 18:11:27 +00:00
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2016-10-14 23:10:36 +00:00
|
|
|
class ReportViewRequestData(object):
|
|
|
|
|
|
|
|
def __init__(self, report_view, request, *a, **k):
|
|
|
|
self.report_view = report_view
|
|
|
|
self.request = request
|
|
|
|
|
|
|
|
# Calculate other data
|
|
|
|
self.form = report_view.get_form(request)
|
|
|
|
|
2016-10-14 23:26:36 +00:00
|
|
|
# Content type and section come from request.GET
|
|
|
|
self.content_type = request.GET.get("content_type")
|
|
|
|
self.section = request.GET.get("section")
|
|
|
|
self.section = int(self.section) if self.section else None
|
2016-10-14 23:10:36 +00:00
|
|
|
|
|
|
|
# Reports come from calling the inner view
|
|
|
|
reports = report_view.inner_view(request, self.form, *a, **k)
|
|
|
|
|
|
|
|
# Normalise to a list
|
|
|
|
if isinstance(reports, Report):
|
|
|
|
reports = [reports]
|
|
|
|
|
|
|
|
# Wrap them in appropriate format
|
|
|
|
reports = ReportView.wrap_reports(reports, self.content_type)
|
|
|
|
|
|
|
|
self.reports = reports
|
|
|
|
|
2016-09-02 00:08:11 +00:00
|
|
|
|
|
|
|
def get_all_reports():
|
|
|
|
''' Returns all the views that have been registered with @report '''
|
|
|
|
|
|
|
|
return list(_all_report_views)
|