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