Add basic ability to download Ordering Batch as Excel spreadsheet
This commit is contained in:
parent
b841ce664e
commit
522aad5880
1
setup.py
1
setup.py
|
@ -83,6 +83,7 @@ requires = [
|
|||
|
||||
'humanize', # 0.5.1
|
||||
'Mako', # 0.6.2
|
||||
'openpyxl', # 2.4.7
|
||||
'pyramid_beaker>=0.6', # 0.6.1
|
||||
'pyramid_debugtoolbar', # 1.0
|
||||
'pyramid_exclog', # 0.6
|
||||
|
|
|
@ -27,6 +27,13 @@
|
|||
${h.stylesheet_link(request.static_url('tailbone:static/css/purchases.css'))}
|
||||
</%def>
|
||||
|
||||
<%def name="context_menu_items()">
|
||||
${parent.context_menu_items()}
|
||||
% if request.has_perm('{}.download_excel'.format(permission_prefix)):
|
||||
<li>${h.link_to("Download {} as Excel".format(model_title), url('{}.download_excel'.format(route_prefix), uuid=batch.uuid))}</li>
|
||||
% endif
|
||||
</%def>
|
||||
|
||||
<%def name="leading_buttons()">
|
||||
% if not batch.complete and not batch.executed and request.has_perm('ordering.order_form'):
|
||||
<button type="button" id="order-form">Ordering Form</button>
|
||||
|
|
|
@ -26,12 +26,18 @@ Views for 'ordering' (purchasing) batches
|
|||
|
||||
from __future__ import unicode_literals, absolute_import
|
||||
|
||||
import os
|
||||
|
||||
import six
|
||||
import openpyxl
|
||||
from sqlalchemy import orm
|
||||
|
||||
from rattail.db import model, api
|
||||
from rattail.core import Object
|
||||
from rattail.time import localtime
|
||||
|
||||
from pyramid.response import FileResponse
|
||||
|
||||
from tailbone import forms
|
||||
from tailbone.views.purchasing import PurchasingBatchView
|
||||
|
||||
|
@ -218,6 +224,37 @@ class OrderingBatchView(PurchasingBatchView):
|
|||
'batch_po_total': '${:0,.2f}'.format(batch.po_total or 0),
|
||||
}
|
||||
|
||||
def download_excel(self):
|
||||
"""
|
||||
Download ordering batch as Excel spreadsheet.
|
||||
"""
|
||||
batch = self.get_instance()
|
||||
|
||||
# populate Excel worksheet
|
||||
workbook = openpyxl.Workbook()
|
||||
worksheet = workbook.active
|
||||
worksheet.title = "Purchase Order"
|
||||
worksheet.append(["Store", "Vendor", "Date ordered"])
|
||||
worksheet.append([batch.store.name, batch.vendor.name, batch.date_ordered.strftime('%m/%d/%Y')])
|
||||
worksheet.append([])
|
||||
worksheet.append(['vendor_code', 'upc', 'brand_name', 'description', 'case_quantity', 'cases_ordered', 'units_ordered'])
|
||||
for row in batch.active_rows():
|
||||
worksheet.append([row.vendor_code, six.text_type(row.upc), row.brand_name,
|
||||
'{} {}'.format(row.description, row.size),
|
||||
row.case_quantity, row.cases_ordered, row.units_ordered])
|
||||
|
||||
# write Excel file to batch data dir
|
||||
filedir = batch.filedir(self.rattail_config)
|
||||
if not os.path.exists(filedir):
|
||||
os.makedirs(filedir)
|
||||
filename = 'PO.{}.xlsx'.format(batch.id_str)
|
||||
path = batch.filepath(self.rattail_config, filename)
|
||||
workbook.save(path)
|
||||
|
||||
response = FileResponse(path, request=self.request)
|
||||
response.content_length = os.path.getsize(path)
|
||||
response.content_disposition = b'attachment; filename="{}"'.format(filename)
|
||||
return response
|
||||
|
||||
@classmethod
|
||||
def defaults(cls, config):
|
||||
|
@ -242,6 +279,13 @@ class OrderingBatchView(PurchasingBatchView):
|
|||
config.add_view(cls, attr='order_form_update', route_name='{}.order_form_update'.format(route_prefix),
|
||||
renderer='json', permission='{}.order_form'.format(permission_prefix))
|
||||
|
||||
# download as Excel
|
||||
config.add_route('{}.download_excel'.format(route_prefix), '{}/{{uuid}}/excel'.format(url_prefix))
|
||||
config.add_view(cls, attr='download_excel', route_name='{}.download_excel'.format(route_prefix),
|
||||
permission='{}.download_excel'.format(permission_prefix))
|
||||
config.add_tailbone_permission(permission_prefix, '{}.download_excel'.format(permission_prefix),
|
||||
"Download {} as Excel".format(model_title))
|
||||
|
||||
|
||||
def includeme(config):
|
||||
OrderingBatchView.defaults(config)
|
||||
|
|
Loading…
Reference in a new issue