blob: 0073e57837bc3753bc1829bef8fa0c5024ed963f [file] [log] [blame]
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
"""This file sets up all the urls for monorail pages."""
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from six.moves import http_client
import logging
import webapp2
def MakeRedirect(redirect_to_this_uri, permanent=True):
"""Return a new request handler class that redirects to the given URL."""
class Redirect(webapp2.RequestHandler):
"""Redirect is a response handler that issues a redirect to another URI."""
def get(self, **_kw):
"""Send the 301/302 response code and write the Location: redirect."""
self.response.location = redirect_to_this_uri
'max-age=31536000; includeSubDomains')
self.response.status = (
http_client.MOVED_PERMANENTLY if permanent else http_client.FOUND)
return Redirect
def MakeRedirectInScope(uri_in_scope, scope, permanent=True, keep_qs=False):
"""Redirect to a URI within a given scope, e.g., per project or user.
uri_in_scope: a uri within a project or user starting with a slash.
scope: a string indicating the uri-space scope:
p for project pages
u for user pages
g for group pages
permanent: True for a HTTP 301 permanently moved response code,
otherwise a HTTP 302 temporarily moved response will be used.
keep_qs: set to True to make the redirect retain the query string.
When true, permanent is ignored.
redirect.MakeRedirectInScope('/newpage', 'p'), '/oldpage')
A class that can be used with webapp2.
assert uri_in_scope.startswith('/')
class RedirectInScope(webapp2.RequestHandler):
"""A handler that redirects to another URI in the same scope."""
def get(self, **_kw):
"""Send the 301/302 response code and write the Location: redirect."""
split_path = self.request.path.lstrip('/').split('/')
if len(split_path) > 1:
project_or_user = split_path[1]
url = '//%s/%s/%s%s' % (, scope, project_or_user, uri_in_scope)
url = '/'
if keep_qs and self.request.query_string:
url += '?' + self.request.query_string
self.response.location = url
'max-age=31536000; includeSubDomains')
if permanent and not keep_qs:
self.response.status = http_client.MOVED_PERMANENTLY
self.response.status = http_client.FOUND
return RedirectInScope