From e3b1be583545a3480a7c79478ac994c867391264 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Sun, 15 May 2022 16:04:22 -0500 Subject: [PATCH] Expose config for identifying supported vendors unfortunately must identify vendors at each app node separately, but this is definitely still an improvement.. --- tailbone/templates/vendors/configure.mako | 35 ++++++++++++++ tailbone/views/master.py | 15 ++++++ tailbone/views/vendors/core.py | 57 +++++++++++++++++++++++ 3 files changed, 107 insertions(+) diff --git a/tailbone/templates/vendors/configure.mako b/tailbone/templates/vendors/configure.mako index cb370e43..79dad455 100644 --- a/tailbone/templates/vendors/configure.mako +++ b/tailbone/templates/vendors/configure.mako @@ -16,6 +16,41 @@ + +

Supported Vendors

+
+ +

+ The following vendor "keys" are defined within various places in + the software.  You must identify each explicitly with a + Vendor record, for things to work as designed. +

+ + + + + + + +
+ + +<%def name="modify_this_page_vars()"> + ${parent.modify_this_page_vars()} + diff --git a/tailbone/views/master.py b/tailbone/views/master.py index 83f77b69..e7dc7c64 100644 --- a/tailbone/views/master.py +++ b/tailbone/views/master.py @@ -4359,6 +4359,21 @@ class MasterView(View): def configure_get_context(self, simple_settings=None, input_file_templates=True): + """ + Returns the full context dict, for rendering the configure + page template. + + Default context will include the "simple" settings, as well as + any "input file template" settings. + + You may need to override this method, to add additional + "custom" settings. + + :param simple_settings: Optional list of simple settings, if + already initialized. + + :returns: Context dict for the page template. + """ context = {} if simple_settings is None: simple_settings = self.configure_get_simple_settings() diff --git a/tailbone/views/vendors/core.py b/tailbone/views/vendors/core.py index b3b003e7..9f964d2c 100644 --- a/tailbone/views/vendors/core.py +++ b/tailbone/views/vendors/core.py @@ -33,6 +33,7 @@ from rattail.db import model from webhelpers2.html import tags from tailbone.views import MasterView +from tailbone.db import Session class VendorView(MasterView): @@ -179,6 +180,62 @@ class VendorView(MasterView): 'type': bool}, ] + def configure_get_context(self, **kwargs): + context = super(VendorView, self).configure_get_context(**kwargs) + + context['supported_vendor_settings'] = self.configure_get_supported_vendor_settings() + + return context + + def configure_gather_settings(self, data, **kwargs): + settings = super(VendorView, self).configure_gather_settings( + data, **kwargs) + + supported_vendor_settings = self.configure_get_supported_vendor_settings() + for setting in six.itervalues(supported_vendor_settings): + name = 'rattail.vendor.{}'.format(setting['key']) + settings.append({'name': name, + 'value': data[name]}) + + return settings + + def configure_remove_settings(self, **kwargs): + super(VendorView, self).configure_remove_settings(**kwargs) + + model = self.model + names = [] + + supported_vendor_settings = self.configure_get_supported_vendor_settings() + for setting in six.itervalues(supported_vendor_settings): + names.append('rattail.vendor.{}'.format(setting['key'])) + + if names: + # nb. we do not use self.Session b/c that may not point to + # the Rattail DB for the subclass + Session().query(model.Setting)\ + .filter(model.Setting.name.in_(names))\ + .delete(synchronize_session=False) + + def configure_get_supported_vendor_settings(self): + app = self.get_rattail_app() + vendor_handler = app.get_vendor_handler() + batch_handler = app.get_batch_handler('purchase') + settings = {} + + for parser in batch_handler.get_supported_invoice_parsers(): + key = parser.vendor_key + if not key: + continue + + vendor = vendor_handler.get_vendor(self.Session(), key) + settings[key] = { + 'key': key, + 'value': vendor.uuid if vendor else None, + 'label': six.text_type(vendor) if vendor else None, + } + + return settings + def defaults(config, **kwargs): base = globals()