Refactor vendor invoice batch views to use master3

This commit is contained in:
Lance Edgar 2018-02-03 12:37:24 -06:00
parent 97c5e97ccb
commit 33931b4bf2
3 changed files with 89 additions and 50 deletions

View file

@ -63,7 +63,7 @@
<%def name="refresh_button()"> <%def name="refresh_button()">
% if master.viewing and master.batch_refreshable(batch) and request.has_perm('{}.refresh'.format(permission_prefix)): % if master.viewing and master.batch_refreshable(batch) and request.has_perm('{}.refresh'.format(permission_prefix)):
<button type="button" id="refresh-data">Refresh data</button> <button type="button" id="refresh-data">Refresh Data</button>
% endif % endif
</%def> </%def>

View file

@ -492,7 +492,7 @@ class BatchMasterView(MasterView):
def get_execute_title(self, batch): def get_execute_title(self, batch):
if hasattr(self.handler, 'get_execute_title'): if hasattr(self.handler, 'get_execute_title'):
return self.handler.get_execute_title(batch) return self.handler.get_execute_title(batch)
return "Execute this batch" return "Execute Batch"
def prefill(self): def prefill(self):
""" """

View file

@ -2,7 +2,7 @@
################################################################################ ################################################################################
# #
# Rattail -- Retail Software Framework # Rattail -- Retail Software Framework
# Copyright © 2010-2017 Lance Edgar # Copyright © 2010-2018 Lance Edgar
# #
# This file is part of Rattail. # This file is part of Rattail.
# #
@ -29,10 +29,11 @@ from __future__ import unicode_literals, absolute_import
from rattail.db import model, api from rattail.db import model, api
from rattail.vendors.invoices import iter_invoice_parsers, require_invoice_parser from rattail.vendors.invoices import iter_invoice_parsers, require_invoice_parser
import formalchemy # import formalchemy
import colander
from deform import widget as dfwidget
from tailbone.db import Session from tailbone.views.batch import FileBatchMasterView3 as FileBatchMasterView
from tailbone.views.batch import FileBatchMasterView2 as FileBatchMasterView
class VendorInvoicesView(FileBatchMasterView): class VendorInvoicesView(FileBatchMasterView):
@ -45,11 +46,27 @@ class VendorInvoicesView(FileBatchMasterView):
url_prefix = '/vendors/invoices' url_prefix = '/vendors/invoices'
grid_columns = [ grid_columns = [
'id',
'vendor',
'invoice_date',
'purchase_order_number',
'filename',
'created', 'created',
'created_by', 'created_by',
'executed',
]
form_fields = [
'id',
'vendor', 'vendor',
'filename', 'filename',
'parser_key',
'purchase_order_number',
'invoice_date',
'created',
'created_by',
'executed', 'executed',
'executed_by',
] ]
row_grid_columns = [ row_grid_columns = [
@ -70,56 +87,78 @@ class VendorInvoicesView(FileBatchMasterView):
def configure_grid(self, g): def configure_grid(self, g):
super(VendorInvoicesView, self).configure_grid(g) super(VendorInvoicesView, self).configure_grid(g)
g.joiners['vendor'] = lambda q: q.join(model.Vendor)
g.filters['vendor'] = g.make_filter('vendor', model.Vendor.name,
default_active=True, default_verb='contains')
g.sorters['vendor'] = g.make_sorter(model.Vendor.name)
def configure_fieldset(self, fs): # vendor
fs.purchase_order_number.set(label=self.handler.po_number_title) g.set_joiner('vendor', lambda q: q.join(model.Vendor))
fs.purchase_order_number.set(validate=self.validate_po_number) g.set_filter('vendor', model.Vendor.name,
fs.filename.set(label="Invoice File") default_active=True,
default_verb='contains')
g.set_sorter('vendor', model.Vendor.name)
g.set_link('vendor')
fs.configure( # invoice_date
include=[ g.set_link('invoice_date')
fs.vendor.readonly(),
fs.filename,
fs.parser_key,
fs.purchase_order_number,
fs.invoice_date.readonly(),
fs.created,
fs.created_by,
fs.executed,
fs.executed_by,
])
# purchase_order_number
g.set_link('purchase_order_number')
# filename
g.set_link('filename')
# created
g.set_link('created', False)
# executed
g.set_link('executed', False)
def configure_form(self, f):
super(VendorInvoicesView, self).configure_form(f)
# vendor
if self.creating:
f.remove_field('vendor')
else:
f.set_readonly('vendor')
# purchase_order_number
f.set_label('purchase_order_number', self.handler.po_number_title)
# f.set_validator('purchase_order_number', self.validate_po_number)
# filename
f.set_label('filename', "Invoice File")
# invoice_date
if self.creating:
f.remove_field('invoice_date')
else:
f.set_readonly('invoice_date')
# parser_key
if self.creating: if self.creating:
parsers = sorted(iter_invoice_parsers(), key=lambda p: p.display) parsers = sorted(iter_invoice_parsers(), key=lambda p: p.display)
parser_options = [(p.display, p.key) for p in parsers] parser_values = [(p.key, p.display) for p in parsers]
parser_options.insert(0, ("(please choose)", '')) parser_values.insert(0, ('', "(please choose)"))
fs.parser_key.set(label="File Type", f.set_widget('parser_key', dfwidget.SelectWidget(values=parser_values))
renderer=formalchemy.fields.SelectFieldRenderer, f.set_label('parser_key', "File Type")
options=parser_options)
del fs.vendor
del fs.invoice_date
else: else:
del fs.parser_key f.remove_field('parser_key')
def validate_po_number(self, value, field): # TODO: this is not needed for now, and this whole thing should be merged
""" # with the Purchasing Batch system anyway...
Let the invoice handler in effect determine if the user-provided # def validate_po_number(self, value, field):
purchase order number is valid. # """
""" # Let the invoice handler in effect determine if the user-provided
parser_key = field.parent.parser_key.value # purchase order number is valid.
if not parser_key: # """
raise formalchemy.ValidationError("Cannot validate PO number until File Type is chosen") # parser_key = field.parent.parser_key.value
parser = require_invoice_parser(parser_key) # if not parser_key:
vendor = api.get_vendor(Session(), parser.vendor_key) # raise formalchemy.ValidationError("Cannot validate PO number until File Type is chosen")
try: # parser = require_invoice_parser(parser_key)
self.handler.validate_po_number(value, vendor) # vendor = api.get_vendor(self.Session(), parser.vendor_key)
except ValueError as error: # try:
raise formalchemy.ValidationError(unicode(error)) # self.handler.validate_po_number(value, vendor)
# except ValueError as error:
# raise formalchemy.ValidationError(unicode(error))
def get_batch_kwargs(self, batch): def get_batch_kwargs(self, batch):
kwargs = super(VendorInvoicesView, self).get_batch_kwargs(batch) kwargs = super(VendorInvoicesView, self).get_batch_kwargs(batch)
@ -128,7 +167,7 @@ class VendorInvoicesView(FileBatchMasterView):
def init_batch(self, batch): def init_batch(self, batch):
parser = require_invoice_parser(batch.parser_key) parser = require_invoice_parser(batch.parser_key)
vendor = api.get_vendor(Session(), parser.vendor_key) vendor = api.get_vendor(self.Session(), parser.vendor_key)
if not vendor: if not vendor:
self.request.session.flash("No vendor setting found in database for key: {}".format(parser.vendor_key)) self.request.session.flash("No vendor setting found in database for key: {}".format(parser.vendor_key))
return False return False