Makes the reporting framework a bit more DRY.

This commit is contained in:
Christopher Neugebauer 2016-08-26 15:11:08 +10:00
parent 3607fb19b8
commit 32b887fed3

View file

@ -31,22 +31,15 @@ A table
class Report(object): class Report(object):
def __init__(self, title, form, headings, data): def __init__(self, title, headings, data):
self._title = title
self._form = form
self._headings = headings self._headings = headings
self._data = data self._data = data
@property @property
def title(self): def title(self):
''' Returns the form. ''' ''' Returns the title for this report. '''
return self._title return self._title
@property
def form(self):
''' Returns the form. '''
return self._form
@property @property
def headings(self): def headings(self):
''' Returns the headings for the table. ''' ''' Returns the headings for the table. '''
@ -58,40 +51,46 @@ class Report(object):
return self._data return self._data
def report(view): def report(title, form_type):
''' Decorator that converts a report view function into something that ''' Decorator that converts a report view function into something that
displays a Report. displays a Report.
Arguments:
form_type: A form class that can make this report display things.
''' '''
def _report(view):
@wraps(view) @wraps(view)
def inner_view(request, *a, **k): def inner_view(request, *a, **k):
report = view(request, *a, **k)
form = form_type(request.GET)
if form.is_valid() and form.has_changed():
report = view(request, form, *a, **k)
else:
report = None
ctx = { ctx = {
"title": report.title, "title": title,
"form": report.form, "form": form,
"report": report, "report": report,
} }
return render(request, "registrasion/report.html", ctx) return render(request, "registrasion/report.html", ctx)
return inner_view return inner_view
return _report
@report @report("Paid items", forms.ProductAndCategoryForm)
def items_sold(request): def items_sold(request, form):
''' Summarises the items sold and discounts granted for a given set of ''' Summarises the items sold and discounts granted for a given set of
products, or products from categories. ''' products, or products from categories. '''
title = "Paid items"
form = forms.ProductAndCategoryForm(request.GET)
data = None data = None
headings = None headings = None
if form.is_valid() and form.has_changed():
products = form.cleaned_data["product"] products = form.cleaned_data["product"]
categories = form.cleaned_data["category"] categories = form.cleaned_data["category"]
@ -127,22 +126,14 @@ def items_sold(request):
"(TOTAL)", "--", "--", total_income, "(TOTAL)", "--", "--", total_income,
]) ])
return Report(title, form, headings, data) return Report("Paid items", headings, data)
@report @report("Inventory", forms.ProductAndCategoryForm)
def inventory(request): def inventory(request, form):
''' Summarises the inventory status of the given items, grouping by ''' Summarises the inventory status of the given items, grouping by
invoice status. ''' invoice status. '''
title = "Inventory"
form = forms.ProductAndCategoryForm(request.GET)
data = None
headings = None
if form.is_valid() and form.has_changed():
products = form.cleaned_data["product"] products = form.cleaned_data["product"]
categories = form.cleaned_data["category"] categories = form.cleaned_data["category"]
@ -177,14 +168,13 @@ def inventory(request):
def status(reserved, status): def status(reserved, status):
r = "Reserved" if reserved else "Unreserved" r = "Reserved" if reserved else "Unreserved"
# This is a bit weird -- can we simplify?
s = "".join( s = "".join(
"%s" % i[1] "%s" % i[1] for i in commerce.Cart.STATUS_TYPES if i[0]==status
for i in commerce.Cart.STATUS_TYPES if i[0]==status
) )
return "%s - %s" % (r, s) return "%s - %s" % (r, s)
for item in items: for item in items:
print commerce.Cart.STATUS_TYPES
data.append([ data.append([
"%s - %s" % ( "%s - %s" % (
item["product__category__name"], item["product__name"] item["product__category__name"], item["product__name"]
@ -193,4 +183,4 @@ def inventory(request):
item["total_quantity"], item["total_quantity"],
]) ])
return Report(title, form, headings, data) return Report("Inventory", headings, data)