def test_traverse(url): """Traverse the url in the same way normal publishing occurs. Returns a tuple of (object, view, request) where: object is the last model object in the traversal chain view is the defined view for the object at the specified url (if the url didn't directly specify a view, then the view is the default view for the object. request is the request object resulting from the traversal. This contains a populated traversed_objects list just as a browser request would from a normal call into the app servers. This call uses the currently logged in user, and does not start a new transaction. """ url_parts = urlsplit(url) server_url = '://'.join(url_parts[0:2]) path_info = url_parts[2] request, publication = get_request_and_publication( host=url_parts[1], extra_environment={ 'SERVER_URL': server_url, 'PATH_INFO': path_info}) request.setPublication(publication) # We avoid calling publication.beforePublication because this starts a new # transaction, which causes an abort of the existing transaction, and the # removal of any created and uncommitted objects. # Set the default layer. adapters = getGlobalSiteManager().adapters layer = adapters.lookup((providedBy(request),), IDefaultSkin, '') if layer is not None: layers.setAdditionalLayer(request, layer) principal = get_current_principal() if IUnauthenticatedPrincipal.providedBy(principal): login = None else: login = principal.person setupInteraction(principal, login, request) getUtility(IOpenLaunchBag).clear() app = publication.getApplication(request) view = request.traverse(app) # Find the object from the view instead on relying that it stays # in the traversed_objects stack. That doesn't apply to the web # service for example. try: obj = removeSecurityProxy(view).context except AttributeError: # But sometime the view didn't store the context... # Use the last traversed object in these cases. obj = request.traversed_objects[-2] restoreInteraction() return obj, view, request
def beforeTraversal(self, request): notify(StartRequestEvent(request)) request._traversalticks_start = tickcount.tickcount() threadid = thread.get_ident() threadrequestfile = open_for_writing( 'logs/thread-%s.request' % threadid, 'w') try: request_txt = unicode(request).encode('UTF-8') except Exception: request_txt = 'Exception converting request to string\n\n' try: request_txt += traceback.format_exc() except: request_txt += 'Unable to render traceback!' threadrequestfile.write(request_txt) threadrequestfile.close() # Tell our custom database adapter that the request has started. da.set_request_started() newInteraction(request) transaction.begin() # Now we are logged in, install the correct IDatabasePolicy for # this request. db_policy = IDatabasePolicy(request) getUtility(IStoreSelector).push(db_policy) getUtility(IOpenLaunchBag).clear() # Set the default layer. adapters = getGlobalSiteManager().adapters layer = adapters.lookup((providedBy(request), ), IDefaultSkin, '') if layer is not None: layers.setAdditionalLayer(request, layer) principal = self.getPrincipal(request) request.setPrincipal(principal) self.maybeRestrictToTeam(request) maybe_block_offsite_form_post(request)
def beforeTraversal(self, request): notify(StartRequestEvent(request)) request._traversalticks_start = tickcount.tickcount() threadid = thread.get_ident() threadrequestfile = open_for_writing( 'logs/thread-%s.request' % threadid, 'w') try: request_txt = unicode(request).encode('UTF-8') except Exception: request_txt = 'Exception converting request to string\n\n' try: request_txt += traceback.format_exc() except: request_txt += 'Unable to render traceback!' threadrequestfile.write(request_txt) threadrequestfile.close() # Tell our custom database adapter that the request has started. da.set_request_started() newInteraction(request) transaction.begin() # Now we are logged in, install the correct IDatabasePolicy for # this request. db_policy = IDatabasePolicy(request) getUtility(IStoreSelector).push(db_policy) getUtility(IOpenLaunchBag).clear() # Set the default layer. adapters = getGlobalSiteManager().adapters layer = adapters.lookup((providedBy(request),), IDefaultSkin, '') if layer is not None: layers.setAdditionalLayer(request, layer) principal = self.getPrincipal(request) request.setPrincipal(principal) self.maybeRestrictToTeam(request) maybe_block_offsite_form_post(request)
def test_traverse(url): """Traverse the url in the same way normal publishing occurs. Returns a tuple of (object, view, request) where: object is the last model object in the traversal chain view is the defined view for the object at the specified url (if the url didn't directly specify a view, then the view is the default view for the object. request is the request object resulting from the traversal. This contains a populated traversed_objects list just as a browser request would from a normal call into the app servers. This call uses the currently logged in user, and does not start a new transaction. """ url_parts = urlsplit(url) server_url = '://'.join(url_parts[0:2]) path_info = url_parts[2] request, publication = get_request_and_publication(host=url_parts[1], extra_environment={ 'SERVER_URL': server_url, 'PATH_INFO': path_info }) request.setPublication(publication) # We avoid calling publication.beforePublication because this starts a new # transaction, which causes an abort of the existing transaction, and the # removal of any created and uncommitted objects. # Set the default layer. adapters = getGlobalSiteManager().adapters layer = adapters.lookup((providedBy(request), ), IDefaultSkin, '') if layer is not None: layers.setAdditionalLayer(request, layer) principal = get_current_principal() if IUnauthenticatedPrincipal.providedBy(principal): login = None else: login = principal.person setupInteraction(principal, login, request) getUtility(IOpenLaunchBag).clear() app = publication.getApplication(request) view = request.traverse(app) # Find the object from the view instead on relying that it stays # in the traversed_objects stack. That doesn't apply to the web # service for example. try: obj = removeSecurityProxy(view).context except AttributeError: # But sometime the view didn't store the context... # Use the last traversed object in these cases. obj = request.traversed_objects[-2] restoreInteraction() return obj, view, request