add Crud pyramid view class
This commit is contained in:
parent
19dc86f4a9
commit
e1abfa3e3f
1 changed files with 108 additions and 0 deletions
|
@ -37,9 +37,117 @@ from pyramid.httpexceptions import HTTPFound, HTTPException
|
||||||
# from rattail.db.perms import has_permission
|
# from rattail.db.perms import has_permission
|
||||||
# from rattail.pyramid.forms.formalchemy import Grid
|
# from rattail.pyramid.forms.formalchemy import Grid
|
||||||
|
|
||||||
|
from edbob.db import Base
|
||||||
|
from edbob.pyramid import forms
|
||||||
from edbob.pyramid import Session
|
from edbob.pyramid import Session
|
||||||
|
from edbob.util import requires_impl
|
||||||
|
|
||||||
|
|
||||||
|
class Crud(object):
|
||||||
|
|
||||||
|
def __init__(self, request):
|
||||||
|
self.request = request
|
||||||
|
|
||||||
|
@property
|
||||||
|
@requires_impl(is_property=True)
|
||||||
|
def mapped_class(self):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
@property
|
||||||
|
@requires_impl(is_property=True)
|
||||||
|
def list_route(self):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
@property
|
||||||
|
def list_url(self):
|
||||||
|
return self.request.route_url(self.list_route)
|
||||||
|
|
||||||
|
def make_fieldset(self, model, **kwargs):
|
||||||
|
if 'action_url' not in kwargs:
|
||||||
|
kwargs['action_url'] = self.request.current_route_url()
|
||||||
|
if 'list_url' not in kwargs:
|
||||||
|
kwargs['list_url'] = self.list_url
|
||||||
|
return forms.make_fieldset(model, **kwargs)
|
||||||
|
|
||||||
|
def fieldset(self, obj):
|
||||||
|
"""
|
||||||
|
Creates, configures and returns the fieldset.
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
You more than likely will want to override this.
|
||||||
|
"""
|
||||||
|
|
||||||
|
fs = self.make_fieldset(obj)
|
||||||
|
return fs
|
||||||
|
|
||||||
|
def crud(self, obj):
|
||||||
|
|
||||||
|
fs = self.fieldset(obj)
|
||||||
|
if not fs.readonly and self.request.POST:
|
||||||
|
fs.rebind(data=self.request.params)
|
||||||
|
if fs.validate():
|
||||||
|
|
||||||
|
with transaction.manager:
|
||||||
|
fs.sync()
|
||||||
|
Session.add(fs.model)
|
||||||
|
Session.flush()
|
||||||
|
self.request.session.flash('%s "%s" has been %s.' % (
|
||||||
|
fs.crud_title, fs.get_display_text(),
|
||||||
|
'updated' if fs.edit else 'created'))
|
||||||
|
|
||||||
|
if self.request.params.get('add-another') == '1':
|
||||||
|
return HTTPFound(location=self.request.current_route_url())
|
||||||
|
|
||||||
|
return HTTPFound(location=self.list_url)
|
||||||
|
|
||||||
|
# TODO: This probably needs attention.
|
||||||
|
if not fs.edit:
|
||||||
|
fs.allow_continue = True
|
||||||
|
|
||||||
|
return {'fieldset': fs, 'crud': True}
|
||||||
|
|
||||||
|
def new(self):
|
||||||
|
return self.crud(self.mapped_class)
|
||||||
|
|
||||||
|
def edit(self):
|
||||||
|
uuid = self.request.matchdict['uuid']
|
||||||
|
obj = Session.query(self.mapped_class).get(uuid) if uuid else None
|
||||||
|
assert obj
|
||||||
|
return self.crud(obj)
|
||||||
|
|
||||||
|
def delete(self):
|
||||||
|
uuid = self.request.matchdict['uuid']
|
||||||
|
obj = Session.query(self.mapped_class).get(uuid) if uuid else None
|
||||||
|
assert obj
|
||||||
|
with transaction.manager:
|
||||||
|
Session.delete(obj)
|
||||||
|
return HTTPFound(location=self.request.route_url(self.list_route))
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def add_routes(cls, config, route_prefix, url_prefix, template_prefix=None, permission_prefix=None):
|
||||||
|
"""
|
||||||
|
Add standard routes (i.e. 'new', 'edit' and 'delete') for the mapped
|
||||||
|
class to the config object.
|
||||||
|
"""
|
||||||
|
|
||||||
|
if not template_prefix:
|
||||||
|
template_prefix = url_prefix
|
||||||
|
if not permission_prefix:
|
||||||
|
permission_prefix = route_prefix
|
||||||
|
|
||||||
|
config.add_route('%s.new' % route_prefix, '%s/new' % url_prefix)
|
||||||
|
config.add_view(cls, attr='new', route_name='%s.new' % route_prefix, renderer='%s/crud.mako' % template_prefix,
|
||||||
|
permission='%s.create' % permission_prefix, http_cache=0)
|
||||||
|
|
||||||
|
config.add_route('%s.edit' % route_prefix, '%s/{uuid}/edit' % url_prefix)
|
||||||
|
config.add_view(cls, attr='edit', route_name='%s.edit' % route_prefix, renderer='%s/crud.mako' % template_prefix,
|
||||||
|
permission='%s.edit' % permission_prefix, http_cache=0)
|
||||||
|
|
||||||
|
config.add_route('%s.delete' % route_prefix, '%s/{uuid}/delete' % url_prefix)
|
||||||
|
config.add_view(cls, attr='delete', route_name='%s.delete' % route_prefix,
|
||||||
|
permission='%s.delete' % permission_prefix, http_cache=0)
|
||||||
|
|
||||||
|
|
||||||
def crud(request, cls, fieldset_factory, home=None, delete=None, post_sync=None, pre_render=None):
|
def crud(request, cls, fieldset_factory, home=None, delete=None, post_sync=None, pre_render=None):
|
||||||
"""
|
"""
|
||||||
Adds a common CRUD mechanism for objects.
|
Adds a common CRUD mechanism for objects.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue