refactor categories views etc.

This commit is contained in:
Lance Edgar 2012-07-14 10:42:01 -05:00
parent e9c1ee8dce
commit c2dee891fb
5 changed files with 52 additions and 142 deletions

View file

@ -3,8 +3,8 @@
<%def name="crud_name()">Category</%def> <%def name="crud_name()">Category</%def>
<%def name="menu()"> <%def name="context_menu_items()">
<p>${h.link_to("Back to Categories", url('categories.list'))}</p> <li>${h.link_to("Back to Categories", url('categories.list'))}</li>
</%def> </%def>
${parent.body()} ${parent.body()}

View file

@ -0,0 +1,2 @@
<%inherit file="/categories/crud.mako" />
${parent.body()}

View file

@ -3,9 +3,9 @@
<%def name="title()">Categories</%def> <%def name="title()">Categories</%def>
<%def name="menu()"> <%def name="context_menu_items()">
% if request.has_perm('categories.create'): % if request.has_perm('categories.create'):
<p>${h.link_to("Create a new Category", url('category.new'))}</p> <li>${h.link_to("Create a new Category", url('category.new'))}</li>
% endif % endif
</%def> </%def>

View file

@ -0,0 +1,2 @@
<%inherit file="/categories/crud.mako" />
${parent.body()}

View file

@ -26,79 +26,55 @@
``rattail.pyramid.views.categories`` -- Category Views ``rattail.pyramid.views.categories`` -- Category Views
""" """
import transaction from edbob.pyramid.views import GridView
from pyramid.httpexceptions import HTTPFound from edbob.pyramid.views.crud import Crud
from edbob.pyramid import filters
from edbob.pyramid import forms
from edbob.pyramid import grids
from edbob.pyramid import Session
import rattail import rattail
def filter_map(): class CategoryGrid(GridView):
return filters.get_filter_map(
rattail.Category, mapped_class = rattail.Category
route_name = 'categories.list'
route_prefix = 'category'
def filter_map(self):
return self.make_filter_map(
exact=['number'], exact=['number'],
ilike=['name']) ilike=['name'])
def search_config(request, fmap): def search_config(self, fmap):
return filters.get_search_config( return self.make_search_config(
'categories.list', request, fmap, fmap,
include_filter_name=True, include_filter_name=True,
filter_type_name='lk') filter_type_name='lk')
def search_form(config): def grid_config(self, search, fmap):
return filters.get_search_form(config) return self.make_grid_config(search, fmap,
sort='number')
def grid_config(request, search, fmap): def sort_map(self):
return grids.get_grid_config( return self.make_sort_map('number', 'name')
'categories.list', request, search,
filter_map=fmap, sort='name',
deletable=True)
def sort_map():
return grids.get_sort_map(
rattail.Category,
['number', 'name'])
def query(config):
smap = sort_map()
q = Session.query(rattail.Category)
q = filters.filter_query(q, config)
q = grids.sort_query(q, config, smap)
return q
def categories(request):
fmap = filter_map()
config = search_config(request, fmap)
search = search_form(config)
config = grid_config(request, search, fmap)
categories = grids.get_pager(query, config)
g = forms.AlchemyGrid(
rattail.Category, categories, config,
gridurl=request.route_url('categories.list'),
objurl='category.edit', delurl='category.delete')
def grid(self, data, config):
g = self.make_grid(data, config)
g.configure( g.configure(
include=[ include=[
g.number, g.number,
g.name, g.name,
], ],
readonly=True) readonly=True)
return g
grid = g.render(class_='clickable categories')
return grids.render_grid(request, grid, search)
def category_fieldset(category, request): class CategoryCrud(Crud):
fs = forms.make_fieldset(category, url=request.route_url,
url_action=request.current_route_url(), mapped_class = rattail.Category
route_name='categories.list') home_route = 'categories.list'
url_prefix = '/categories'
def fieldset(self, obj):
fs = self.make_fieldset(obj)
fs.configure( fs.configure(
include=[ include=[
fs.number, fs.number,
@ -107,76 +83,6 @@ def category_fieldset(category, request):
return fs return fs
def new_category(request):
fs = category_fieldset(rattail.Category, request)
if not fs.readonly and request.POST:
fs.rebind(data=request.params)
if fs.validate():
with transaction.manager:
fs.sync()
Session.add(fs.model)
Session.flush()
request.session.flash("%s \"%s\" has been %s." % (
fs.crud_title, fs.get_display_text(),
'updated' if fs.edit else 'created'))
return HTTPFound(location=request.route_url('categories.list'))
return {'fieldset': fs, 'crud': True}
def edit_category(request):
"""
View for editing a :class:`rattail.Category` instance.
"""
uuid = request.matchdict['uuid']
category = Session.query(rattail.Category).get(uuid) if uuid else None
assert category
fs = category_fieldset(category, request)
if request.POST:
fs.rebind(data=request.params)
if fs.validate():
with transaction.manager:
fs.sync()
fs.model = Session.merge(fs.model)
request.session.flash("%s \"%s\" has been %s." % (
fs.crud_title, fs.get_display_text(),
'updated' if fs.edit else 'created'))
home = request.route_url('categories.list')
return HTTPFound(location=home)
return {'fieldset': fs, 'crud': True}
def delete_category(request):
uuid = request.matchdict['uuid']
category = Session.query(rattail.Category).get(uuid) if uuid else None
assert category
with transaction.manager:
Session.delete(category)
return HTTPFound(location=request.route_url('categories.list'))
def includeme(config): def includeme(config):
CategoryGrid.add_route(config, 'categories.list', '/categories')
config.add_route('categories.list', '/categories') CategoryCrud.add_routes(config)
config.add_view(categories, route_name='categories.list', renderer='/categories/index.mako',
permission='categories.list', http_cache=0)
config.add_route('category.new', '/categories/new')
config.add_view(new_category, route_name='category.new', renderer='/categories/category.mako',
permission='categories.create', http_cache=0)
config.add_route('category.edit', '/categories/{uuid}/edit')
config.add_view(edit_category, route_name='category.edit', renderer='/categories/category.mako',
permission='category.edit', http_cache=0)
config.add_route('category.delete', '/categories/{uuid}/delete')
config.add_view(delete_category, route_name='category.delete',
permission='category.delete', http_cache=0)