"""This is a mostly empty superclass for controllers."""
__docformat__ = "restructuredtext"
# Date: Thu May 27 07:41:48 PDT 2004
# Author: Shannon -jj Behrens
# Email: jjinux@users.sourceforge.net
#
# Copyright (c) Shannon -jj Behrens. All rights reserved.
from aquarium.util.AquariumClass import AquariumClass
from aquarium.util.InternalLibrary import FormValueError
import cgi
class Controller(AquariumClass):
"""This is a mostly empty superclass for controllers."""
def __call__(self, callNext, *args, **kargs):
"""Save a reference to self in ``ctx``."""
self._ctx.controller = self
return callNext(*args, **kargs)
def executeAction(self):
"""Call a method of the form ``doFooAction``.
If there is a form variable named ``action`` containing something like
``Foo``, e.g.::
http://bar?action=Foo
or if there is a form variable named something like ``action:Foo`` (this
syntax was stolen from Zope), e.g.::
<input type="submit" name="action:Foo" value="Submit" />
this method will call the ``doFooAction`` method. Since controllers
must often do double duty (they change state as well as prepare for the
view), this method makes it possible to put all the state changing code
in methods of the form ``doFooAction``, leaving the main ``__call__``
method mostly free to handle the preparation for the view.
The ``__call__`` method should call ``self.executeAction()`` when it is
ready for the action to be called. Having the subclass call
``executeAction`` rather than letting the superclass do it permits the
subclass to do initialization in ``__call__`` before the action method
is called.
I'll call ``clearAction`` right before executing the action.
Otherwise, if the action forwards to another controller, the new
controller may try to execute its own ``doFooAction``, which probably
isn't what you want.
"""
action = self.getAction()
if not action:
return
name = "do" + action + "Action"
if not hasattr(self, name):
raise FormValueError("There is no action named '%s' (i.e. '%s')" %
(action, name))
f = getattr(self, name)
self.clearAction()
f()
def getAction(self):
"""Get the name of the action executeAction would call or None.
This is *not* the method name.
"""
form = self._ctx.form
if form.has_key("action"):
return form["action"]
for key in form.keys():
if key.startswith("action:"):
return key[len("action:"):]
return None
def clearAction(self):
"""Clear out all form parameters that would execute an action."""
form = self._ctx.form
try:
del form["action"]
except KeyError:
pass
for i in form.keys():
if i.startswith("action:"):
del form[i]
def getBookmark(self, vars=None, stripAction=True):
"""Returns a bookmarkable URL for the current screen.
Builds a URL that can be used to return to this location in the site.
Tries to save everything in the querystring, however, by default
it will strip any actions that could be picked up by ``executeAction``.
You may override this method in your own controller if you need to
change the default for ``stripAction`` or if you need to do something
fancy to create the bookmark URL. This method should not assume that
``self.screen`` is still set to this controller.
vars
A dictionary of variables that you want appended to the URL in the
query string. If you have variables of the same name in the
bookmark URL, ``vars`` will take precedence.
stripAction
If True, attempts to remove all actions from the querystring that
could potentially be executed by ``executeAction()``.
"""
if vars is None:
vars = {}
ctx = self._ctx
rawqs = cgi.parse_qs(ctx.wsa.getCgiEnv()["QUERY_STRING"])
if not stripAction:
newqs = rawqs
else:
newqs = {}
for name in rawqs.keys():
if name.startswith("action:") or name == "action":
continue
newqs[name] = rawqs[name]
newqs.update(vars)
return ctx.url.screen(self.getName(), newqs)
def getName(self):
"""Get the name of this screen."""
return self.__class__.__module__[len("aquarium.screen."):]
|