def test_getCache_redirected_view(self): # A redirection view may be provided with a target view instance from # which json cache data is obtained. class TestView(LaunchpadView): pass request = LaunchpadTestRequest() test_view = TestView(self.getCanada(), request) view = RedirectionView(None, request, cache_view=test_view) IJSONRequestCache(request).objects['my_bool'] = True json_dict = simplejson.loads(view.getCacheJSON()) self.assertIsCanada(json_dict['context']) self.assertIn('my_bool', json_dict)
def traverse(self, name): """Traverse the paths of a feed. If a query string is provided it is normalized. 'bugs' paths and persons ('~') are special cased. """ # Normalize the query string so caching is more effective. This is # done by simply sorting the entries. # XXX bac 20071019, we would like to normalize with respect to case # too but cannot due to a problem with the bug search requiring status # values to be of a particular case. See bug 154562. query_string = self.request.get('QUERY_STRING', '') fields = sorted(query_string.split('&')) normalized_query_string = '&'.join(fields) if query_string != normalized_query_string: # We must empty the traversal stack to prevent an error # when calling RedirectionView.publishTraverse(). self.request.setTraversalStack([]) target = "%s%s?%s" % (self.request.getApplicationURL(), self.request['PATH_INFO'], normalized_query_string) redirect = RedirectionView(target, self.request, 301) return redirect # Handle the two formats of urls: # http://feeds.launchpad.net/bugs/+bugs.atom?... # http://feeds.launchpad.net/bugs/1/bug.atom if name == 'bugs': stack = self.request.getTraversalStack() if len(stack) == 0: raise NotFound(self, '', self.request) bug_id = stack.pop() if bug_id.startswith('+'): if config.launchpad.is_bug_search_feed_active: return getUtility(IBugTaskSet) else: raise Unauthorized("Bug search feed deactivated") else: self.request.stepstogo.consume() return getUtility(IBugSet).getByNameOrID(bug_id) # Redirect to the canonical name before doing the lookup. if canonical_name(name) != name: return self.redirectSubTree(canonical_url(self.context) + canonical_name(name), status=301) try: if name.startswith('~'): # Handle persons and teams. # http://feeds.launchpad.net/~salgado/latest-bugs.html person = getUtility(IPersonSet).getByName(name[1:]) return person else: # Otherwise, handle products, projects, and distros return getUtility(IPillarNameSet)[name] except NotFoundError: raise NotFound(self, name, self.request)
def browserDefault(self, request): """Redirect to index.html if the directory itself is requested.""" if len(self.names) == 0: return RedirectionView( "%s+tour/index" % canonical_url(self.context), self.request, status=302), () else: return self, ()
def traverse_files(self, filename): """Traverse on filename in the archive domain.""" if not check_permission('launchpad.View', self.context): raise Unauthorized() library_file = self.context.getFileByName(filename) # Deleted library files result in NotFound-like error. if library_file.deleted: raise DeletedProxiedLibraryFileAlias(filename, self.context) # There can be no further path segments. if len(self.request.stepstogo) > 0: return None return RedirectionView(library_file.getURL(include_token=True), self.request)
def _getBetaRedirectionView(self): # If the inhibit_beta_redirect cookie is set, don't redirect. if self.request.cookies.get('inhibit_beta_redirect', '0') == '1': return None # If we are looking at the front page, don't redirect. if self.request['PATH_INFO'] == '/': return None # If this is a HTTP POST, we don't want to issue a redirect. # Doing so would go against the HTTP standard. if self.request.method == 'POST': return None # If this is a web service request, don't redirect. if WebServiceLayer.providedBy(self.request): return None # If the request is for a bug then redirect straight to that bug. bug_match = re.match("/bugs/(\d+)$", self.request['PATH_INFO']) if bug_match: bug_number = bug_match.group(1) bug_set = getUtility(IBugSet) try: bug = bug_set.get(bug_number) except NotFoundError: raise NotFound(self.context, bug_number) if not check_permission("launchpad.View", bug): return None # Empty the traversal stack, since we're redirecting. self.request.setTraversalStack([]) # And perform a temporary redirect. return RedirectionView(canonical_url(bug.default_bugtask), self.request, status=303) # Explicit catchall - do not redirect. return None
def test_getCache_redirected_view_default(self): # A redirection view by default provides no json cache data. request = LaunchpadTestRequest() view = RedirectionView(None, request) json_dict = simplejson.loads(view.getCacheJSON()) self.assertEqual({}, json_dict)