blob: 76cf37802a27371df9c1e6a4271167a902adf7d2 [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"""A servlet for project and component owners to view and edit field defs."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import logging
12import time
13import re
14
15import ezt
16
17from framework import exceptions
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020018from framework import flaskservlet
Copybara854996b2021-09-07 19:36:02 +000019from framework import framework_helpers
20from framework import framework_views
21from framework import permissions
22from framework import servlet
23from framework import urls
24from proto import tracker_pb2
25from tracker import field_helpers
26from tracker import tracker_bizobj
27from tracker import tracker_helpers
28from tracker import tracker_views
29
30
31class FieldDetail(servlet.Servlet):
32 """Servlet allowing project owners to view and edit a custom field."""
33
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020034 _MAIN_TAB_MODE = flaskservlet.FlaskServlet.MAIN_TAB_PROCESS
Copybara854996b2021-09-07 19:36:02 +000035 _PAGE_TEMPLATE = 'tracker/field-detail-page.ezt'
36
37 def _GetFieldDef(self, mr):
38 """Get the config and field definition to be viewed or edited."""
39 # TODO(jrobbins): since so many requests get the config object, and
40 # it is usually cached in RAM, just always get it and include it
41 # in the MonorailRequest, mr.
42 config = self.services.config.GetProjectConfig(
43 mr.cnxn, mr.project_id, use_cache=False)
44 field_def = tracker_bizobj.FindFieldDef(mr.field_name, config)
45 if not field_def:
46 self.abort(404, 'custom field not found')
47 return config, field_def
48
49 def AssertBasePermission(self, mr):
50 """Check whether the user has any permission to visit this page.
51
52 Args:
53 mr: commonly used info parsed from the request.
54 """
55 super(FieldDetail, self).AssertBasePermission(mr)
56 _config, field_def = self._GetFieldDef(mr)
57
58 allow_view = permissions.CanViewFieldDef(
59 mr.auth.effective_ids, mr.perms, mr.project, field_def)
60 if not allow_view:
61 raise permissions.PermissionException(
62 'User is not allowed to view this field definition')
63
64 def GatherPageData(self, mr):
65 """Build up a dictionary of data values to use when rendering the page.
66
67 Args:
68 mr: commonly used info parsed from the request.
69
70 Returns:
71 Dict of values used by EZT for rendering the page.
72 """
73 config, field_def = self._GetFieldDef(mr)
74 approval_def, subfields = None, []
75 if field_def.field_type == tracker_pb2.FieldTypes.APPROVAL_TYPE:
76 approval_def = tracker_bizobj.FindApprovalDefByID(
77 field_def.field_id, config)
78 user_views = framework_views.MakeAllUserViews(
79 mr.cnxn, self.services.user, field_def.admin_ids,
80 approval_def.approver_ids)
81 subfields = tracker_bizobj.FindApprovalsSubfields(
82 [field_def.field_id], config)[field_def.field_id]
83 else:
84 user_views = framework_views.MakeAllUserViews(
85 mr.cnxn, self.services.user, field_def.admin_ids,
86 field_def.editor_ids)
87 field_def_view = tracker_views.FieldDefView(
88 field_def, config, user_views=user_views, approval_def=approval_def)
89
90 well_known_issue_types = tracker_helpers.FilterIssueTypes(config)
91
92 allow_edit = permissions.CanEditFieldDef(
93 mr.auth.effective_ids, mr.perms, mr.project, field_def)
94
95 # Right now we do not allow renaming of enum fields.
96 _uneditable_name = field_def.field_type == tracker_pb2.FieldTypes.ENUM_TYPE
97
98 initial_choices = '\n'.join(
99 [choice.name if not choice.docstring else (
100 choice.name + ' = ' + choice.docstring) for
101 choice in field_def_view.choices])
102
103 initial_approvers = ', '.join(sorted([
104 approver_view.email for approver_view in field_def_view.approvers]))
105
106 initial_admins = ', '.join(sorted([
107 uv.email for uv in field_def_view.admins]))
108 initial_editors = ', '.join(
109 sorted([uv.email for uv in field_def_view.editors]))
110
111 return {
112 'admin_tab_mode': servlet.Servlet.PROCESS_TAB_LABELS,
113 'field_def': field_def_view,
114 'allow_edit': ezt.boolean(allow_edit),
115 # TODO(jojwang): update when name changes are actually saved
116 'uneditable_name': ezt.boolean(True),
117 'initial_admins': initial_admins,
118 'initial_editors': initial_editors,
119 'initial_applicable_type': field_def.applicable_type,
120 'initial_applicable_predicate': field_def.applicable_predicate,
121 'initial_approvers': initial_approvers,
122 'initial_choices': initial_choices,
123 'approval_subfields': [fd for fd in subfields if not fd.is_deleted],
124 'well_known_issue_types': well_known_issue_types,
125 }
126
127 def ProcessFormData(self, mr, post_data):
128 """Validate and store the contents of the issues tracker admin page.
129
130 Args:
131 mr: commonly used info parsed from the request.
132 post_data: HTML form data from the request.
133
134 Returns:
135 String URL to redirect the user to, or None if response was already sent.
136 """
137 config, field_def = self._GetFieldDef(mr)
138 allow_edit = permissions.CanEditFieldDef(
139 mr.auth.effective_ids, mr.perms, mr.project, field_def)
140 if not allow_edit:
141 raise permissions.PermissionException(
142 'User is not allowed to delete this field')
143
144 if 'deletefield' in post_data:
145 return self._ProcessDeleteField(mr, config, field_def)
146 elif 'cancel' in post_data:
147 return framework_helpers.FormatAbsoluteURL(
148 mr, urls.ADMIN_LABELS, ts=int(time.time()))
149 else:
150 return self._ProcessEditField(mr, post_data, config, field_def)
151
152
153 def _ProcessDeleteField(self, mr, config, field_def):
154 """The user wants to delete the specified custom field definition."""
155 field_ids = [field_def.field_id]
156 if field_def.field_type is tracker_pb2.FieldTypes.APPROVAL_TYPE:
157 for fd in config.field_defs:
158 if fd.approval_id == field_def.field_id:
159 field_ids.append(fd.field_id)
160 self.services.config.SoftDeleteFieldDefs(
161 mr.cnxn, mr.project_id, field_ids)
162
163 return framework_helpers.FormatAbsoluteURL(
164 mr, urls.ADMIN_LABELS, deleted=1, ts=int(time.time()))
165
166 # TODO(jrobbins): add logic to reaper cron task to look for
167 # soft deleted field definitions that have no issues with
168 # any value and hard deleted them.
169
170 def _ProcessEditField(self, mr, post_data, config, field_def):
171 """The user wants to edit this field definition."""
172 # TODO(jrobbins): future feature: editable field names
173
174 parsed = field_helpers.ParseFieldDefRequest(post_data, config)
175
176 admin_ids, admin_str = tracker_helpers.ParsePostDataUsers(
177 mr.cnxn, post_data['admin_names'], self.services.user)
178 editor_ids, editor_str = tracker_helpers.ParsePostDataUsers(
179 mr.cnxn, post_data.get('editor_names', ''), self.services.user)
180
181 field_helpers.ParsedFieldDefAssertions(mr, parsed)
182
183 if not parsed.is_restricted_field:
184 assert not editor_ids, 'Editors are only for restricted fields.'
185
186 if field_def.field_type == tracker_pb2.FieldTypes.APPROVAL_TYPE:
187 assert not (
188 parsed.is_restricted_field), 'Approval fields cannot be restricted.'
189 assert not editor_ids, 'Approval fields cannot have editors.'
190
191 if parsed.approvers_str:
192 approver_ids_dict = self.services.user.LookupUserIDs(
193 mr.cnxn, re.split('[,;\s]+', parsed.approvers_str),
194 autocreate=True)
195 approver_ids = list(set(approver_ids_dict.values()))
196 else:
197 mr.errors.approvers = 'Please provide at least one default approver.'
198
199 if mr.errors.AnyErrors():
200 new_field_def = field_helpers.ReviseFieldDefFromParsed(parsed, field_def)
201
202 new_field_def_view = tracker_views.FieldDefView(
203 new_field_def, config)
204
205 self.PleaseCorrect(
206 mr,
207 field_def=new_field_def_view,
208 initial_applicable_type=parsed.applicable_type,
209 initial_choices=parsed.choices_text,
210 initial_admins=admin_str,
211 initial_editors=editor_str,
212 initial_approvers=parsed.approvers_str,
213 initial_is_restricted_field=parsed.is_restricted_field)
214 return
215
216 self.services.config.UpdateFieldDef(
217 mr.cnxn,
218 mr.project_id,
219 field_def.field_id,
220 applicable_type=parsed.applicable_type,
221 applicable_predicate=parsed.applicable_predicate,
222 is_required=parsed.is_required,
223 is_niche=parsed.is_niche,
224 min_value=parsed.min_value,
225 max_value=parsed.max_value,
226 regex=parsed.regex,
227 needs_member=parsed.needs_member,
228 needs_perm=parsed.needs_perm,
229 grants_perm=parsed.grants_perm,
230 notify_on=parsed.notify_on,
231 is_multivalued=parsed.is_multivalued,
232 date_action=parsed.date_action_str,
233 docstring=parsed.field_docstring,
234 admin_ids=admin_ids,
235 editor_ids=editor_ids,
236 is_restricted_field=parsed.is_restricted_field)
237
238 if field_def.field_type == tracker_pb2.FieldTypes.APPROVAL_TYPE:
239 approval_defs = field_helpers.ReviseApprovals(
240 field_def.field_id, approver_ids, parsed.survey, config)
241 self.services.config.UpdateConfig(
242 mr.cnxn, mr.project, approval_defs=approval_defs)
243
244 if field_def.field_type == tracker_pb2.FieldTypes.ENUM_TYPE:
245 self.services.config.UpdateConfig(
246 mr.cnxn, mr.project, well_known_labels=parsed.revised_labels)
247
248 return framework_helpers.FormatAbsoluteURL(
249 mr, urls.FIELD_DETAIL, field=field_def.field_name,
250 saved=1, ts=int(time.time()))
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +0200251
252 # def GetFieldDetail(self, **kwargs):
253 # return self.handler(**kwargs)
254
255 # def PostFieldDetail(self, **kwargs):
256 # return self.handler(**kwargs)