Convert label profiles to use master view.

Plus some more minor tweaks to the framework.
This commit is contained in:
Lance Edgar 2015-08-12 00:46:39 -05:00
parent af07f477dc
commit d4210844ac
11 changed files with 145 additions and 184 deletions

View file

@ -267,7 +267,7 @@ class AlchemyBooleanFilter(AlchemyGridFilter):
""" """
Boolean filter for SQLAlchemy. Boolean filter for SQLAlchemy.
""" """
default_verbs = ['is_true', 'is_false', 'is_null', 'is_not_null', 'is_any'] default_verbs = ['is_true', 'is_false', 'is_any']
def filter_is_true(self, query, value): def filter_is_true(self, query, value):
""" """

View file

@ -0,0 +1,17 @@
## -*- coding: utf-8 -*-
<%inherit file="/master/create.mako" />
<%def name="head_tags()">
${parent.head_tags()}
<style type="text/css">
div.form div.field-wrapper.format textarea {
font-size: 120%;
font-family: monospace;
width: auto;
}
</style>
</%def>
${parent.body()}

View file

@ -1,29 +0,0 @@
## -*- coding: utf-8 -*-
<%inherit file="/crud.mako" />
<%def name="head_tags()">
${parent.head_tags()}
<style type="text/css">
div.form div.field-wrapper.format textarea {
font-size: 120%;
font-family: monospace;
width: auto;
}
</style>
</%def>
<%def name="context_menu_items()">
<li>${h.link_to("Back to Label Profiles", url('label_profiles'))}</li>
% if form.updating:
<% profile = form.fieldset.model %>
<% printer = profile.get_printer(request.rattail_config) %>
% if printer and printer.required_settings:
<li>${h.link_to("Edit Printer Settings", url('label_profile.printer_settings', uuid=profile.uuid))}</li>
% endif
<li>${h.link_to("View this Label Profile", url('label_profile.read', uuid=profile.uuid))}</li>
% endif
</%def>
${parent.body()}

View file

@ -0,0 +1,25 @@
## -*- coding: utf-8 -*-
<%inherit file="/master/edit.mako" />
<%def name="head_tags()">
${parent.head_tags()}
<style type="text/css">
div.form div.field-wrapper.format textarea {
font-size: 120%;
font-family: monospace;
width: auto;
}
</style>
</%def>
<%def name="context_menu_items()">
${parent.context_menu_items()}
<% printer = instance.get_printer(request.rattail_config) %>
% if printer and printer.required_settings:
<li>${h.link_to("Edit Printer Settings", url('labelprofiles.printer_settings', uuid=instance.uuid))}</li>
% endif
</%def>
${parent.body()}

View file

@ -1,12 +0,0 @@
## -*- coding: utf-8 -*-
<%inherit file="/grid.mako" />
<%def name="title()">Label Profiles</%def>
<%def name="context_menu_items()">
% if request.has_perm('label_profiles.create'):
<li>${h.link_to("Create a new Label Profile", url('label_profile.create'))}</li>
% endif
</%def>
${parent.body()}

View file

@ -4,9 +4,9 @@
<%def name="title()">Printer Settings</%def> <%def name="title()">Printer Settings</%def>
<%def name="context_menu_items()"> <%def name="context_menu_items()">
<li>${h.link_to("Back to Label Profiles", url('label_profiles'))}</li> <li>${h.link_to("Back to Label Profiles", url('labelprofiles'))}</li>
<li>${h.link_to("View this Label Profile", url('label_profile.read', uuid=profile.uuid))}</li> <li>${h.link_to("View this Label Profile", url('labelprofiles.view', uuid=profile.uuid))}</li>
<li>${h.link_to("Edit this Label Profile", url('label_profile.update', uuid=profile.uuid))}</li> <li>${h.link_to("Edit this Label Profile", url('labelprofiles.edit', uuid=profile.uuid))}</li>
</%def> </%def>
<div class="form-wrapper"> <div class="form-wrapper">
@ -40,7 +40,7 @@
<div class="buttons"> <div class="buttons">
${h.submit('update', "Update")} ${h.submit('update', "Update")}
<button type="button" onclick="location.href = '${url('label_profile.read', uuid=profile.uuid)}';">Cancel</button> <button type="button" onclick="location.href = '${url('labelprofiles.view', uuid=profile.uuid)}';">Cancel</button>
</div> </div>
${h.end_form()} ${h.end_form()}

