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="menu()">
<p>${h.link_to("Back to Categories", url('categories.list'))}</p>
<%def name="context_menu_items()">
<li>${h.link_to("Back to Categories", url('categories.list'))}</li>
</%def>
${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="menu()">
<%def name="context_menu_items()">
% 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
</%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
"""
import transaction
from pyramid.httpexceptions import HTTPFound
from edbob.pyramid import filters
from edbob.pyramid import forms
from edbob.pyramid import grids
from edbob.pyramid import Session
from edbob.pyramid.views import GridView
from edbob.pyramid.views.crud import Crud
import rattail
def filter_map():
return filters.get_filter_map(
rattail.Category,
class CategoryGrid(GridView):
mapped_class = rattail.Category
route_name = 'categories.list'
route_prefix = 'category'
def filter_map(self):
return self.make_filter_map(
exact=['number'],
ilike=['name'])
def search_config(request, fmap):
return filters.get_search_config(
'categories.list', request, fmap,
def search_config(self, fmap):
return self.make_search_config(
fmap,
include_filter_name=True,
filter_type_name='lk')
def search_form(config):
return filters.get_search_form(config)
def grid_config(self, search, fmap):
return self.make_grid_config(search, fmap,
sort='number')
def grid_config(request, search, fmap):
return grids.get_grid_config(
'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 sort_map(self):
return self.make_sort_map('number', 'name')
def grid(self, data, config):
g = self.make_grid(data, config)
g.configure(
include=[
g.number,
g.name,
],
readonly=True)
grid = g.render(class_='clickable categories')
return grids.render_grid(request, grid, search)
return g
def category_fieldset(category, request):
fs = forms.make_fieldset(category, url=request.route_url,
url_action=request.current_route_url(),
route_name='categories.list')
class CategoryCrud(Crud):
mapped_class = rattail.Category
home_route = 'categories.list'
url_prefix = '/categories'
def fieldset(self, obj):
fs = self.make_fieldset(obj)
fs.configure(
include=[
fs.number,
@ -107,76 +83,6 @@ def category_fieldset(category, request):
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):
config.add_route('categories.list', '/categories')
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)
CategoryGrid.add_route(config, 'categories.list', '/categories')
CategoryCrud.add_routes(config)