Change vendor catalog upload, to allow vendor-less parsers.

The user must provide the vendor if the parser does not; the user cannot
override the vendor if the parser does provide it.
This commit is contained in:
Lance Edgar 2016-01-21 20:05:01 -06:00
parent 0a223ac4f9
commit d6bdc0e87d
3 changed files with 119 additions and 35 deletions

View file

@ -1,3 +1,55 @@
## -*- coding: utf-8 -*- ## -*- coding: utf-8 -*-
<%inherit file="/batch/create.mako" /> <%inherit file="/batch/create.mako" />
<%def name="head_tags()">
${parent.head_tags()}
<script type="text/javascript">
var vendormap = {
% for i, parser in enumerate(parsers, 1):
'${parser.key}': ${parser.vendormap_value|n}${',' if i < len(parsers) else ''}
% endfor
};
$(function() {
if ($('#VendorCatalog--parser_key option:first').is(':selected')) {
$('#VendorCatalog--vendor_uuid-container').hide();
} else {
$('#VendorCatalog--vendor_uuid').val('');
$('#VendorCatalog--vendor_uuid-display').hide();
$('#VendorCatalog--vendor_uuid-display button').show();
$('#VendorCatalog--vendor_uuid-textbox').val('');
$('#VendorCatalog--vendor_uuid-textbox').show();
$('#VendorCatalog--vendor_uuid-container').show();
}
$('#VendorCatalog--parser_key').change(function() {
if ($(this).find('option:first').is(':selected')) {
$('#VendorCatalog--vendor_uuid-container').hide();
} else {
var vendor = vendormap[$(this).val()];
if (vendor) {
$('#VendorCatalog--vendor_uuid').val(vendor.uuid);
$('#VendorCatalog--vendor_uuid-textbox').hide();
$('#VendorCatalog--vendor_uuid-display span:first').text(vendor.name);
$('#VendorCatalog--vendor_uuid-display button').hide();
$('#VendorCatalog--vendor_uuid-display').show();
$('#VendorCatalog--vendor_uuid-container').show();
} else {
$('#VendorCatalog--vendor_uuid').val('');
$('#VendorCatalog--vendor_uuid-display').hide();
$('#VendorCatalog--vendor_uuid-display button').show();
$('#VendorCatalog--vendor_uuid-textbox').val('');
$('#VendorCatalog--vendor_uuid-textbox').show();
$('#VendorCatalog--vendor_uuid-container').show();
$('#VendorCatalog--vendor_uuid-textbox').focus();
}
}
});
});
</script>
</%def>
${parent.body()} ${parent.body()}

View file

@ -1,3 +1,6 @@
## -*- coding: utf-8 -*- ## -*- coding: utf-8 -*-
<%inherit file="/batch/view.mako" /> <%inherit file="/batch/view.mako" />
<%def name="title()">Vendor Catalog: ${batch.vendor}</%def>
${parent.body()} ${parent.body()}

View file

@ -2,7 +2,7 @@
################################################################################ ################################################################################
# #
# Rattail -- Retail Software Framework # Rattail -- Retail Software Framework
# Copyright © 2010-2015 Lance Edgar # Copyright © 2010-2016 Lance Edgar
# #
# This file is part of Rattail. # This file is part of Rattail.
# #
@ -24,21 +24,27 @@
Views for maintaining vendor catalogs 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 import model
from rattail.db.api import get_setting, get_vendor from rattail.db.api import get_setting, get_vendor
from rattail.db.batch.vendorcatalog import VendorCatalog, VendorCatalogRow from rattail.db.batch.vendorcatalog import VendorCatalog, VendorCatalogRow
from rattail.db.batch.vendorcatalog.handler import VendorCatalogHandler 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 from rattail.util import load_object
import formalchemy import formalchemy
from tailbone import forms
from tailbone.db import Session from tailbone.db import Session
from tailbone.views.batch import FileBatchGrid, FileBatchCrud, ProductBatchRowGrid, BatchRowCrud, defaults from tailbone.views.batch import FileBatchGrid, FileBatchCrud, ProductBatchRowGrid, BatchRowCrud, defaults
log = logging.getLogger(__name__)
class VendorCatalogGrid(FileBatchGrid): class VendorCatalogGrid(FileBatchGrid):
""" """
Grid view for vendor catalogs. Grid view for vendor catalogs.
@ -105,39 +111,62 @@ class VendorCatalogCrud(FileBatchCrud):
handler = super(VendorCatalogCrud, self).get_handler() handler = super(VendorCatalogCrud, self).get_handler()
return handler return handler
def configure_fieldset(self, fs): def get_parsers(self):
parsers = sorted(iter_catalog_parsers(), key=lambda p: p.display) if not hasattr(self, 'parsers'):
parser_options = [(p.display, p.key) for p in parsers] self.parsers = sorted(iter_catalog_parsers(), key=lambda p: p.display)
parser_options.insert(0, ("(please choose)", '')) return self.parsers
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 init_batch(self, batch): def configure_fieldset(self, fs):
parser = require_catalog_parser(batch.parser_key) fs.vendor.set(renderer=forms.renderers.VendorFieldRenderer)
vendor = get_vendor(Session, parser.vendor_key) fs.filename.set(label="Catalog File")
if not vendor:
self.request.session.flash("No vendor setting found in database for key: {0}".format(parser.vendor_key)) if self.creating:
return False parser_options = [(p.display, p.key) for p in self.get_parsers()]
batch.vendor = vendor parser_options.insert(0, ("(please choose)", ''))
return True 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): class VendorCatalogRowGrid(ProductBatchRowGrid):