Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | # Copyright 2016 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 | |
| 6 | """A class to display a message explaining that a project has moved. |
| 7 | |
| 8 | When a project moves, we just display a link to the new location. |
| 9 | """ |
| 10 | from __future__ import print_function |
| 11 | from __future__ import division |
| 12 | from __future__ import absolute_import |
| 13 | |
| 14 | import logging |
| 15 | |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 16 | from framework import exceptions, flaskservlet |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 17 | from framework import framework_helpers |
| 18 | from framework import servlet |
| 19 | from framework import urls |
| 20 | from project import project_constants |
| 21 | |
| 22 | |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame^] | 23 | class ProjectMoved(flaskservlet.FlaskServlet): |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 24 | """The ProjectMoved page explains that the project has moved.""" |
| 25 | |
| 26 | _PAGE_TEMPLATE = 'sitewide/moved-page.ezt' |
| 27 | |
| 28 | def GatherPageData(self, mr): |
| 29 | """Build up a dictionary of data values to use when rendering the page.""" |
| 30 | |
| 31 | # We are not actually in /p/PROJECTNAME, so mr.project_name is None. |
| 32 | # Putting the ProjectMoved page inside a moved project would make |
| 33 | # the redirect logic much more complicated. |
| 34 | if not mr.specified_project: |
| 35 | raise exceptions.InputException('No project specified') |
| 36 | |
| 37 | project = self.services.project.GetProjectByName( |
| 38 | mr.cnxn, mr.specified_project) |
| 39 | if not project: |
| 40 | self.abort(404, 'project not found') |
| 41 | |
| 42 | if not project.moved_to: |
| 43 | # Only show this page for projects that are actually moved. |
| 44 | # Don't allow hackers to construct misleading links to this servlet. |
| 45 | logging.info('attempt to view ProjectMoved for non-moved project: %s', |
| 46 | mr.specified_project) |
| 47 | self.abort(400, 'This project has not been moved') |
| 48 | |
| 49 | if project_constants.RE_PROJECT_NAME.match(project.moved_to): |
| 50 | moved_to_url = framework_helpers.FormatAbsoluteURL( |
| 51 | mr, urls.SUMMARY, include_project=True, project_name=project.moved_to) |
| 52 | elif (project.moved_to.startswith('https://') or |
| 53 | project.moved_to.startswith('http://')): |
| 54 | moved_to_url = project.moved_to |
| 55 | else: |
| 56 | # Prevent users from using javascript: or any other tricky URL scheme. |
| 57 | moved_to_url = '#invalid-destination-url' |
| 58 | |
| 59 | return { |
| 60 | 'project_name': mr.specified_project, |
| 61 | 'moved_to_url': moved_to_url, |
| 62 | } |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 63 | |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame^] | 64 | def GetProjectMoved(self, **kwargs): |
| 65 | return self.handler(**kwargs) |