Add basic Buefy support for App Settings page

also various buttons have been tweaked on some other "master view" pages
This commit is contained in:
Lance Edgar 2019-04-17 14:55:27 -05:00
parent e1ff4578e9
commit fcfc8b56bb
13 changed files with 245 additions and 52 deletions

View file

@ -73,6 +73,25 @@ class View(object):
def notfound(self):
return httpexceptions.HTTPNotFound()
def get_use_buefy(self):
"""
Returns a flag indicating whether or not the current theme supports
(and therefore should use) the Buefy JS library.
"""
# first check theme-specific setting, if one has been defined
theme = self.request.registry.settings['tailbone.theme']
buefy = self.rattail_config.getbool('tailbone', 'themes.{}.use_buefy'.format(theme))
if buefy is not None:
return buefy
# TODO: should not hard-code this surely, but works for now...
if theme == 'falafel':
return True
# TODO: probably should not use this fallback? it was the first setting
# i tested with, but is poorly named to say the least
return self.rattail_config.getbool('tailbone', 'grids.use_buefy', default=False)
def late_login_user(self):
"""
Returns the :class:`rattail:rattail.db.model.User` instance

View file

@ -33,7 +33,6 @@ import sqlalchemy as sa
from sqlalchemy import orm
import colander
from deform import widget as dfwidget
from pyramid.httpexceptions import HTTPNotFound
from webhelpers2.html import HTML, tags
@ -220,7 +219,7 @@ class CustomersView(MasterView):
f.set_enum('email_preference', self.enum.EMAIL_PREFERENCE)
preferences = list(self.enum.EMAIL_PREFERENCE.items())
preferences.insert(0, ('', "(no preference)"))
f.set_widget('email_preference', dfwidget.SelectWidget(values=preferences))
f.widgets['email_preference'].values = preferences
# person
if self.creating:

View file

@ -251,25 +251,6 @@ class MasterView(View):
labels.update(cls.row_labels)
return labels
def get_use_buefy(self):
"""
Returns a flag indicating whether or not the current theme supports
(and therefore should use) the Buefy JS library.
"""
# first check theme-specific setting, if one has been defined
theme = self.request.registry.settings['tailbone.theme']
buefy = self.rattail_config.getbool('tailbone', 'themes.{}.use_buefy'.format(theme))
if buefy is not None:
return buefy
# TODO: should not hard-code this surely, but works for now...
if theme == 'falafel':
return True
# TODO: probably should not use this fallback? it was the first setting
# i tested with, but is poorly named to say the least
return self.rattail_config.getbool('tailbone', 'grids.use_buefy', default=False)
##############################
# Available Views
##############################
@ -368,6 +349,8 @@ class MasterView(View):
defaults = {
'model_class': getattr(self, 'model_class', None),
'model_title': self.get_model_title(),
'model_title_plural': self.get_model_title_plural(),
'width': 'full',
'filterable': self.filterable,
'use_byte_string_filters': self.use_byte_string_filters,

View file

@ -2,7 +2,7 @@
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2018 Lance Edgar
# Copyright © 2010-2019 Lance Edgar
#
# This file is part of Rattail.
#
@ -33,6 +33,7 @@ import six
from rattail.db import model, api
from rattail.settings import Setting
from rattail.util import import_module_path
from rattail.config import parse_bool
import colander
from webhelpers2.html import tags
@ -109,7 +110,7 @@ class AppSettingsView(View):
if form.validate(newstyle=True):
self.save_form(form)
group = self.request.POST.get('settings-group')
if group:
if group is not None:
self.request.session['appsettings.current_group'] = group
self.request.session.flash("App Settings have been saved.")
return self.redirect(self.request.current_route_url())
@ -120,17 +121,56 @@ class AppSettingsView(View):
if not current_group:
current_group = self.request.session.get('appsettings.current_group')
group_options = [tags.Option(group, group) for group in groups]
group_options.insert(0, tags.Option("(All)", "(All)"))
return {
use_buefy = self.get_use_buefy()
context = {
'index_title': "App Settings",
'form': form,
'dform': form.make_deform_form(),
'groups': groups,
'group_options': group_options,
'current_group': current_group,
'settings': settings,
'use_buefy': use_buefy,
}
if use_buefy:
context['buefy_data'] = self.get_buefy_data(form, groups, settings)
# TODO: this seems hacky, and probably only needed if theme changes?
if current_group == '(All)':
current_group = ''
else:
group_options = [tags.Option(group, group) for group in groups]
group_options.insert(0, tags.Option("(All)", "(All)"))
context['group_options'] = group_options
context['current_group'] = current_group
return context
def get_buefy_data(self, form, groups, settings):
dform = form.make_deform_form()
grouped = dict([(label, [])
for label in groups])
for setting in settings:
field = dform[setting.node_name]
s = {
'field_name': field.name,
'label': form.get_label(field.name),
'data_type': setting.data_type.__name__,
'choices': setting.choices,
'helptext': form.render_helptext(field.name) if form.has_helptext(field.name) else None,
'error': field.error,
}
value = self.get_setting_value(setting)
if setting.data_type is bool:
value = parse_bool(value)
s['value'] = value
if field.error:
s['error_messages'] = field.error_messages()
grouped[setting.group].append(s)
data = []
for label in groups:
group = {'label': label, 'settings': grouped[label]}
data.append(group)
return data
def make_form(self, known_settings):
schema = colander.MappingSchema()