Back to index

plone3  3.1.7
TraverseTo.py
Go to the documentation of this file.
00001 from BaseFormAction import BaseFormAction
00002 
00003 from Products.CMFFormController.FormController import registerFormAction
00004 from ZPublisher.Publish import call_object, missing_name, dont_publish_class
00005 from ZPublisher.mapply import mapply
00006 import urlparse
00007 
00008 def factory(arg):
00009     """Create a new traverse-to action"""
00010     return TraverseTo(arg)
00011 
00012 
00013 class TraverseTo(BaseFormAction):
00014     def __call__(self, controller_state):
00015         url = self.getArg(controller_state)
00016         # see if this is a relative url or an absolute
00017         if len(urlparse.urlparse(url)[1]) != 0:
00018             # host specified, so url is absolute.  No good for traversal.
00019             raise ValueError, 'Can\'t traverse to absolute url %s' % str(url)
00020 
00021         url_path = urlparse.urlparse(url)[2]
00022         # combine args from query string with args from the controller state
00023         # (args in the state supercede those in the query string)
00024         args = self.combineArgs(url, controller_state.kwargs)
00025 
00026         # put the args in the REQUEST
00027         REQUEST = controller_state.getContext().REQUEST
00028         for (key, value) in args.items():
00029             REQUEST.set(key, value)
00030 
00031         # make sure target exists
00032         context = controller_state.getContext()
00033         obj = context.restrictedTraverse(url_path, default=None)
00034         if obj is None:
00035             raise ValueError, 'Unable to find %s\n' % str(url_path)
00036         return mapply(obj, REQUEST.args, REQUEST,
00037                                call_object, 1, missing_name, dont_publish_class,
00038                                REQUEST, bind=1)
00039 
00040 
00041 registerFormAction('traverse_to',
00042                    factory,
00043                    'Traverse to the URL specified in the argument (a TALES expression).  The URL must be a relative URL.')