Adrià Vilanova MartÃnez | ac4a644 | 2022-05-15 19:05:13 +0200 | [diff] [blame] | 1 | # Copyright 2022 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style |
| 3 | # license that can be found in the LICENSE file or at |
| 4 | # https://developers.google.com/open-source/licenses/bsd |
| 5 | """This file sets up all the urls for monorail pages.""" |
| 6 | |
| 7 | import logging |
| 8 | from framework import excessiveactivity |
| 9 | import settings |
| 10 | from flask import Flask |
| 11 | |
| 12 | from project import project_constants |
| 13 | |
| 14 | |
| 15 | class ServletRegistry(object): |
| 16 | |
| 17 | _PROJECT_NAME_REGEX = project_constants.PROJECT_NAME_PATTERN |
| 18 | _USERNAME_REGEX = r'[-+\w=.%]+(@([-a-z0-9]+\.)*[a-z0-9]+)?' |
| 19 | _HOTLIST_ID_NAME_REGEX = r'\d+|[a-zA-Z][-0-9a-zA-Z\.]*' |
| 20 | |
| 21 | def __init__(self): |
| 22 | self.routes = [] |
| 23 | |
| 24 | def _AddRoute( |
| 25 | self, path_regex, servlet_handler, method='GET', does_write=False): |
| 26 | """Add a GET or POST handler to our flask route list. |
| 27 | |
| 28 | Args: |
| 29 | path_regex: string with flask URL template regex. |
| 30 | servlet_handler: a servlet handler function. |
| 31 | method: string 'GET' or 'POST'. |
| 32 | does_write: True if the servlet could write to the database, we skip |
| 33 | registering such servlets when the site is in read_only mode. GET |
| 34 | handlers never write. Most, but not all, POST handlers do write. |
| 35 | """ |
| 36 | if settings.read_only and does_write: |
| 37 | logging.info('Not registring %r because site is read-only', path_regex) |
| 38 | else: |
| 39 | self.routes.append([path_regex, servlet_handler, [method]]) |
| 40 | |
| 41 | def _SetupServlets(self, spec_dict, base='', post_does_write=True): |
| 42 | """Register each of the given servlets.""" |
| 43 | for get_uri, servlet_handler in spec_dict.items(): |
| 44 | self._AddRoute(base + get_uri, servlet_handler, 'GET') |
| 45 | post_uri = get_uri + ('edit.do' if get_uri.endswith('/') else '.do') |
| 46 | self._AddRoute( |
| 47 | base + post_uri, servlet_handler, 'POST', does_write=post_does_write) |
| 48 | |
| 49 | def Register(self): |
| 50 | """Register all the monorail request handlers.""" |
| 51 | return self.routes |
| 52 | |
| 53 | def RegisterExcesiveActivity(self, service): |
| 54 | flaskapp_excessive_activity = Flask(__name__) |
| 55 | flaskapp_excessive_activity.add_url_rule( |
| 56 | '/', |
| 57 | view_func=excessiveactivity.ExcessiveActivity(services=service).handler, |
| 58 | methods=['GET']) |
| 59 | return flaskapp_excessive_activity |