From 23d38604c4464759863a642fedd01b2cfc84a1b2 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Sun, 7 Nov 2021 17:10:33 -0600 Subject: [PATCH] Let handler restrict available invoice parser options --- tailbone/views/purchasing/batch.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/tailbone/views/purchasing/batch.py b/tailbone/views/purchasing/batch.py index 11a891c7..96fe2128 100644 --- a/tailbone/views/purchasing/batch.py +++ b/tailbone/views/purchasing/batch.py @@ -30,7 +30,6 @@ import six from rattail.db import model, api from rattail.time import localtime -from rattail.vendors.invoices import iter_invoice_parsers import colander from deform import widget as dfwidget @@ -219,6 +218,7 @@ class PurchasingBatchView(BatchMasterView): def configure_form(self, f): super(PurchasingBatchView, self).configure_form(f) + model = self.model batch = f.model_instance today = localtime(self.rattail_config).date() use_buefy = self.get_use_buefy() @@ -324,12 +324,23 @@ class PurchasingBatchView(BatchMasterView): # invoice_parser_key if self.creating: - parsers = sorted(iter_invoice_parsers(), key=lambda p: p.display) + kwargs = {} + + if 'vendor_uuid' in self.request.matchdict: + vendor = self.Session.query(model.Vendor).get( + self.request.matchdict['vendor_uuid']) + if vendor: + kwargs['vendor'] = vendor + + parsers = self.handler.get_supported_invoice_parsers(**kwargs) parser_values = [(p.key, p.display) for p in parsers] - parser_values.insert(0, ('', "(please choose)")) + if len(parsers) == 1: + f.set_default('invoice_parser_key', parsers[0].key) + if use_buefy: f.set_widget('invoice_parser_key', dfwidget.SelectWidget(values=parser_values)) else: + parser_values.insert(0, ('', "(please choose)")) f.set_widget('invoice_parser_key', forms.widgets.JQuerySelectWidget(values=parser_values)) else: f.remove_field('invoice_parser_key')