Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | # Copyright 2018 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 | """Classes that implement a web components page. |
| 7 | |
| 8 | Summary of classes: |
| 9 | WebComponentsPage: Show one web components page. |
| 10 | """ |
| 11 | from __future__ import print_function |
| 12 | from __future__ import division |
| 13 | from __future__ import absolute_import |
| 14 | |
| 15 | |
| 16 | import logging |
| 17 | |
| 18 | import settings |
| 19 | from framework import servlet |
| 20 | from framework import framework_helpers |
| 21 | from framework import permissions |
| 22 | from framework import urls |
| 23 | |
| 24 | |
| 25 | class WebComponentsPage(servlet.Servlet): |
| 26 | |
| 27 | _PAGE_TEMPLATE = 'tracker/web-components-page.ezt' |
| 28 | |
| 29 | def AssertBasePermission(self, mr): |
| 30 | # type: (MonorailRequest) -> None |
| 31 | """Check that the user has permission to visit this page.""" |
| 32 | super(WebComponentsPage, self).AssertBasePermission(mr) |
| 33 | |
| 34 | def GatherPageData(self, mr): |
| 35 | # type: (MonorailRequest) -> Mapping[str, Any] |
| 36 | """Build up a dictionary of data values to use when rendering the page. |
| 37 | |
| 38 | Args: |
| 39 | mr: commonly used info parsed from the request. |
| 40 | |
| 41 | Returns: |
| 42 | Dict of values used by EZT for rendering the page. |
| 43 | """ |
| 44 | # Create link to view in old UI for the list view pages. |
| 45 | old_ui_url = None |
| 46 | url = mr.request.url |
| 47 | if '/hotlists/' in url: |
| 48 | hotlist = self.services.features.GetHotlist(mr.cnxn, mr.hotlist_id) |
| 49 | if '/people' in url: |
| 50 | old_ui_url = '/u/%s/hotlists/%s/people' % ( |
| 51 | hotlist.owner_ids[0], hotlist.name) |
| 52 | elif '/settings' in url: |
| 53 | old_ui_url = '/u/%s/hotlists/%s/details' % ( |
| 54 | hotlist.owner_ids[0], hotlist.name) |
| 55 | else: |
| 56 | old_ui_url = '/u/%s/hotlists/%s' % (hotlist.owner_ids[0], hotlist.name) |
| 57 | |
| 58 | return { |
| 59 | 'local_id': mr.local_id, |
| 60 | 'old_ui_url': old_ui_url, |
| 61 | } |
| 62 | |
| 63 | |
| 64 | class ProjectListPage(WebComponentsPage): |
| 65 | |
| 66 | def GatherPageData(self, mr): |
| 67 | # type: (MonorailRequest) -> Mapping[str, Any] |
| 68 | """Build up a dictionary of data values to use when rendering the page. |
| 69 | |
| 70 | May redirect the user to a default project if one is configured for |
| 71 | the current domain. |
| 72 | |
| 73 | Args: |
| 74 | mr: commonly used info parsed from the request. |
| 75 | |
| 76 | Returns: |
| 77 | Dict of values used by EZT for rendering the page. |
| 78 | """ |
| 79 | redirect_msg = self._MaybeRedirectToDomainDefaultProject(mr) |
| 80 | logging.info(redirect_msg) |
| 81 | return { |
| 82 | 'local_id': None, |
| 83 | 'old_ui_url': '/hosting_old/', |
| 84 | } |
| 85 | |
| 86 | def _MaybeRedirectToDomainDefaultProject(self, mr): |
| 87 | # type: (MonorailRequest) -> str |
| 88 | """If there is a relevant default project, redirect to it. |
| 89 | |
| 90 | This function is copied from: sitewide/hostinghome.py |
| 91 | |
| 92 | Args: |
| 93 | mr: commonly used info parsed from the request. |
| 94 | |
| 95 | Returns: |
| 96 | String with a message about what happened for logging purposes. |
| 97 | """ |
| 98 | project_name = settings.domain_to_default_project.get(mr.request.host) |
| 99 | if not project_name: |
| 100 | return 'No configured default project redirect for this domain.' |
| 101 | |
| 102 | project = None |
| 103 | try: |
| 104 | project = self.services.project.GetProjectByName(mr.cnxn, project_name) |
| 105 | except exceptions.NoSuchProjectException: |
| 106 | pass |
| 107 | |
| 108 | if not project: |
| 109 | return 'Domain default project %s not found' % project_name |
| 110 | |
| 111 | if not permissions.UserCanViewProject(mr.auth.user_pb, |
| 112 | mr.auth.effective_ids, project): |
| 113 | return 'User cannot view default project: %r' % project |
| 114 | |
| 115 | project_url = '/p/%s' % project_name |
| 116 | self.redirect(project_url, abort=True) |
| 117 | return 'Redirected to %r' % project_url |