View file

@ -1,36 +0,0 @@
## -*- coding: utf-8 -*-
<%inherit file="/labels/profiles/crud.mako" />
<%def name="context_menu_items()">
<li>${h.link_to("Back to Label Profiles", url('label_profiles'))}</li>
% if form.readonly and request.has_perm('label_profiles.update'):
<% profile = form.fieldset.model %>
<% printer = profile.get_printer(request.rattail_config) %>
<li>${h.link_to("Edit this Label Profile", url('label_profile.update', uuid=form.fieldset.model.uuid))}</li>
% if printer and printer.required_settings:
<li>${h.link_to("Edit Printer Settings", url('label_profile.printer_settings', uuid=profile.uuid))}</li>
% endif
% endif
% if version_count is not Undefined and request.has_perm('labelprofile.versions.view'):
<li>${h.link_to("View Change History ({0})".format(version_count), url('labelprofile.versions', uuid=form.fieldset.model.uuid))}</li>
% endif
</%def>
${parent.body()}
<% profile = form.fieldset.model %>
<% printer = profile.get_printer(request.rattail_config) %>
% if printer and printer.required_settings:
<h2>Printer Settings</h2>
<div class="form">
% for name, display in printer.required_settings.iteritems():
<div class="field-wrapper">
<label>${display}</label>
<div class="field">${profile.get_printer_setting(name) or ''}</div>
</div>
% endfor
</div>
% endif

View file

@ -0,0 +1,45 @@
## -*- coding: utf-8 -*-
<%inherit file="/master/view.mako" />
<%def name="head_tags()">
${parent.head_tags()}
<style type="text/css">
div.form div.field-wrapper.format textarea {
font-size: 120%;
font-family: monospace;
width: auto;
}
</style>
</%def>
<%def name="context_menu_items()">
${parent.context_menu_items()}
% if request.has_perm('labelprofiles.edit'):
<% printer = instance.get_printer(request.rattail_config) %>
% if printer and printer.required_settings:
<li>${h.link_to("Edit Printer Settings", url('labelprofiles.printer_settings', uuid=instance.uuid))}</li>
% endif
% endif
% if version_count is not Undefined and request.has_perm('labelprofile.versions.view'):
<li>${h.link_to("View Change History ({0})".format(version_count), url('labelprofile.versions', uuid=instance.uuid))}</li>
% endif
</%def>
${parent.body()}
<% printer = instance.get_printer(request.rattail_config) %>
% if printer and printer.required_settings:
<h2>Printer Settings</h2>
<div class="form">
% for name, display in printer.required_settings.iteritems():
<div class="field-wrapper">
<label>${display}</label>
<div class="field">${instance.get_printer_setting(name) or ''}</div>
</div>
% endfor
</div>
% endif

View file

