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 | """Servlets for project administration main subtab.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 11 | import time |
| 12 | |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame^] | 13 | from google.cloud import exceptions |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 14 | from six import string_types |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 15 | import ezt |
| 16 | |
| 17 | from businesslogic import work_env |
| 18 | from framework import emailfmt |
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 framework_helpers |
| 21 | from framework import gcs_helpers |
| 22 | from framework import permissions |
| 23 | from framework import servlet |
| 24 | from framework import urls |
| 25 | from framework import validate |
| 26 | from project import project_helpers |
| 27 | from project import project_views |
| 28 | from tracker import tracker_views |
| 29 | |
| 30 | |
| 31 | _MSG_INVALID_EMAIL_ADDRESS = 'Invalid email address' |
| 32 | _MSG_DESCRIPTION_MISSING = 'Description is missing' |
| 33 | _MSG_SUMMARY_MISSING = 'Summary is missing' |
| 34 | |
| 35 | |
| 36 | class ProjectAdmin(servlet.Servlet): |
| 37 | """A page with project configuration options for the Project Owner(s).""" |
| 38 | |
| 39 | _PAGE_TEMPLATE = 'project/project-admin-page.ezt' |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame^] | 40 | _MAIN_TAB_MODE = flaskservlet.FlaskServlet.MAIN_TAB_ADMIN |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 41 | |
| 42 | def AssertBasePermission(self, mr): |
| 43 | super(ProjectAdmin, self).AssertBasePermission(mr) |
| 44 | if not self.CheckPerm(mr, permissions.EDIT_PROJECT): |
| 45 | raise permissions.PermissionException( |
| 46 | 'User is not allowed to administer this project') |
| 47 | |
| 48 | def GatherPageData(self, mr): |
| 49 | """Build up a dictionary of data values to use when rendering the page.""" |
| 50 | available_access_levels = project_helpers.BuildProjectAccessOptions( |
| 51 | mr.project) |
| 52 | offer_access_level = len(available_access_levels) > 1 |
| 53 | access_view = project_views.ProjectAccessView(mr.project.access) |
| 54 | |
| 55 | return { |
| 56 | 'admin_tab_mode': |
| 57 | self.ADMIN_TAB_META, |
| 58 | 'initial_summary': |
| 59 | mr.project.summary, |
| 60 | 'initial_project_home': |
| 61 | mr.project.home_page, |
| 62 | 'initial_docs_url': |
| 63 | mr.project.docs_url, |
| 64 | 'initial_source_url': |
| 65 | mr.project.source_url, |
| 66 | 'initial_logo_gcs_id': |
| 67 | mr.project.logo_gcs_id, |
| 68 | 'initial_logo_file_name': |
| 69 | mr.project.logo_file_name, |
| 70 | 'logo_view': |
| 71 | tracker_views.LogoView(mr.project), |
| 72 | 'initial_description': |
| 73 | mr.project.description, |
| 74 | 'issue_notify': |
| 75 | mr.project.issue_notify_address, |
| 76 | 'process_inbound_email': |
| 77 | ezt.boolean(mr.project.process_inbound_email), |
| 78 | 'email_from_addr': |
| 79 | emailfmt.FormatFromAddr(mr.project), |
| 80 | 'only_owners_remove_restrictions': |
| 81 | ezt.boolean(mr.project.only_owners_remove_restrictions), |
| 82 | 'only_owners_see_contributors': |
| 83 | ezt.boolean(mr.project.only_owners_see_contributors), |
| 84 | 'offer_access_level': |
| 85 | ezt.boolean(offer_access_level), |
| 86 | 'initial_access': |
| 87 | access_view, |
| 88 | 'available_access_levels': |
| 89 | available_access_levels, |
| 90 | 'issue_notify_always_detailed': |
| 91 | ezt.boolean(mr.project.issue_notify_always_detailed), |
| 92 | } |
| 93 | |
| 94 | def ProcessFormData(self, mr, post_data): |
| 95 | """Process the posted form.""" |
| 96 | # 1. Parse and validate user input. |
| 97 | summary, description = self._ParseMeta(post_data, mr.errors) |
| 98 | access = project_helpers.ParseProjectAccess( |
| 99 | mr.project, post_data.get('access')) |
| 100 | |
| 101 | only_owners_remove_restrictions = ( |
| 102 | 'only_owners_remove_restrictions' in post_data) |
| 103 | only_owners_see_contributors = 'only_owners_see_contributors' in post_data |
| 104 | |
| 105 | issue_notify = post_data['issue_notify'] |
| 106 | if issue_notify and not validate.IsValidEmail(issue_notify): |
| 107 | mr.errors.issue_notify = _MSG_INVALID_EMAIL_ADDRESS |
| 108 | |
| 109 | process_inbound_email = 'process_inbound_email' in post_data |
| 110 | home_page = post_data.get('project_home') |
| 111 | if home_page and not ( |
| 112 | home_page.startswith('http:') or home_page.startswith('https:')): |
| 113 | mr.errors.project_home = 'Home page link must start with http: or https:' |
| 114 | docs_url = post_data.get('docs_url') |
| 115 | if docs_url and not ( |
| 116 | docs_url.startswith('http:') or docs_url.startswith('https:')): |
| 117 | mr.errors.docs_url = 'Documentation link must start with http: or https:' |
| 118 | source_url = post_data.get('source_url') |
| 119 | if source_url and not ( |
| 120 | source_url.startswith('http:') or source_url.startswith('https:')): |
| 121 | mr.errors.source_url = 'Source link must start with http: or https:' |
| 122 | |
| 123 | logo_gcs_id = '' |
| 124 | logo_file_name = '' |
| 125 | if 'logo' in post_data and not isinstance(post_data['logo'], string_types): |
| 126 | item = post_data['logo'] |
| 127 | logo_file_name = item.filename |
| 128 | try: |
| 129 | logo_gcs_id = gcs_helpers.StoreLogoInGCS( |
| 130 | logo_file_name, item.value, mr.project.project_id) |
| 131 | except gcs_helpers.UnsupportedMimeType, e: |
| 132 | mr.errors.logo = e.message |
| 133 | elif mr.project.logo_gcs_id and mr.project.logo_file_name: |
| 134 | logo_gcs_id = mr.project.logo_gcs_id |
| 135 | logo_file_name = mr.project.logo_file_name |
| 136 | if post_data.get('delete_logo'): |
| 137 | try: |
| 138 | gcs_helpers.DeleteObjectFromGCS(logo_gcs_id) |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame^] | 139 | except exceptions.NotFound: |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 140 | pass |
| 141 | # Reset the GCS ID and file name. |
| 142 | logo_gcs_id = '' |
| 143 | logo_file_name = '' |
| 144 | |
| 145 | issue_notify_always_detailed = 'issue_notify_always_detailed' in post_data |
| 146 | |
| 147 | # 2. Call services layer to save changes. |
| 148 | if not mr.errors.AnyErrors(): |
| 149 | with work_env.WorkEnv(mr, self.services) as we: |
| 150 | we.UpdateProject( |
| 151 | mr.project.project_id, |
| 152 | issue_notify_address=issue_notify, |
| 153 | summary=summary, |
| 154 | description=description, |
| 155 | only_owners_remove_restrictions=only_owners_remove_restrictions, |
| 156 | only_owners_see_contributors=only_owners_see_contributors, |
| 157 | process_inbound_email=process_inbound_email, |
| 158 | access=access, |
| 159 | home_page=home_page, |
| 160 | docs_url=docs_url, |
| 161 | source_url=source_url, |
| 162 | logo_gcs_id=logo_gcs_id, |
| 163 | logo_file_name=logo_file_name, |
| 164 | issue_notify_always_detailed=issue_notify_always_detailed) |
| 165 | |
| 166 | # 3. Determine the next page in the UI flow. |
| 167 | if mr.errors.AnyErrors(): |
| 168 | access_view = project_views.ProjectAccessView(access) |
| 169 | self.PleaseCorrect( |
| 170 | mr, initial_summary=summary, initial_description=description, |
| 171 | initial_access=access_view) |
| 172 | else: |
| 173 | return framework_helpers.FormatAbsoluteURL( |
| 174 | mr, urls.ADMIN_META, saved=1, ts=int(time.time())) |
| 175 | |
| 176 | def _ParseMeta(self, post_data, errors): |
| 177 | """Process a POST on the project metadata section of the admin page.""" |
| 178 | summary = None |
| 179 | description = None |
| 180 | |
| 181 | if 'summary' in post_data: |
| 182 | summary = post_data['summary'] |
| 183 | if not summary: |
| 184 | errors.summary = _MSG_SUMMARY_MISSING |
| 185 | if 'description' in post_data: |
| 186 | description = post_data['description'] |
| 187 | if not description: |
| 188 | errors.description = _MSG_DESCRIPTION_MISSING |
| 189 | |
| 190 | return summary, description |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame^] | 191 | |
| 192 | # def GetProjectAdminPage(self, **kwargs): |
| 193 | # return self.handler(**kwargs) |
| 194 | |
| 195 | # def PostProjectAdminPage(self, **kwargs): |
| 196 | # return self.handler(**kwargs) |