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; -*-
<%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()">
<div class="object-helper">
<h3>Cross-Reference</h3>
<div class="object-helper-content">
<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>
${self.render_xref_button()}
</div>
</div>
</%def>

View file

@ -1,13 +1,26 @@
## -*- coding: utf-8; -*-
<%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()">
${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 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')}
</%def>

View file

@ -2,7 +2,7 @@
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2020 Lance Edgar
# Copyright © 2010-2021 Lance Edgar
#
# This file is part of Rattail.
#
@ -40,23 +40,35 @@ class ProductView(base.ProductsView):
@property
def form_fields(self):
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',
]
])
return fields
def query(self, 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()
return query.outerjoin(model.CoreProduct)
def configure_grid(self, g):
super(ProductView, self).configure_grid(g)
self.corepos_configure_grid(g)
def corepos_configure_grid(self, g):
model = self.rattail_config.get_model()
g.set_filter('corepos_id', model.CoreProduct.corepos_id)
def configure_form(self, f):
super(ProductView, self).configure_form(f)
self.corepos_configure_form(f)
def corepos_configure_form(self, f):
f.set_required('corepos_id', False)
if self.creating:
f.remove('corepos_id')
@ -65,28 +77,35 @@ class ProductView(base.ProductsView):
if data is None:
data = form.validated
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
if product._corepos and not product.corepos_id:
self.Session.delete(product._corepos)
self.Session.flush()
return product
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()
return super(ProductView, self).get_version_child_classes() + [
classes.extend([
model.CoreProduct,
]
])
return classes
def template_kwargs_view(self, **kwargs):
"""
Supplements the default logic as follows:
kwargs = super(ProductView, self).template_kwargs_view(**kwargs)
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
reason for lack of such a URL.
"""
kwargs = super(ProductView, self).template_kwargs_view(**kwargs)
product = kwargs['instance']
# CORE Office URL