@ -27,69 +27,15 @@ Label Views
from __future__ import unicode_literals from __future__ import unicode_literals
from rattail.db import model from rattail.db import model
from rattail.db.model import LabelProfile
from pyramid.httpexceptions import HTTPFound
import formalchemy import formalchemy
from webhelpers.html import HTML from webhelpers.html import HTML
from pyramid.httpexceptions import HTTPFound
from ..db import Session from tailbone.db import Session
from . import SearchableAlchemyGridView, CrudView from tailbone.views import MasterView
from ..grids.search import BooleanSearchFilter from tailbone.views.continuum import VersionView, version_defaults
from .continuum import VersionView, version_defaults
class ProfilesGrid(SearchableAlchemyGridView):
mapped_class = LabelProfile
config_prefix = 'label_profiles'
sort = 'ordinal'
def filter_map(self):
return self.make_filter_map(
exact=['code', 'visible'],
ilike=['description'])
def filter_config(self):
return self.make_filter_config(
filter_factory_visible=BooleanSearchFilter)
def sort_map(self):
return self.make_sort_map('ordinal', 'code', 'description', 'visible')
def grid(self):
g = self.make_grid()
g.configure(
include=[
g.ordinal,
g.code,
g.description,
g.visible,
],
readonly=True)
if self.request.has_perm('label_profiles.read'):
g.viewable = True
g.view_route_name = 'label_profile.read'
if self.request.has_perm('label_profiles.update'):
g.editable = True
g.edit_route_name = 'label_profile.update'
if self.request.has_perm('label_profiles.delete'):
g.deletable = True
g.delete_route_name = 'label_profile.delete'
return g
class ProfileCrud(CrudView):
mapped_class = LabelProfile
home_route = 'label_profiles'
pretty_name = "Label Profile"
update_cancel_route = 'label_profile.read'
def fieldset(self, model):
class FormatFieldRenderer(formalchemy.TextAreaFieldRenderer): class FormatFieldRenderer(formalchemy.TextAreaFieldRenderer):
@ -103,7 +49,27 @@ class ProfileCrud(CrudView):
kwargs.setdefault('size', (80, 8)) kwargs.setdefault('size', (80, 8))
return super(FormatFieldRenderer, self).render(**kwargs) return super(FormatFieldRenderer, self).render(**kwargs)
fs = self.make_fieldset(model)
class ProfilesView(MasterView):
"""
Master view for the LabelProfile model.
"""
model_class = model.LabelProfile
model_title = "Label Profile"
url_prefix = '/labels/profiles'
def configure_grid(self, g):
g.default_sortkey = 'ordinal'
g.configure(
include=[
g.ordinal,
g.code,
g.description,
g.visible,
],
readonly=True)
def configure_fieldset(self, fs):
fs.format.set(renderer=FormatFieldRenderer) fs.format.set(renderer=FormatFieldRenderer)
fs.configure( fs.configure(
include=[ include=[
@ -115,10 +81,11 @@ class ProfileCrud(CrudView):
fs.format, fs.format,
fs.visible, fs.visible,
]) ])
return fs
def post_save(self, form): def after_create(self, profile):
profile = form.fieldset.model self.after_edit(profile)
def after_edit(self, profile):
if not profile.format: if not profile.format:
formatter = profile.get_formatter() formatter = profile.get_formatter()
if formatter: if formatter:
@ -127,10 +94,6 @@ class ProfileCrud(CrudView):
except NotImplementedError: except NotImplementedError:
pass pass
def post_save_url(self, form):
return self.request.route_url('label_profile.read',
uuid=form.fieldset.model.uuid)
class LabelProfileVersionView(VersionView): class LabelProfileVersionView(VersionView):
""" """
@ -144,12 +107,12 @@ class LabelProfileVersionView(VersionView):
def printer_settings(request): def printer_settings(request):
uuid = request.matchdict['uuid'] uuid = request.matchdict['uuid']
profile = Session.query(LabelProfile).get(uuid) if uuid else None profile = Session.query(model.LabelProfile).get(uuid) if uuid else None
if not profile: if not profile:
return HTTPFound(location=request.route_url('label_profiles')) return HTTPFound(location=request.route_url('labelprofiles'))
read_profile = HTTPFound(location=request.route_url( read_profile = HTTPFound(location=request.route_url(
'label_profile.read', uuid=profile.uuid)) 'labelprofiles.view', uuid=profile.uuid))
printer = profile.get_printer(request.rattail_config) printer = profile.get_printer(request.rattail_config)
if not printer: if not printer:
@ -161,7 +124,7 @@ def printer_settings(request):
"require any settings." % profile) "require any settings." % profile)
return read_profile return read_profile
if request.POST: if request.method == 'POST':
for setting in printer.required_settings: for setting in printer.required_settings:
if setting in request.POST: if setting in request.POST:
profile.save_printer_setting(setting, request.POST[setting]) profile.save_printer_setting(setting, request.POST[setting])
@ -171,34 +134,11 @@ def printer_settings(request):
def includeme(config): def includeme(config):
ProfilesView.defaults(config)
config.add_route('label_profiles', '/labels/profiles')
config.add_view(ProfilesGrid, route_name='label_profiles',
renderer='/labels/profiles/index.mako',
permission='label_profiles.list')
config.add_route('label_profile.create', '/labels/profiles/new')
config.add_view(ProfileCrud, attr='create', route_name='label_profile.create',
renderer='/labels/profiles/crud.mako',
permission='label_profiles.create')
config.add_route('label_profile.read', '/labels/profiles/{uuid}')
config.add_view(ProfileCrud, attr='read', route_name='label_profile.read',
renderer='/labels/profiles/read.mako',
permission='label_profiles.read')
config.add_route('label_profile.update', '/labels/profiles/{uuid}/edit')
config.add_view(ProfileCrud, attr='update', route_name='label_profile.update',
renderer='/labels/profiles/crud.mako',
permission='label_profiles.update')
config.add_route('label_profile.delete', '/labels/profiles/{uuid}/delete')
config.add_view(ProfileCrud, attr='delete', route_name='label_profile.delete',
permission='label_profiles.delete')
config.add_route('label_profile.printer_settings', '/labels/profiles/{uuid}/printer')
config.add_view(printer_settings, route_name='label_profile.printer_settings',
renderer='/labels/profiles/printer.mako',
permission='label_profiles.update')
version_defaults(config, LabelProfileVersionView, 'labelprofile', template_prefix='/labels/profiles') version_defaults(config, LabelProfileVersionView, 'labelprofile', template_prefix='/labels/profiles')
# edit printer settings
config.add_route('labelprofiles.printer_settings', '/labels/profiles/{uuid}/printer')
config.add_view(printer_settings, route_name='labelprofiles.printer_settings',
renderer='/labels/profiles/printer.mako',
permission='labelprofiles.edit')

