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,157 +26,63 @@
``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,
exact=['number'],
ilike=['name'])
def search_config(request, fmap): mapped_class = rattail.Category
return filters.get_search_config( route_name = 'categories.list'
'categories.list', request, fmap, route_prefix = 'category'
include_filter_name=True,
filter_type_name='lk')
def search_form(config): def filter_map(self):
return filters.get_search_form(config) return self.make_filter_map(
exact=['number'],
ilike=['name'])
def grid_config(request, search, fmap): def search_config(self, fmap):
return grids.get_grid_config( return self.make_search_config(
'categories.list', request, search, fmap,
filter_map=fmap, sort='name', include_filter_name=True,
deletable=True) filter_type_name='lk')
def sort_map(): def grid_config(self, search, fmap):
return grids.get_sort_map( return self.make_grid_config(search, fmap,
rattail.Category, sort='number')
['number', 'name'])
def query(config): def sort_map(self):
smap = sort_map() return self.make_sort_map('number', 'name')
q = Session.query(rattail.Category)
q = filters.filter_query(q, config) def grid(self, data, config):
q = grids.sort_query(q, config, smap) g = self.make_grid(data, config)
return q g.configure(
include=[
g.number,
g.name,
],
readonly=True)
return g
def categories(request): class CategoryCrud(Crud):
fmap = filter_map() mapped_class = rattail.Category
config = search_config(request, fmap) home_route = 'categories.list'
search = search_form(config) url_prefix = '/categories'
config = grid_config(request, search, fmap)
categories = grids.get_pager(query, config)
g = forms.AlchemyGrid( def fieldset(self, obj):
rattail.Category, categories, config, fs = self.make_fieldset(obj)
gridurl=request.route_url('categories.list'), fs.configure(
objurl='category.edit', delurl='category.delete') include=[
fs.number,
g.configure( fs.name,
include=[ ])
g.number, return fs
g.name,
],
readonly=True)
grid = g.render(class_='clickable categories')
return grids.render_grid(request, grid, search)
def category_fieldset(category, request):
fs = forms.make_fieldset(category, url=request.route_url,
url_action=request.current_route_url(),
route_name='categories.list')
fs.configure(
include=[
fs.number,
fs.name,
])
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)