diff --git a/tailbone/templates/vendors/catalogs/create.mako b/tailbone/templates/vendors/catalogs/create.mako index 2e46901c..fd7b0c9f 100644 --- a/tailbone/templates/vendors/catalogs/create.mako +++ b/tailbone/templates/vendors/catalogs/create.mako @@ -1,3 +1,55 @@ ## -*- coding: utf-8 -*- <%inherit file="/batch/create.mako" /> + +<%def name="head_tags()"> + ${parent.head_tags()} + + + ${parent.body()} diff --git a/tailbone/templates/vendors/catalogs/view.mako b/tailbone/templates/vendors/catalogs/view.mako index 9b89af91..4bc8b177 100644 --- a/tailbone/templates/vendors/catalogs/view.mako +++ b/tailbone/templates/vendors/catalogs/view.mako @@ -1,3 +1,6 @@ ## -*- coding: utf-8 -*- <%inherit file="/batch/view.mako" /> + +<%def name="title()">Vendor Catalog: ${batch.vendor} + ${parent.body()} diff --git a/tailbone/views/vendors/catalogs.py b/tailbone/views/vendors/catalogs.py index 0d5aa8d9..14a59dea 100644 --- a/tailbone/views/vendors/catalogs.py +++ b/tailbone/views/vendors/catalogs.py @@ -2,7 +2,7 @@ ################################################################################ # # Rattail -- Retail Software Framework -# Copyright © 2010-2015 Lance Edgar +# Copyright © 2010-2016 Lance Edgar # # This file is part of Rattail. # @@ -24,21 +24,27 @@ Views for maintaining vendor catalogs """ -from __future__ import unicode_literals +from __future__ import unicode_literals, absolute_import + +import logging from rattail.db import model from rattail.db.api import get_setting, get_vendor from rattail.db.batch.vendorcatalog import VendorCatalog, VendorCatalogRow from rattail.db.batch.vendorcatalog.handler import VendorCatalogHandler -from rattail.vendors.catalogs import iter_catalog_parsers, require_catalog_parser +from rattail.vendors.catalogs import iter_catalog_parsers from rattail.util import load_object import formalchemy +from tailbone import forms from tailbone.db import Session from tailbone.views.batch import FileBatchGrid, FileBatchCrud, ProductBatchRowGrid, BatchRowCrud, defaults +log = logging.getLogger(__name__) + + class VendorCatalogGrid(FileBatchGrid): """ Grid view for vendor catalogs. @@ -105,39 +111,62 @@ class VendorCatalogCrud(FileBatchCrud): handler = super(VendorCatalogCrud, self).get_handler() return handler - def configure_fieldset(self, fs): - parsers = sorted(iter_catalog_parsers(), key=lambda p: p.display) - parser_options = [(p.display, p.key) for p in parsers] - parser_options.insert(0, ("(please choose)", '')) - fs.parser_key.set(renderer=formalchemy.fields.SelectFieldRenderer, - options=parser_options) - fs.configure( - include=[ - fs.vendor, - fs.filename.label("Catalog File"), - fs.parser_key.label("File Type"), - fs.effective, - fs.created, - fs.created_by, - fs.executed, - fs.executed_by, - ]) - if self.creating: - del fs.vendor - del fs.effective - else: - del fs.parser_key - fs.vendor.set(readonly=True) - fs.effective.set(readonly=True) + def get_parsers(self): + if not hasattr(self, 'parsers'): + self.parsers = sorted(iter_catalog_parsers(), key=lambda p: p.display) + return self.parsers - def init_batch(self, batch): - parser = require_catalog_parser(batch.parser_key) - vendor = get_vendor(Session, parser.vendor_key) - if not vendor: - self.request.session.flash("No vendor setting found in database for key: {0}".format(parser.vendor_key)) - return False - batch.vendor = vendor - return True + def configure_fieldset(self, fs): + fs.vendor.set(renderer=forms.renderers.VendorFieldRenderer) + fs.filename.set(label="Catalog File") + + if self.creating: + parser_options = [(p.display, p.key) for p in self.get_parsers()] + parser_options.insert(0, ("(please choose)", '')) + fs.parser_key.set(renderer=formalchemy.fields.SelectFieldRenderer, + options=parser_options, label="File Type") + + fs.configure( + include=[ + fs.filename, + fs.parser_key, + fs.vendor, + fs.created, + fs.created_by, + fs.executed, + fs.executed_by, + ]) + + else: + fs.configure( + include=[ + fs.vendor.readonly(), + fs.filename, + fs.effective.readonly(), + fs.created, + fs.created_by, + fs.executed, + fs.executed_by, + ]) + + def template_kwargs(self, form): + kwargs = super(VendorCatalogCrud, self).template_kwargs(form) + if self.creating: + parsers = self.get_parsers() + for parser in parsers: + if parser.vendor_key: + vendor = get_vendor(Session(), parser.vendor_key) + if vendor: + parser.vendormap_value = "{{uuid: '{}', name: '{}'}}".format( + vendor.uuid, vendor.name.replace("'", "\\'")) + else: + log.warning("vendor '{}' not found for parser: {}".format( + parser.vendor_key, parser.key)) + parser.vendormap_value = 'null' + else: + parser.vendormap_value = 'null' + kwargs['parsers'] = parsers + return kwargs class VendorCatalogRowGrid(ProductBatchRowGrid):