blob: a6e0d3e1f770a06acec04f1f018db282fbd7eb59 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# 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."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
Copybara854996b2021-09-07 19:36:02 +000011import time
12
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020013from google.cloud import exceptions
Copybara854996b2021-09-07 19:36:02 +000014from six import string_types
Copybara854996b2021-09-07 19:36:02 +000015import ezt
16
17from businesslogic import work_env
18from framework import emailfmt
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020019from framework import flaskservlet
Copybara854996b2021-09-07 19:36:02 +000020from framework import framework_helpers
21from framework import gcs_helpers
22from framework import permissions
23from framework import servlet
24from framework import urls
25from framework import validate
26from project import project_helpers
27from project import project_views
28from 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
36class 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ínezde942802022-07-15 14:06:55 +020040 _MAIN_TAB_MODE = flaskservlet.FlaskServlet.MAIN_TAB_ADMIN
Copybara854996b2021-09-07 19:36:02 +000041
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ínezde942802022-07-15 14:06:55 +0200139 except exceptions.NotFound:
Copybara854996b2021-09-07 19:36:02 +0000140 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ínezde942802022-07-15 14:06:55 +0200191
192 # def GetProjectAdminPage(self, **kwargs):
193 # return self.handler(**kwargs)
194
195 # def PostProjectAdminPage(self, **kwargs):
196 # return self.handler(**kwargs)