Refactor purchases view to use master3
This commit is contained in:
parent
8041c085f6
commit
bb12c5107c
|
@ -33,37 +33,7 @@ from webhelpers2.html import HTML, tags
|
|||
|
||||
from tailbone import forms
|
||||
from tailbone.db import Session
|
||||
from tailbone.views import MasterView2 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)
|
||||
from tailbone.views import MasterView3 as MasterView
|
||||
|
||||
|
||||
class PurchaseView(MasterView):
|
||||
|
@ -89,6 +59,24 @@ class PurchaseView(MasterView):
|
|||
'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 = [
|
||||
'sequence',
|
||||
'upc',
|
||||
|
@ -154,42 +142,83 @@ class PurchaseView(MasterView):
|
|||
g.set_label('date_received', "Received")
|
||||
g.set_label('invoice_number', "Invoice No.")
|
||||
|
||||
def _preconfigure_fieldset(self, fs):
|
||||
fs.store.set(renderer=forms.renderers.StoreFieldRenderer)
|
||||
fs.vendor.set(renderer=forms.renderers.VendorFieldRenderer)
|
||||
fs.department.set(renderer=forms.renderers.DepartmentFieldRenderer)
|
||||
fs.status.set(renderer=forms.renderers.EnumFieldRenderer(self.enum.PURCHASE_STATUS),
|
||||
readonly=True)
|
||||
fs.po_number.set(label="PO Number")
|
||||
fs.po_total.set(label="PO Total", renderer=forms.renderers.CurrencyFieldRenderer)
|
||||
fs.invoice_total.set(renderer=forms.renderers.CurrencyFieldRenderer)
|
||||
fs.batches.set(renderer=BatchesFieldRenderer)
|
||||
def configure_form(self, f):
|
||||
super(PurchaseView, self).configure_form(f)
|
||||
|
||||
f.set_renderer('store', self.render_store)
|
||||
f.set_renderer('vendor', self.render_vendor)
|
||||
f.set_renderer('department', self.render_department)
|
||||
|
||||
f.set_readonly('status')
|
||||
f.set_enum('status', self.enum.PURCHASE_STATUS)
|
||||
|
||||
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:
|
||||
purchase = fs.model
|
||||
purchase = f.model_instance
|
||||
if purchase.status == self.enum.PURCHASE_STATUS_ORDERED:
|
||||
del fs.date_received
|
||||
del fs.invoice_number
|
||||
del fs.invoice_total
|
||||
f.remove('date_received',
|
||||
'invoice_number',
|
||||
'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):
|
||||
"""
|
||||
|
|
Loading…
Reference in a new issue