My RESTful controller has the following default() method. No other
methods are not @exposed.
---
@expose()
def default(self, atom=None, *path, **kw):
trail = list(path)
parent = kw and kw.pop('parent', None) or None
if atom is None and not path:
# Record listing.
return self.index(parent, **kw)
elif hasattr(self, atom):
# Non-associated action like 'add'.
action = getattr(self, atom)
return action(parent=parent, **kw)
else:
# Associated action. Use numerical IDs to avoid name conflicts.
try:
record = self.getter(self.filter.to_python(atom))
except:
raise cherrypy.NotFound
action = getattr(self, trail.pop(0), None)
if action is None: raise cherrypy.NotFound
if not callable(action):
# Descend into child controllers.
return action.default(parent=record, *trail, **kw)
return action(record, parent=parent, **kw)
raise cherrypy.NotFound
---
For example, the add() method:
---
def add(self, **kw):
"""Display the add a record form and widgets."""
parent = kw and kw.pop('parent', None) or None
if kw:
@validate(form=self.create_form)
def get_errors(self, tg_errors=None, **data):
return tg_errors, data
tg_errors, data = get_errors(self, **kw)
if not tg_errors:
transaction = model.hub.begin()
try:
self.create_action(parent=parent, **data)
except:
transaction.rollback()
raise
else:
transaction.commit()
flash("success::" +
_("A new %s record has been successfully created.") %
self.label.lower())
redirect("./")
else:
flash("error::" + _("There were errors processing your
request."))
else:
data = self.create_prepare_action(parent)
return dict(
title=_("Adding %s Record") % self.label,
tg_template=self.form_template,
data=data,
form=self.create_form,
action="add",
**self.additional_data
)
---
self.form_template points to my magic genericform.kid template. My
flash messages are split on a pair of colons to define CSS class and
message. Being generic, create_action and create_prepare_action can
be overridden. create_action by default adds the parent ID to the
data set and simply runs self.so(**data). create_prepare_action
returns an empty dict by default.
If anyone wants me to split out the entire controller from my current
project, I'll see what I can do.
Matthew Bevan, Systems Administrator
Top Floor Computer Systems Ltd.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---