Improve basic support for unit/pack info when viewing product details

This commit is contained in:
Lance Edgar 2018-07-08 00:01:14 -05:00
parent 9149902c78
commit 3dfdb26502
3 changed files with 55 additions and 14 deletions

View file

@ -728,6 +728,8 @@ class Form(object):
return True return True
def render_field_readonly(self, field_name, **kwargs): def render_field_readonly(self, field_name, **kwargs):
if field_name not in self.fields:
return ''
label = HTML.tag('label', self.get_label(field_name), for_=field_name) label = HTML.tag('label', self.get_label(field_name), for_=field_name)
field = self.render_field_value(field_name) or '' field = self.render_field_value(field_name) or ''
field_div = HTML.tag('div', class_='field', c=[field]) field_div = HTML.tag('div', class_='field', c=[field])

View file

@ -68,12 +68,14 @@
${form.render_field_readonly('size')} ${form.render_field_readonly('size')}
${form.render_field_readonly('unit_size')} ${form.render_field_readonly('unit_size')}
${form.render_field_readonly('unit_of_measure')} ${form.render_field_readonly('unit_of_measure')}
% if instance.is_pack_item():
${form.render_field_readonly('unit')}
${form.render_field_readonly('pack_size')}
${form.render_field_readonly('default_pack')}
% endif
${form.render_field_readonly('case_size')} ${form.render_field_readonly('case_size')}
% if instance.is_pack_item():
${form.render_field_readonly('pack_size')}
${form.render_field_readonly('unit')}
${form.render_field_readonly('default_pack')}
% elif instance.packs:
${form.render_field_readonly('packs')}
% endif
${self.extra_main_fields(form)} ${self.extra_main_fields(form)}
</%def> </%def>

View file

@ -102,8 +102,9 @@ class ProductsView(MasterView):
'unit_size', 'unit_size',
'unit_of_measure', 'unit_of_measure',
'size', 'size',
'unit', 'packs',
'pack_size', 'pack_size',
'unit',
'default_pack', 'default_pack',
'case_size', 'case_size',
'weighed', 'weighed',
@ -510,14 +511,23 @@ class ProductsView(MasterView):
f.set_enum('unit_of_measure', self.enum.UNIT_OF_MEASURE) f.set_enum('unit_of_measure', self.enum.UNIT_OF_MEASURE)
f.set_label('unit_of_measure', "Unit of Measure") f.set_label('unit_of_measure', "Unit of Measure")
# packs
if self.creating:
f.remove_field('packs')
elif self.viewing and product.packs:
f.set_renderer('packs', self.render_packs)
f.set_label('packs', "Pack Items")
else:
f.remove_field('packs')
# unit # unit
if self.creating: if self.creating:
f.remove_field('unit') f.remove_field('unit')
elif self.viewing and not product.is_pack_item(): elif self.viewing and product.is_pack_item():
f.remove_field('unit')
else:
f.set_renderer('unit', self.render_unit) f.set_renderer('unit', self.render_unit)
f.set_label('unit', "Unit Item") f.set_label('unit', "Unit Item")
else:
f.remove_field('unit')
# pack_size # pack_size
if self.viewing and not product.is_pack_item(): if self.viewing and not product.is_pack_item():
@ -619,12 +629,39 @@ class ProductsView(MasterView):
url = self.request.route_url('categories.view', uuid=category.uuid) url = self.request.route_url('categories.view', uuid=category.uuid)
return tags.link_to(text, url) return tags.link_to(text, url)
def render_unit(self, product, field): def render_packs(self, product, field):
product = product.unit if product.is_pack_item():
if not product:
return "" return ""
text = product.full_description
url = self.request.route_url('products.view', uuid=product.uuid) links = []
for pack in product.packs:
if pack.upc:
code = pack.upc.pretty()
elif pack.scancode:
code = pack.scancode
else:
code = pack.item_id
text = "({}) {}".format(code, pack.full_description)
url = self.get_action_url('view', pack)
links.append(tags.link_to(text, url))
items = [HTML.tag('li', c=[link]) for link in links]
return HTML.tag('ul', c=items)
def render_unit(self, product, field):
unit = product.unit
if not unit:
return ""
if unit.upc:
code = unit.upc.pretty()
elif unit.scancode:
code = unit.scancode
else:
code = unit.item_id
text = "({}) {}".format(code, unit.full_description)
url = self.get_action_url('view', unit)
return tags.link_to(text, url) return tags.link_to(text, url)
def render_current_price_ends(self, product, field): def render_current_price_ends(self, product, field):