Overhauled report views to allow easier template customization.
This commit is contained in:
parent
dc44bbac67
commit
1ade594046
|
@ -23,22 +23,19 @@
|
|||
################################################################################
|
||||
|
||||
"""
|
||||
``rattail.pyramid.views.reports`` -- Report Views
|
||||
Report Views
|
||||
"""
|
||||
|
||||
import os
|
||||
import os.path
|
||||
import re
|
||||
|
||||
from .core import View
|
||||
from mako.template import Template
|
||||
|
||||
from pyramid.response import Response
|
||||
|
||||
import edbob
|
||||
from edbob.time import local_time
|
||||
|
||||
import rattail
|
||||
from rattail.pyramid import Session
|
||||
from rattail.db import Vendor, Department, Product, ProductCost
|
||||
|
||||
import re
|
||||
import rattail
|
||||
from edbob.time import local_time
|
||||
from rattail.files import resource_path
|
||||
|
||||
|
||||
|
@ -56,73 +53,28 @@ def get_upc(product):
|
|||
return upc
|
||||
|
||||
|
||||
def inventory_report(request):
|
||||
"""
|
||||
This is the "Inventory Worksheet" report.
|
||||
"""
|
||||
|
||||
departments = Session.query(rattail.Department)
|
||||
|
||||
if request.params.get('department'):
|
||||
department = departments.get(request.params['department'])
|
||||
if department:
|
||||
body = write_inventory_worksheet(request, department)
|
||||
response = Response(content_type='text/html')
|
||||
response.headers['Content-Length'] = len(body)
|
||||
response.headers['Content-Disposition'] = 'attachment; filename=inventory.html'
|
||||
response.text = body
|
||||
return response
|
||||
|
||||
departments = departments.order_by(rattail.Department.name)
|
||||
departments = departments.all()
|
||||
return{'departments': departments}
|
||||
|
||||
|
||||
def write_inventory_worksheet(request, department):
|
||||
"""
|
||||
Generates the Inventory Worksheet report.
|
||||
"""
|
||||
|
||||
def get_products(subdepartment):
|
||||
q = Session.query(rattail.Product)
|
||||
q = q.outerjoin(rattail.Brand)
|
||||
q = q.filter(rattail.Product.subdepartment == subdepartment)
|
||||
if request.params.get('weighted-only'):
|
||||
q = q.filter(rattail.Product.unit_of_measure == rattail.UNIT_OF_MEASURE_POUND)
|
||||
q = q.order_by(rattail.Brand.name, rattail.Product.description)
|
||||
return q.all()
|
||||
|
||||
now = local_time()
|
||||
data = dict(
|
||||
date=now.strftime('%a %d %b %Y'),
|
||||
time=now.strftime('%I:%M %p'),
|
||||
department=department,
|
||||
get_products=get_products,
|
||||
get_upc=get_upc,
|
||||
)
|
||||
|
||||
report = resource_path('rattail.pyramid:reports/inventory_worksheet.mako')
|
||||
template = Template(filename=report)
|
||||
return template.render(**data)
|
||||
|
||||
|
||||
def ordering_report(request):
|
||||
class OrderingWorksheet(View):
|
||||
"""
|
||||
This is the "Ordering Worksheet" report.
|
||||
"""
|
||||
|
||||
if request.params.get('vendor'):
|
||||
vendor = Session.query(rattail.Vendor).get(request.params['vendor'])
|
||||
report_template_path = 'rattail.pyramid:reports/ordering_worksheet.mako'
|
||||
|
||||
upc_getter = staticmethod(get_upc)
|
||||
|
||||
def __call__(self):
|
||||
if self.request.params.get('vendor'):
|
||||
vendor = Session.query(Vendor).get(self.request.params['vendor'])
|
||||
if vendor:
|
||||
departments = []
|
||||
uuids = request.params.get('departments')
|
||||
uuids = self.request.params.get('departments')
|
||||
if uuids:
|
||||
for uuid in uuids.split(','):
|
||||
dept = Session.query(rattail.Department).get(uuid)
|
||||
dept = Session.query(Department).get(uuid)
|
||||
if dept:
|
||||
departments.append(dept)
|
||||
preferred_only = request.params.get('preferred_only') == '1'
|
||||
body = write_ordering_worksheet(vendor, departments, preferred_only)
|
||||
preferred_only = self.request.params.get('preferred_only') == '1'
|
||||
body = self.write_report(vendor, departments, preferred_only)
|
||||
response = Response(content_type='text/html')
|
||||
response.headers['Content-Length'] = len(body)
|
||||
response.headers['Content-Disposition'] = 'attachment; filename=ordering.html'
|
||||
|
@ -130,18 +82,17 @@ def ordering_report(request):
|
|||
return response
|
||||
return {}
|
||||
|
||||
|
||||
def write_ordering_worksheet(vendor, departments, preferred_only):
|
||||
def write_report(self, vendor, departments, preferred_only):
|
||||
"""
|
||||
Rendering engine for the ordering worksheet report.
|
||||
"""
|
||||
|
||||
q = Session.query(rattail.ProductCost)
|
||||
q = q.join(rattail.Product)
|
||||
q = q.filter(rattail.ProductCost.vendor == vendor)
|
||||
q = q.filter(rattail.Product.department_uuid.in_([x.uuid for x in departments]))
|
||||
q = Session.query(ProductCost)
|
||||
q = q.join(Product)
|
||||
q = q.filter(ProductCost.vendor == vendor)
|
||||
q = q.filter(Product.department_uuid.in_([x.uuid for x in departments]))
|
||||
if preferred_only:
|
||||
q = q.filter(rattail.ProductCost.preference == 1)
|
||||
q = q.filter(ProductCost.preference == 1)
|
||||
|
||||
costs = {}
|
||||
for cost in q:
|
||||
|
@ -164,26 +115,83 @@ def write_ordering_worksheet(vendor, departments, preferred_only):
|
|||
cost_sort_key=cost_sort_key,
|
||||
date=now.strftime('%a %d %b %Y'),
|
||||
time=now.strftime('%I:%M %p'),
|
||||
get_upc=get_upc,
|
||||
get_upc=self.upc_getter,
|
||||
rattail=rattail,
|
||||
)
|
||||
|
||||
report = resource_path('rattail.pyramid:reports/ordering_worksheet.mako')
|
||||
report = edbob.config.get('rattail.pyramid', 'report.ordering_worksheet',
|
||||
default=report)
|
||||
report = os.path.abspath(report)
|
||||
template = Template(filename=report)
|
||||
template_path = resource_path(self.report_template_path)
|
||||
template = Template(filename=template_path)
|
||||
return template.render(**data)
|
||||
|
||||
|
||||
def includeme(config):
|
||||
class InventoryWorksheet(View):
|
||||
"""
|
||||
This is the "Inventory Worksheet" report.
|
||||
"""
|
||||
|
||||
config.add_route('reports.inventory', '/reports/inventory')
|
||||
config.add_view(inventory_report,
|
||||
route_name='reports.inventory',
|
||||
renderer='/reports/inventory.mako')
|
||||
report_template_path = 'rattail.pyramid:reports/inventory_worksheet.mako'
|
||||
|
||||
upc_getter = staticmethod(get_upc)
|
||||
|
||||
def __call__(self):
|
||||
"""
|
||||
This is the "Inventory Worksheet" report.
|
||||
"""
|
||||
|
||||
departments = Session.query(Department)
|
||||
|
||||
if self.request.params.get('department'):
|
||||
department = departments.get(self.request.params['department'])
|
||||
if department:
|
||||
body = self.write_report(department)
|
||||
response = Response(content_type='text/html')
|
||||
response.headers['Content-Length'] = len(body)
|
||||
response.headers['Content-Disposition'] = 'attachment; filename=inventory.html'
|
||||
response.text = body
|
||||
return response
|
||||
|
||||
departments = departments.order_by(rattail.Department.name)
|
||||
departments = departments.all()
|
||||
return{'departments': departments}
|
||||
|
||||
def write_report(self, department):
|
||||
"""
|
||||
Generates the Inventory Worksheet report.
|
||||
"""
|
||||
|
||||
def get_products(subdepartment):
|
||||
q = Session.query(rattail.Product)
|
||||
q = q.outerjoin(rattail.Brand)
|
||||
q = q.filter(rattail.Product.subdepartment == subdepartment)
|
||||
if self.request.params.get('weighted-only'):
|
||||
q = q.filter(rattail.Product.unit_of_measure == rattail.UNIT_OF_MEASURE_POUND)
|
||||
q = q.order_by(rattail.Brand.name, rattail.Product.description)
|
||||
return q.all()
|
||||
|
||||
now = local_time()
|
||||
data = dict(
|
||||
date=now.strftime('%a %d %b %Y'),
|
||||
time=now.strftime('%I:%M %p'),
|
||||
department=department,
|
||||
get_products=get_products,
|
||||
get_upc=self.upc_getter,
|
||||
)
|
||||
|
||||
template_path = resource_path(self.report_template_path)
|
||||
template = Template(filename=template_path)
|
||||
return template.render(**data)
|
||||
|
||||
|
||||
def add_routes(config):
|
||||
config.add_route('reports.ordering', '/reports/ordering')
|
||||
config.add_view(ordering_report,
|
||||
route_name='reports.ordering',
|
||||
config.add_route('reports.inventory', '/reports/inventory')
|
||||
|
||||
|
||||
def includeme(config):
|
||||
add_routes(config)
|
||||
|
||||
config.add_view(OrderingWorksheet, route_name='reports.ordering',
|
||||
renderer='/reports/ordering.mako')
|
||||
|
||||
config.add_view(InventoryWorksheet, route_name='reports.inventory',
|
||||
renderer='/reports/inventory.mako')
|
||||
|
|
Loading…
Reference in a new issue