tailbone/tailbone/views/custorders/config.py
2017-02-16 21:33:54 -06:00

89 lines
2.9 KiB
Python

# -*- coding: utf-8 -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2015 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
# more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Customer Order System Configuration Views
"""
from __future__ import unicode_literals
from rattail.db import custorders
import formencode
from pyramid_simpleform import Form
from pyramid.httpexceptions import HTTPFound
from tailbone.db import Session
class ValidHandler(formencode.validators.FancyValidator):
"""
Validator/converter for the customer order handler choice field.
"""
def __init__(self, *args, **kwargs):
super(ValidHandler, self).__init__(*args, **kwargs)
self.registered_handlers = custorders.get_registered_handlers()
def _to_python(self, value, state):
if not value:
return None
handler = self.registered_handlers.get(value)
if not handler:
raise formencode.Invalid("No such customer order handler", value, state)
return handler
class RootConfig(formencode.Schema):
"""
Schema for root system configuration form.
"""
allow_extra_fields = True
filter_extra_fields = True
handler = formencode.Pipe(validators=[
formencode.validators.NotEmpty(),
ValidHandler()])
def config_root(request):
"""
Primary (root) view for customer order system configuration.
"""
handlers = custorders.get_registered_handlers()
handler = custorders.get_current_handler(Session(), handlers)
form = Form(request, schema=RootConfig)
if form.validate():
custorders.set_current_handler(Session(), form.data['handler'])
request.session.flash("Customer order system configuration has been updated.")
return HTTPFound(location=request.route_url('custorders.config'))
return {
'registered_handlers': handlers,
'current_handler': handler,
}
def includeme(config):
config.add_route('custorders.config', '/custorders/config/')
config.add_view(config_root, route_name='custorders.config',
renderer='/custorders/config.mako', permission='admin')