Changeset - fb022bbc7b92
[Not reviewed]
0 2 0
Christopher Neugebauer - 8 years ago 2016-09-01 23:55:29
chrisjrn@gmail.com
Adds a view that shows all reports
2 files changed with 40 insertions and 0 deletions:
0 comments (0 inline, 0 general)
registrasion/staff_views.py
Show inline comments
 
import forms
 
import views
 

	
 
from collections import namedtuple
 

	
 
from django.contrib.auth.decorators import user_passes_test
 
from django.core.urlresolvers import reverse
 
from django.db import models
 
from django.db.models import F, Q
 
from django.db.models import Sum
 
from django.db.models import Case, When, Value
 
from django.http import Http404
 
from django.shortcuts import render
 
from functools import wraps
 

	
 
from models import commerce
 
from models import inventory
 

	
 

	
 
'''
 

	
 
All reports must be viewable by staff only (permissions?)
 

	
...
 
@@ -41,65 +44,101 @@ class Report(object):
 
    @property
 
    def title(self):
 
        ''' Returns the title for this report. '''
 
        return self._title
 

	
 
    @property
 
    def headings(self):
 
        ''' Returns the headings for the table. '''
 
        return self._headings
 

	
 
    @property
 
    def data(self):
 
        ''' Returns the data rows for the table. '''
 
        return self._data
 

	
 

	
 
''' A list of report views objects that can be used to load a list of
 
reports. '''
 
_all_report_views = []
 

	
 

	
 
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)
 
        @user_passes_test(views._staff_only)
 
        def inner_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": title,
 
                "form": form,
 
                "report": report,
 
            }
 

	
 
            return render(request, "registrasion/report.html", ctx)
 

	
 
        # Add this report to the list of reports -- makes this reversable.
 
        _all_report_views.append(inner_view)
 

	
 
        # Return the callable
 
        return inner_view
 
    return _report
 

	
 

	
 
@user_passes_test(views._staff_only)
 
def reports_list(request):
 
    ''' Lists all of the reports currently available. '''
 

	
 
    reports = []
 

	
 
    for report in _all_report_views:
 
        reports.append({
 
            "name" : report.__name__,
 
            "url" : reverse(report),
 
            "description" : report.__doc__,
 
        })
 

	
 
    reports.sort(key=lambda report: report["name"])
 

	
 
    ctx = {
 
        "reports" : reports,
 
    }
 

	
 
    print reports
 

	
 
    return render(request, "registrasion/reports_list.html", ctx)
 

	
 

	
 
# Report functions
 

	
 

	
 
@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. '''
 

	
 
    data = None
 
    headings = None
 

	
 
    products = form.cleaned_data["product"]
 
    categories = form.cleaned_data["category"]
 

	
 
    line_items = commerce.LineItem.objects.filter(
 
        Q(product__in=products) | Q(product__category__in=categories),
 
        invoice__status=commerce.Invoice.STATUS_PAID,
 
    ).select_related("invoice")
 

	
registrasion/urls.py
Show inline comments
...
 
@@ -24,25 +24,26 @@ public = [
 
    url(r"^invoice/([0-9]+)$", invoice, name="invoice"),
 
    url(r"^invoice/([0-9]+)/([A-Z0-9]+)$", invoice, name="invoice"),
 
    url(r"^invoice/([0-9]+)/manual_payment$",
 
        manual_payment, name="manual_payment"),
 
    url(r"^invoice/([0-9]+)/refund$",
 
        refund, name="refund"),
 
    url(r"^invoice_access/([A-Z0-9]+)$", invoice_access,
 
        name="invoice_access"),
 
    url(r"^profile$", edit_profile, name="attendee_edit"),
 
    url(r"^register$", guided_registration, name="guided_registration"),
 
    url(r"^register/([0-9]+)$", guided_registration,
 
        name="guided_registration"),
 
]
 

	
 

	
 
reports = [
 
    url(r"^$", staff_views.reports_list, name="reports_list"),
 
    url(r"^inventory/?$", staff_views.inventory, name="inventory"),
 
    url(r"^items_sold/?$", staff_views.items_sold, name="items_sold"),
 
]
 

	
 

	
 
urlpatterns = [
 
    url(r"^reports/", include(reports)),
 
    url(r"^", include(public))  # This one must go last.
 
]
0 comments (0 inline, 0 general)