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