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