Back to index

plone3  3.1.7
RedirectToAction.py
Go to the documentation of this file.
00001 from BaseFormAction import BaseFormAction
00002 import RedirectTo
00003 
00004 from Products.CMFCore.utils import getToolByName
00005 from Products.CMFFormController.FormController import registerFormAction
00006 
00007 def factory(arg):
00008     """Create a new redirect-to-action action"""
00009     return RedirectToAction(arg)
00010 
00011 
00012 class RedirectToAction(BaseFormAction):
00013 
00014     def __call__(self, controller_state):
00015         action = self.getArg(controller_state)
00016         action_url = None
00017         haveAction = False
00018 
00019         context = controller_state.getContext()
00020         fti = context.getTypeInfo()
00021 
00022         try:
00023             # Test to see if the action is defined in the FTI as an object or
00024             # folder action
00025             action_ob = fti.getActionObject('object/'+action)
00026             if action_ob is None:
00027                 action_ob = fti.getActionObject('folder/'+action)
00028             action_url = action_ob.getActionExpression()
00029             haveAction = True
00030         except (ValueError, AttributeError):
00031             actions_tool = getToolByName(context, 'portal_actions')
00032             actions = actions_tool.listFilteredActionsFor(
00033                                                 controller_state.getContext())
00034             # flatten the actions as we don't care where they are
00035             actions = reduce(lambda x,y,a=actions:  x+a[y], actions.keys(), [])
00036             for actiondict in actions:
00037                 if actiondict['id'] == action:
00038                     action_url = actiondict['url'].strip()
00039                     haveAction = True
00040                     break
00041 
00042         # (note: action_url may now be an emptry string, but still valid)
00043         if not haveAction:
00044             raise ValueError, 'No %s action found for %s' % (action, controller_state.getContext().getId())
00045 
00046         # XXX: Is there a better way to check this?
00047         if not action_url.startswith('string:'):
00048             action_url = 'string:%s' % (action_url,)
00049         return RedirectTo.RedirectTo(action_url)(controller_state)
00050 
00051 registerFormAction('redirect_to_action',
00052                    factory,
00053                    'Redirect to the action specified in the argument (a TALES expression) for the current context object (e.g. string:view)')