Refactor purchases view to use master3

This commit is contained in:
Lance Edgar 2017-12-05 18:52:54 -06:00
parent 8041c085f6
commit bb12c5107c

View file

@ -33,37 +33,7 @@ from webhelpers2.html import HTML, tags
from tailbone import forms from tailbone import forms
from tailbone.db import Session from tailbone.db import Session
from tailbone.views import MasterView2 as MasterView from tailbone.views import MasterView3 as MasterView
class BatchesFieldRenderer(fa.FieldRenderer):
def render_readonly(self, **kwargs):
batches = self.raw_value
if not batches:
return ''
enum = self.request.rattail_config.get_enum()
routes = {
enum.PURCHASE_BATCH_MODE_ORDERING: 'ordering.view',
enum.PURCHASE_BATCH_MODE_RECEIVING: 'receiving.view',
}
def render(batch):
if batch.executed:
actor = batch.executed_by
pending = ''
else:
actor = batch.created_by
pending = ' (pending)'
display = '{} ({} by {}){}'.format(batch.id_str,
enum.PURCHASE_BATCH_MODE[batch.mode],
actor, pending)
return tags.link_to(display, self.request.route_url(routes[batch.mode], uuid=batch.uuid))
items = [HTML.tag('li', c=render(batch)) for batch in batches]
return HTML.tag('ul', c=items)
class PurchaseView(MasterView): class PurchaseView(MasterView):
@ -89,6 +59,24 @@ class PurchaseView(MasterView):
'status', 'status',
] ]
form_fields = [
'store',
'vendor',
'department',
'status',
'buyer',
'date_ordered',
'date_received',
'po_number',
'po_total',
'invoice_date',
'invoice_number',
'invoice_total',
'created',
'created_by',
'batches',
]
row_grid_columns = [ row_grid_columns = [
'sequence', 'sequence',
'upc', 'upc',
@ -154,42 +142,83 @@ class PurchaseView(MasterView):
g.set_label('date_received', "Received") g.set_label('date_received', "Received")
g.set_label('invoice_number', "Invoice No.") g.set_label('invoice_number', "Invoice No.")
def _preconfigure_fieldset(self, fs): def configure_form(self, f):
fs.store.set(renderer=forms.renderers.StoreFieldRenderer) super(PurchaseView, self).configure_form(f)
fs.vendor.set(renderer=forms.renderers.VendorFieldRenderer)
fs.department.set(renderer=forms.renderers.DepartmentFieldRenderer) f.set_renderer('store', self.render_store)
fs.status.set(renderer=forms.renderers.EnumFieldRenderer(self.enum.PURCHASE_STATUS), f.set_renderer('vendor', self.render_vendor)
readonly=True) f.set_renderer('department', self.render_department)
fs.po_number.set(label="PO Number")
fs.po_total.set(label="PO Total", renderer=forms.renderers.CurrencyFieldRenderer) f.set_readonly('status')
fs.invoice_total.set(renderer=forms.renderers.CurrencyFieldRenderer) f.set_enum('status', self.enum.PURCHASE_STATUS)
fs.batches.set(renderer=BatchesFieldRenderer)
f.set_label('po_number', "PO Number")
f.set_label('po_total', "PO Total")
f.set_type('po_total', 'currency')
f.set_type('invoice_total', 'currency')
f.set_renderer('batches', self.render_batches)
def configure_fieldset(self, fs):
fs.configure(
include=[
fs.store,
fs.vendor,
fs.department,
fs.status,
fs.buyer,
fs.date_ordered,
fs.date_received,
fs.po_number,
fs.po_total,
fs.invoice_date,
fs.invoice_number,
fs.invoice_total,
fs.created,
fs.created_by,
fs.batches,
])
if self.viewing: if self.viewing:
purchase = fs.model purchase = f.model_instance
if purchase.status == self.enum.PURCHASE_STATUS_ORDERED: if purchase.status == self.enum.PURCHASE_STATUS_ORDERED:
del fs.date_received f.remove('date_received',
del fs.invoice_number 'invoice_number',
del fs.invoice_total 'invoice_total')
def render_store(self, purchase, field):
store = purchase.store
if not store:
return ""
text = "({}) {}".format(store.id, store.name)
url = self.request.route_url('stores.view', uuid=store.uuid)
return tags.link_to(text, url)
def render_vendor(self, purchase, field):
vendor = purchase.vendor
if not vendor:
return ""
text = "({}) {}".format(vendor.id, vendor.name)
url = self.request.route_url('vendors.view', uuid=vendor.uuid)
return tags.link_to(text, url)
def render_department(self, purchase, field):
department = purchase.department
if not department:
return ""
if department.number:
text = "({}) {}".format(department.number, department.name)
else:
text = department.name
url = self.request.route_url('departments.view', uuid=department.uuid)
return tags.link_to(text, url)
def render_batches(self, purchase, field):
batches = purchase.batches
if not batches:
return ""
routes = {
self.enum.PURCHASE_BATCH_MODE_ORDERING: 'ordering.view',
self.enum.PURCHASE_BATCH_MODE_RECEIVING: 'receiving.view',
}
def render(batch):
if batch.executed:
actor = batch.executed_by
pending = ''
else:
actor = batch.created_by
pending = ' (pending)'
text = '{} ({} by {}){}'.format(batch.id_str,
self.enum.PURCHASE_BATCH_MODE[batch.mode],
actor, pending)
url = self.request.route_url(routes[batch.mode], uuid=batch.uuid)
return tags.link_to(text, url)
items = [HTML.tag('li', c=render(batch)) for batch in batches]
return HTML.tag('ul', c=items)
def delete_instance(self, purchase): def delete_instance(self, purchase):
""" """