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