Refactor some things, per patterns learned w/ woocommerce

This commit is contained in:
Lance Edgar 2021-01-20 21:52:12 -06:00
parent 32b04334a3
commit 5269aaa610
3 changed files with 56 additions and 20 deletions

View file

@ -1,18 +1,22 @@
## -*- coding: utf-8; -*- ## -*- coding: utf-8; -*-
<%def name="render_xref_button()">
<b-button type="is-primary"
% if core_office_url:
tag="a" href="${core_office_url}" target="_blank"
% else:
disabled title="${core_office_why_no_url}"
% endif
>
View in CORE Office
</b-button>
</%def>
<%def name="render_xref_helper()"> <%def name="render_xref_helper()">
<div class="object-helper"> <div class="object-helper">
<h3>Cross-Reference</h3> <h3>Cross-Reference</h3>
<div class="object-helper-content"> <div class="object-helper-content">
<b-button type="is-primary" ${self.render_xref_button()}
% if core_office_url:
tag="a" href="${core_office_url}" target="_blank"
% else:
disabled title="${core_office_why_no_url}"
% endif
>
View in CORE Office
</b-button>
</div> </div>
</div> </div>
</%def> </%def>

View file

@ -1,13 +1,26 @@
## -*- coding: utf-8; -*- ## -*- coding: utf-8; -*-
<%inherit file="tailbone:templates/products/view.mako" /> <%inherit file="tailbone:templates/products/view.mako" />
<%namespace file="/corepos-util.mako" import="render_xref_helper" /> <%namespace name="corepos" file="/corepos-util.mako" />
<%def name="object_helpers()"> <%def name="object_helpers()">
${parent.object_helpers()} ${parent.object_helpers()}
${render_xref_helper()} ${self.render_xref_helper()}
</%def>
<%def name="render_xref_helper()">
${corepos.render_xref_helper()}
</%def>
<%def name="render_xref_button()">
${corepos.render_xref_button()}
</%def> </%def>
<%def name="extra_main_fields(form)"> <%def name="extra_main_fields(form)">
${parent.extra_main_fields(form)}
${self.extra_main_fields_corepos(form)}
</%def>
<%def name="extra_main_fields_corepos(form)">
${form.render_field_readonly('corepos_id')} ${form.render_field_readonly('corepos_id')}
</%def> </%def>

View file

@ -2,7 +2,7 @@
################################################################################ ################################################################################
# #
# Rattail -- Retail Software Framework # Rattail -- Retail Software Framework
# Copyright © 2010-2020 Lance Edgar # Copyright © 2010-2021 Lance Edgar
# #
# This file is part of Rattail. # This file is part of Rattail.
# #
@ -40,23 +40,35 @@ class ProductView(base.ProductsView):
@property @property
def form_fields(self): def form_fields(self):
fields = super(ProductView, self).form_fields fields = super(ProductView, self).form_fields
return fields + [ return self.corepos_add_form_fields(fields)
def corepos_add_form_fields(self, fields):
fields.extend([
'corepos_id', 'corepos_id',
] ])
return fields
def query(self, session): def query(self, session):
query = super(ProductView, self).query(session) query = super(ProductView, self).query(session)
return self.corepos_modify_query(query)
def corepos_modify_query(self, query):
model = self.rattail_config.get_model() model = self.rattail_config.get_model()
return query.outerjoin(model.CoreProduct) return query.outerjoin(model.CoreProduct)
def configure_grid(self, g): def configure_grid(self, g):
super(ProductView, self).configure_grid(g) super(ProductView, self).configure_grid(g)
self.corepos_configure_grid(g)
def corepos_configure_grid(self, g):
model = self.rattail_config.get_model() model = self.rattail_config.get_model()
g.set_filter('corepos_id', model.CoreProduct.corepos_id) g.set_filter('corepos_id', model.CoreProduct.corepos_id)
def configure_form(self, f): def configure_form(self, f):
super(ProductView, self).configure_form(f) super(ProductView, self).configure_form(f)
self.corepos_configure_form(f)
def corepos_configure_form(self, f):
f.set_required('corepos_id', False) f.set_required('corepos_id', False)
if self.creating: if self.creating:
f.remove('corepos_id') f.remove('corepos_id')
@ -65,28 +77,35 @@ class ProductView(base.ProductsView):
if data is None: if data is None:
data = form.validated data = form.validated
product = super(ProductView, self).objectify(form, data) product = super(ProductView, self).objectify(form, data)
return self.corepos_objectify(product)
def corepos_objectify(self, product):
# remove the corepos extension record outright, if we just lost the ID # remove the corepos extension record outright, if we just lost the ID
if product._corepos and not product.corepos_id: if product._corepos and not product.corepos_id:
self.Session.delete(product._corepos) self.Session.delete(product._corepos)
self.Session.flush() self.Session.flush()
return product return product
def get_version_child_classes(self): def get_version_child_classes(self):
classes = super(ProductView, self).get_version_child_classes()
return self.corepos_add_version_classes(classes)
def corepos_add_version_classes(self, classes):
model = self.rattail_config.get_model() model = self.rattail_config.get_model()
return super(ProductView, self).get_version_child_classes() + [ classes.extend([
model.CoreProduct, model.CoreProduct,
] ])
return classes
def template_kwargs_view(self, **kwargs): def template_kwargs_view(self, **kwargs):
""" kwargs = super(ProductView, self).template_kwargs_view(**kwargs)
Supplements the default logic as follows: return self.corepos_template_kwargs_view(**kwargs)
def corepos_template_kwargs_view(self, **kwargs):
"""
Adds the URL for viewing the product within CORE Office, or else the Adds the URL for viewing the product within CORE Office, or else the
reason for lack of such a URL. reason for lack of such a URL.
""" """
kwargs = super(ProductView, self).template_kwargs_view(**kwargs)
product = kwargs['instance'] product = kwargs['instance']
# CORE Office URL # CORE Office URL