Controller.py :  » Web-Frameworks » Aquarium » aquarium-2.3 » aquarium » screen » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Aquarium 
Aquarium » aquarium 2.3 » aquarium » screen » Controller.py
"""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."):]
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.