Let settings determine which batch handler to use for vendor catalog views.

This commit is contained in:
Lance Edgar 2015-01-20 13:22:20 -06:00
parent 7c761bee99
commit c328c96203
2 changed files with 29 additions and 2 deletions

View file

@ -366,7 +366,15 @@ class BatchCrud(BaseCrud):
def __init__(self, request):
self.request = request
self.handler = self.batch_handler_class(config=self.request.rattail_config)
self.handler = self.get_handler()
def get_handler(self):
"""
Returns a `BatchHandler` instance for the view. Derived classes may
override this as needed. The default is to create an instance of
:attr:`batch_handler_class`.
"""
return self.batch_handler_class(self.request.rattail_config)
def fieldset(self, model):
"""

View file

@ -27,10 +27,11 @@ Views for maintaining vendor catalogs
from __future__ import unicode_literals
from rattail.db import model
from rattail.db.api import get_vendor
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.util import load_object
import formalchemy
@ -84,6 +85,24 @@ class VendorCatalogCrud(FileBatchCrud):
flash = {'create': "New vendor catalog has been uploaded.",
'delete': "Vendor catalog has been deleted."}
def get_handler(self):
"""
Returns a `BatchHandler` instance for the view.
Derived classes may override this, but if you only need to replace the
handler (i.e. and not the view logic) then you can instead subclass
:class:`rattail.db.batch.vendorcatalog.handler.VendorCatalogHandler`
and create a setting named "rattail.batch.vendorcatalog.handler" in the
database, the value of which should be a spec string pointed at your
custom handler.
"""
handler = get_setting(Session, 'rattail.batch.vendorcatalog.handler')
if handler:
handler = load_object(handler)(self.request.rattail_config)
if not handler:
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]