url = urljoin(context.absolute_url() + '/', url) elif not getToolByName(context, 'portal_url').isURLInPortal(url): url = context.absolute_url() url = self.updateQuery(url, controller_state.kwargs) request = context.REQUEST # this is mostly just for archetypes edit forms... if 'edit' in url and '_authenticator' not in url and \ '_authenticator' in request.form: if '?' in url: url += '&' else: url += '?' auth = request.form['_authenticator'] if isinstance(auth, list): auth = auth[0] url += '_authenticator=' + auth return request.RESPONSE.redirect(url) registerFormAction( 'redirect_to', factory, 'Redirect to the URL specified in the argument (a TALES expression). ' 'The URL can either be absolute or relative and must be internal.') # We register the original one under a new name, # to make it easier for anyone who needs this. registerFormAction( 'external_redirect_to', OrigRedirectTo.factory, 'Redirect to the URL specified in the argument (a TALES expression). ' 'The URL can either be absolute or relative, and may be external.')
# folder action action_ob = fti.getActionObject('object/'+action) if action_ob is None: action_ob = fti.getActionObject('folder/'+action) action_url = action_ob.getActionExpression() haveAction = True except (ValueError, AttributeError): actions_tool = getToolByName(context, 'portal_actions') actions = actions_tool.listFilteredActionsFor( controller_state.getContext()) # flatten the actions as we don't care where they are actions = reduce(lambda x,y,a=actions: x+a[y], actions.keys(), []) for actiondict in actions: if actiondict['id'] == action: action_url = actiondict['url'].strip() haveAction = True break # (note: action_url may now be an emptry string, but still valid) if not haveAction: raise ValueError('No %s action found for %s' % (action, controller_state.getContext().getId())) # XXX: Is there a better way to check this? if not action_url.startswith('string:'): action_url = 'string:%s' % (action_url,) return RedirectTo.RedirectTo(action_url)(controller_state) registerFormAction('redirect_to_action', factory, 'Redirect to the action specified in the argument (a TALES expression) for the current context object (e.g. string:view)')
def __call__(self, controller_state): url = self.getArg(controller_state) # see if this is a relative url or an absolute if len(urlparse(url)[1]) != 0: # host specified, so url is absolute. No good for traversal. raise ValueError('Can\'t traverse to absolute url %s' % str(url)) url_path = urlparse(url)[2] # combine args from query string with args from the controller state # (args in the state supercede those in the query string) args = self.combineArgs(url, controller_state.kwargs) # put the args in the REQUEST REQUEST = controller_state.getContext().REQUEST for (key, value) in args.items(): REQUEST.set(key, value) # make sure target exists context = controller_state.getContext() obj = context.restrictedTraverse(url_path, default=None) if obj is None: raise ValueError('Unable to find %s\n' % str(url_path)) return mapply(obj, REQUEST.args, REQUEST, call_object, 1, missing_name, dont_publish_class, REQUEST, bind=1) registerFormAction('traverse_to', factory, 'Traverse to the URL specified in the argument (a TALES expression). The URL must be a relative URL.')
from BaseFormAction import BaseFormAction from Products.CMFFormController.FormController import registerFormAction from urlparse import urlparse, urljoin def factory(arg): """Create a new redirect-to action""" return RedirectTo(arg) class RedirectTo(BaseFormAction): def __call__(self, controller_state): url = self.getArg(controller_state) context = controller_state.getContext() # see if this is a relative url or an absolute if len(urlparse(url)[1]) == 0: # No host specified, so url is relative. Get an absolute url. url = urljoin(context.absolute_url() + '/', url) url = self.updateQuery(url, controller_state.kwargs) return context.REQUEST.RESPONSE.redirect(url) registerFormAction( 'redirect_to', factory, 'Redirect to the URL specified in the argument (a TALES expression). The URL can either be absolute or relative.' )
else: raise ValueError, 'No %s action found for %s' % (action, controller_state.getContext().getId()) # If we have CMF 1.5, the actual action_url may be hidden behind a method # alias. Attempt to resolve this try: if action_url: # If our url is a path, then we need to see if it contains the # path to the current object, if so we need to check if the # remaining path element is a method alias possible_alias = action_url current_path = '/'.join(context.getPhysicalPath()) if possible_alias.startswith(current_path): possible_alias = possible_alias[len(current_path)+1:] if possible_alias: action_url = fti.queryMethodID(possible_alias, default = action_url, context = context) except AttributeError: # Don't raise if we don't have CMF 1.5 pass # XXX: Is there a better way to check this? if not action_url.startswith('string:'): action_url = 'string:%s' % (action_url,) return TraverseTo.TraverseTo(action_url)(controller_state) registerFormAction('traverse_to_action', factory, 'Traverse to the action specified in the argument (a TALES expression) for the current context object (e.g. string:view)')
def __call__(self, controller_state): url = self.getArg(controller_state) # see if this is a relative url or an absolute if len(urlparse.urlparse(url)[1]) != 0: # host specified, so url is absolute. No good for traversal. raise ValueError, 'Can\'t traverse to absolute url %s' % str(url) url_path = urlparse.urlparse(url)[2] # combine args from query string with args from the controller state # (args in the state supercede those in the query string) args = self.combineArgs(url, controller_state.kwargs) # put the args in the REQUEST REQUEST = controller_state.getContext().REQUEST for (key, value) in args.items(): REQUEST.set(key, value) # make sure target exists context = controller_state.getContext() obj = context.restrictedTraverse(url_path, default=None) if obj is None: raise ValueError, 'Unable to find %s\n' % str(url_path) return mapply(obj, REQUEST.args, REQUEST, call_object, 1, missing_name, dont_publish_class, REQUEST, bind=1) registerFormAction('traverse_to', factory, 'Traverse to the URL specified in the argument (a TALES expression). The URL must be a relative URL.')
# this is mostly just for archetypes edit forms... if 'edit' in url and '_authenticator' not in url and \ '_authenticator' in request.form: if '?' in url: url += '&' else: url += '?' auth = request.form['_authenticator'] if isinstance(auth, list): auth = auth[0] url += '_authenticator=' + auth return request.RESPONSE.redirect(url) class ExternalRedirectTo(RedirectTo): allow_external_url = True registerFormAction( 'redirect_to', factory, 'Redirect to the URL specified in the argument (a TALES expression). ' 'The URL can either be absolute or relative, and must be internal.') registerFormAction( 'external_redirect_to', factory_external, 'Redirect to the URL specified in the argument (a TALES expression). ' 'The URL can either be absolute or relative, and may be external.')