View file

@ -79,6 +79,7 @@ class MasterView(View):
if form.validate(): if form.validate():
form.save() form.save()
instance = form.fieldset.model instance = form.fieldset.model
self.after_create(instance)
self.request.session.flash("{0} {1} has been created.".format( self.request.session.flash("{0} {1} has been created.".format(
self.get_model_title(), instance)) self.get_model_title(), instance))
return HTTPFound(location=self.get_action_url('view', instance)) return HTTPFound(location=self.get_action_url('view', instance))
@ -104,6 +105,7 @@ class MasterView(View):
if self.request.method == 'POST': if self.request.method == 'POST':
if form.validate(): if form.validate():
form.save() form.save()
self.after_edit(instance)
self.request.session.flash("{0} {1} has been updated.".format( self.request.session.flash("{0} {1} has been updated.".format(
self.get_model_title(), instance)) self.get_model_title(), instance))
return HTTPFound(location=self.get_action_url('view', instance)) return HTTPFound(location=self.get_action_url('view', instance))
@ -433,9 +435,19 @@ class MasterView(View):
fieldset.prettify = prettify fieldset.prettify = prettify
return fieldset return fieldset
def after_create(self, instance):
"""
Event hook, called just after a new instance is saved.
"""
def after_edit(self, instance):
"""
Event hook, called just after an existing instance is saved.
"""
def before_delete(self, instance): def before_delete(self, instance):
""" """
Event hook which is called just before deletion is attempted. Event hook, called just before deletion is attempted.
""" """
############################## ##############################

View file

@ -137,7 +137,6 @@ class UsersView(MasterView):
del g.filters['salt'] del g.filters['salt']
g.filters['username'].default_active = True g.filters['username'].default_active = True
g.filters['username'].default_verb = 'contains' g.filters['username'].default_verb = 'contains'
g.filters['active'].verbs = ['is_true', 'is_false', 'is_any']
g.filters['active'].default_active = True g.filters['active'].default_active = True
g.filters['active'].default_verb = 'is_true' g.filters['active'].default_verb = 'is_true'
g.filters['person'] = g.make_filter('person', model.Person.display_name, label="Person's Name", g.filters['person'] = g.make_filter('person', model.Person.display_name, label="Person's Name",