changeset 4135:0fd8fd9979f8

Removed code for available actions from RequestBase
author Florian Krupicka <florian.krupicka@googlemail.com>
date Wed, 28 May 2008 18:34:35 +0200
parents 719256a8db51
children 9ed19741aee3
files MoinMoin/action/__init__.py MoinMoin/request/__init__.py
diffstat 2 files changed, 60 insertions(+), 59 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/action/__init__.py	Wed May 28 15:19:20 2008 +0200
+++ b/MoinMoin/action/__init__.py	Wed May 28 18:34:35 2008 +0200
@@ -18,10 +18,12 @@
     Additionally to the usual stuff, we provide an ActionBase class here with
     some of the usual base functionality for an action, like checking
     actions_excluded, making and checking tickets, rendering some form,
-    displaying errors and doing stuff after an action.
+    displaying errors and doing stuff after an action. Also utility functions
+    regarding actions are located here.
 
     @copyright: 2000-2004 Juergen Hermann <jh@web.de>,
                 2006 MoinMoin:ThomasWaldmann
+                2008 MoinMoin:FlorianKrupicka
     @license: GNU GPL, see COPYING for details.
 """
 
@@ -300,15 +302,26 @@
     request.http_redirect(Page(request, target).url(request))
 
 # Dispatching ----------------------------------------------------------------
-def getNames(cfg):
-    if not hasattr(cfg.cache, 'action_names'):
-        lnames = names[:]
-        lnames.extend(wikiutil.getPlugins('action', cfg))
-        cfg.cache.action_names = lnames # remember it
+def get_names(config):
+    """ Get a list of known actions. 
+    
+    @param config: a config object
+    @rtype: set
+    @return: set of known actions
+    """
+    if not hasattr(config.cache, 'action_names'):
+        actions = names[:]
+        actions.extend(wikiutil.getPlugins('action', config))
+        actions = set(action for action in actions
+                      if not action in self.config.actions_excluded)
+        config.cache.action_names = actions # remember it
     return cfg.cache.action_names
 
 def getHandler(request, action, identifier="execute"):
-    """ return a handler function for a given action or None """
+    """ return a handler function for a given action or None.
+
+    TODO: remove request dependency
+    """
     # check for excluded actions
     if action in request.cfg.actions_excluded:
         return None
@@ -320,3 +333,37 @@
 
     return handler
 
+def get_available_actions(config, page, user):
+        """ Get a list of actions available on a particular page
+        for a particular user.
+
+        The set does not contain actions that starts with lower case.
+        Themes use this set to display the actions to the user.
+   
+        @param config: a config object (for the per-wiki actions)
+        @param page: the page to which the actions should apply
+        @param user: the user which wants the 
+        @rtype: set
+        @return: set of avaiable actions
+        """
+        if not user.may.read(page.page_name):
+            return []
+
+
+        actions = get_names(config)
+
+        # Filter non ui actions (starts with lower case letter)
+        actions = [action for action in actions if not action[0].islower()]
+
+        # Filter actions by page type, acl and user state
+        excluded = []
+        if (page.isUnderlayPage() and not page.isStandardPage()) or \
+                not user.may.write(page.page_name) or \
+                not user.may.delete(page.page_name):
+                # Prevent modification of underlay only pages, or pages
+                # the user can't write and can't delete
+                excluded = [u'RenamePage', u'DeletePage', ] # AttachFile must NOT be here!
+
+        return set(action for action in actions if not action in excluded)
+
+
--- a/MoinMoin/request/__init__.py	Wed May 28 15:19:20 2008 +0200
+++ b/MoinMoin/request/__init__.py	Wed May 28 18:34:35 2008 +0200
@@ -47,6 +47,7 @@
 
 from MoinMoin.Page import Page
 from MoinMoin import config, wikiutil, user, caching, error
+from MoinMoin.action import get_names, get_available_actions
 from MoinMoin.config import multiconfig
 from MoinMoin.support.python_compatibility import set
 from MoinMoin.util import IsWin9x
@@ -771,59 +772,12 @@
             return ''
         return self.script_name
 
-    def getKnownActions(self):
-        """ Create a dict of avaiable actions
-
-        Return cached version if avaiable.
-
-        @rtype: dict
-        @return: dict of all known actions
-        """
-        try:
-            self.cfg.cache.known_actions # check
-        except AttributeError:
-            from MoinMoin import action
-            self.cfg.cache.known_actions = set(action.getNames(self.cfg))
-
-        # Return a copy, so clients will not change the set.
-        return self.cfg.cache.known_actions.copy()
-
     def getAvailableActions(self, page):
-        """ Get list of avaiable actions for this request
-
-        The dict does not contain actions that starts with lower case.
-        Themes use this dict to display the actions to the user.
-
-        @param page: current page, Page object
-        @rtype: dict
-        @return: dict of avaiable actions
+        """ DEPRECATED! use MoinMoin.action.get_available_actions instead
         """
-        if self._available_actions is None:
-            # some actions might make sense for non-existing pages, so we just
-            # require read access here. Can be later refined to some action
-            # specific check:
-            if not self.user.may.read(page.page_name):
-                return []
-
-            # Filter non ui actions (starts with lower case letter)
-            actions = self.getKnownActions()
-            actions = [action for action in actions if not action[0].islower()]
-
-            # Filter wiki excluded actions
-            actions = [action for action in actions if not action in self.cfg.actions_excluded]
-
-            # Filter actions by page type, acl and user state
-            excluded = []
-            if ((page.isUnderlayPage() and not page.isStandardPage()) or
-                not self.user.may.write(page.page_name) or
-                not self.user.may.delete(page.page_name)):
-                # Prevent modification of underlay only pages, or pages
-                # the user can't write and can't delete
-                excluded = [u'RenamePage', u'DeletePage', ] # AttachFile must NOT be here!
-            actions = [action for action in actions if not action in excluded]
-
-            self._available_actions = set(actions)
-
+        if getattr(self, '_available_actions') is None:
+            self._available_actions = get_available_actions(self.cfg, page, self.user)
+            
         # Return a copy, so clients will not change the dict.
         return self._available_actions.copy()
 
@@ -1267,7 +1221,7 @@
 
                 msg = None
                 # Complain about unknown actions
-                if not action_name in self.getKnownActions():
+                if not action_name in get_names():
                     msg = _("Unknown action %(action_name)s.") % {
                             'action_name': wikiutil.escape(action_name), }