convenience commit
This commit is contained in:
parent
4df92ee794
commit
561f25a5f3
10 changed files with 45 additions and 136 deletions
|
@ -34,6 +34,7 @@ from webhelpers.html.tags import link_to
|
|||
|
||||
from edbob.pyramid.views.autocomplete import *
|
||||
from edbob.pyramid.views.form import *
|
||||
from edbob.pyramid.views.grid import *
|
||||
|
||||
|
||||
def forbidden(request):
|
||||
|
|
|
@ -33,7 +33,10 @@ from edbob.pyramid import Session
|
|||
from edbob.util import requires_impl
|
||||
|
||||
|
||||
class Grid(object):
|
||||
__all__ = ['GridView']
|
||||
|
||||
|
||||
class GridView(object):
|
||||
|
||||
@property
|
||||
@requires_impl(is_property=True)
|
||||
|
|
|
@ -8,7 +8,6 @@ from edbob.db.extensions import Extension
|
|||
|
||||
from {{package}}._version import __version__
|
||||
from {{package}}.model import *
|
||||
from {{package}}.model import __all__
|
||||
|
||||
|
||||
class {{package}}_extension(Extension):
|
||||
|
|
|
@ -46,6 +46,7 @@ def main(global_config, **settings):
|
|||
|
||||
# Additional config is defined elsewhere within {{project}}. This includes
|
||||
# incorporating the various views etc. exposed by other packages.
|
||||
config.include('{{package}}.pyramid.static')
|
||||
config.include('{{package}}.pyramid.subscribers')
|
||||
config.include('{{package}}.pyramid.views')
|
||||
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
"""
|
||||
``{{package}}.pyramid.static`` -- Static Assets
|
||||
"""
|
||||
|
||||
|
||||
def includeme(config):
|
||||
config.add_static_view('{{package}}', '{{package}}.pyramid:static', cache_max_age=3600)
|
|
@ -1,8 +1,6 @@
|
|||
<%inherit file="/foo/base.mako" />
|
||||
<%inherit file="/crud.mako" />
|
||||
|
||||
<%def name="crud_name()">Foo</%def>
|
||||
|
||||
<%def name="menu()">
|
||||
<p>${h.link_to("Back to Foo", url('foo.list'))}</p>
|
||||
</%def>
|
|
@ -0,0 +1,2 @@
|
|||
<%inherit file="/foo/crud.mako" />
|
||||
${parent.body()}
|
|
@ -0,0 +1,2 @@
|
|||
<%inherit file="/foo/crud.mako" />
|
||||
${parent.body()}
|
|
@ -5,163 +5,57 @@
|
|||
``{{package}}.pyramid.views.foo`` -- Foo Views
|
||||
"""
|
||||
|
||||
import transaction
|
||||
from pyramid.httpexceptions import HTTPFound
|
||||
|
||||
import edbob
|
||||
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 {{package}}
|
||||
|
||||
|
||||
class FooGrid(object):
|
||||
class FooGrid(GridView):
|
||||
|
||||
def __init__(self, request):
|
||||
self.request = request
|
||||
mapped_class = {{package}}.Foo
|
||||
route_name = 'foos.list'
|
||||
route_prefix = 'foo'
|
||||
|
||||
def filter_map(self):
|
||||
return filters.get_filter_map(
|
||||
{{package}}.Foo,
|
||||
ilike=['description'])
|
||||
return self.make_filter_map(ilike=['description'])
|
||||
|
||||
def search_config(self, fmap):
|
||||
return filters.get_search_config(
|
||||
'foo.list', self.request, fmap,
|
||||
return self.make_search_config(fmap,
|
||||
include_filter_description=True,
|
||||
filter_type_description='lk')
|
||||
|
||||
def search_form(self, config):
|
||||
return filters.get_search_form(config)
|
||||
|
||||
def grid_config(self, search, fmap):
|
||||
return grids.get_grid_config(
|
||||
'foo.list', self.request, search,
|
||||
filter_map=fmap, sort='description',
|
||||
deletable=True)
|
||||
return self.make_grid_config(search, fmap,
|
||||
sort='description')
|
||||
|
||||
def sort_map(self):
|
||||
return grids.get_sort_map(
|
||||
{{package}}.Foo,
|
||||
['description'])
|
||||
|
||||
def query(self, config):
|
||||
smap = self.sort_map()
|
||||
q = Session.query({{package}}.Foo)
|
||||
q = filters.filter_query(q, config)
|
||||
q = grids.sort_query(q, config, smap)
|
||||
return q
|
||||
|
||||
def __call__(self):
|
||||
|
||||
fmap = self.filter_map()
|
||||
config = self.search_config(fmap)
|
||||
search = self.search_form(config)
|
||||
config = self.grid_config(search, fmap)
|
||||
foos = grids.get_pager(self.query, config)
|
||||
|
||||
g = forms.AlchemyGrid(
|
||||
{{package}}.Foo, foos, config,
|
||||
gridurl=self.request.route_url('foo.list'),
|
||||
objurl='foo.edit', delurl='foo.delete')
|
||||
return self.make_sort_map('description')
|
||||
|
||||
def grid(self, data, config):
|
||||
g = self.make_grid(data, config)
|
||||
g.configure(
|
||||
include=[
|
||||
g.description,
|
||||
],
|
||||
readonly=True)
|
||||
|
||||
grid = g.render(class_='clickable foo')
|
||||
return grids.render_grid(self.request, grid, search)
|
||||
return g
|
||||
|
||||
|
||||
def foo_fieldset(foo, request):
|
||||
fs = forms.make_fieldset(foo, crud_title="Foo",
|
||||
url=request.route_url,
|
||||
url_action=request.current_route_url(),
|
||||
route_name='foo.list')
|
||||
class FooCrud(Crud):
|
||||
|
||||
fs.configure(
|
||||
include=[
|
||||
fs.description,
|
||||
])
|
||||
return fs
|
||||
mapped_class = {{package}}.Foo
|
||||
home_route = 'foos.list'
|
||||
|
||||
|
||||
def new_foo(request):
|
||||
|
||||
fs = foo_fieldset({{package}}.Foo, 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('foo.list'))
|
||||
|
||||
return {'fieldset': fs, 'crud': True}
|
||||
|
||||
|
||||
def edit_foo(request):
|
||||
"""
|
||||
View for editing a :class:`{{package}}.Foo` instance.
|
||||
"""
|
||||
|
||||
uuid = request.matchdict['uuid']
|
||||
foo = Session.query({{package}}.Foo).get(uuid) if uuid else None
|
||||
assert foo
|
||||
|
||||
fs = foo_fieldset(foo, 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('foo.list')
|
||||
|
||||
return HTTPFound(location=home)
|
||||
|
||||
return {'fieldset': fs, 'crud': True}
|
||||
|
||||
|
||||
def delete_foo(request):
|
||||
uuid = request.matchdict['uuid']
|
||||
foo = Session.query({{package}}.Foo).get(uuid) if uuid else None
|
||||
assert foo
|
||||
|
||||
with transaction.manager:
|
||||
Session.delete(foo)
|
||||
|
||||
return HTTPFound(location=request.route_url('foo.list'))
|
||||
def fieldset(self, obj):
|
||||
fs = self.make_fieldset(obj)
|
||||
fs.configure(
|
||||
include=[
|
||||
fs.description,
|
||||
])
|
||||
return fs
|
||||
|
||||
|
||||
def includeme(config):
|
||||
|
||||
config.add_route('foo.list', '/foo')
|
||||
config.add_view(FooGrid, route_name='foo.list', renderer='/foo/index.mako',
|
||||
permission='foo.list', http_cache=0)
|
||||
|
||||
config.add_route('foo.new', '/foo/new')
|
||||
config.add_view(new_foo, route_name='foo.new', renderer='/foo/foo.mako',
|
||||
permission='foo.create', http_cache=0)
|
||||
|
||||
config.add_route('foo.edit', '/foo/{uuid}/edit')
|
||||
config.add_view(edit_foo, route_name='foo.edit', renderer='/foo/foo.mako',
|
||||
permission='foo.edit', http_cache=0)
|
||||
|
||||
config.add_route('foo.delete', '/foo/{uuid}/delete')
|
||||
config.add_view(delete_foo, route_name='foo.delete',
|
||||
permission='foo.delete', http_cache=0)
|
||||
FooGrid.add_route(config, 'foos.list', '/foos')
|
||||
FooCrud.add_routes(config)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue