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