blob: d3bf3b21e3f3c3523b232ca7593ac9ad138c1225 [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 hotlist details main subtab."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import time
12
13import ezt
14
15from features import hotlist_helpers
16from framework import framework_bizobj
17from framework import framework_helpers
18from framework import servlet
19from framework import permissions
20from framework import urls
21
22_MSG_DESCRIPTION_MISSING = 'Description is missing.'
23_MSG_SUMMARY_MISSING = 'Summary is missing.'
24_MSG_NAME_MISSING = 'Hotlist name is missing.'
25_MSG_COL_SPEC_MISSING = 'Hotlist default columns are missing.'
26_MSG_HOTLIST_NAME_NOT_AVAIL = 'You already have a hotlist with that name.'
27# pylint: disable=line-too-long
28_MSG_INVALID_HOTLIST_NAME = "Invalid hotlist name. Please make sure your hotlist name begins with a letter followed by any number of letters, numbers, -'s, and .'s"
29
30
31class HotlistDetails(servlet.Servlet):
32 """A page with hotlist details and editing options."""
33
34 _PAGE_TEMPLATE = 'features/hotlist-details-page.ezt'
35 _MAIN_TAB_MODE = servlet.Servlet.HOTLIST_TAB_DETAILS
36
37 def AssertBasePermission(self, mr):
38 super(HotlistDetails, self).AssertBasePermission(mr)
39 if not permissions.CanViewHotlist(
40 mr.auth.effective_ids, mr.perms, mr.hotlist):
41 raise permissions.PermissionException(
42 'User is not allowed to view the hotlist details')
43
44 def GatherPageData(self, mr):
45 """Buil up a dictionary of data values to use when rendering the page."""
46 if mr.auth.user_id:
47 self.services.user.AddVisitedHotlist(
48 mr.cnxn, mr.auth.user_id, mr.hotlist_id)
49 cant_administer_hotlist = not permissions.CanAdministerHotlist(
50 mr.auth.effective_ids, mr.perms, mr.hotlist)
51
52 return {
53 'initial_summary': mr.hotlist.summary,
54 'initial_description': mr.hotlist.description,
55 'initial_name': mr.hotlist.name,
56 'initial_default_col_spec': mr.hotlist.default_col_spec,
57 'initial_is_private': ezt.boolean(mr.hotlist.is_private),
58 'cant_administer_hotlist': ezt.boolean(cant_administer_hotlist),
59 'viewing_user_page': ezt.boolean(True),
60 'new_ui_url': '%s/%s/settings' % (urls.HOTLISTS, mr.hotlist_id),
61 }
62
63 def ProcessFormData(self, mr, post_data):
64 """Process the posted form."""
65 if not permissions.CanAdministerHotlist(
66 mr.auth.effective_ids, mr.perms, mr.hotlist):
67 raise permissions.PermissionException(
68 'User is not allowed to update hotlist settings.')
69
70 if post_data.get('deletestate') == 'true':
71 hotlist_helpers.RemoveHotlist(mr.cnxn, mr.hotlist_id, self.services)
72 return framework_helpers.FormatAbsoluteURL(
73 mr, '/u/%s/hotlists' % mr.auth.email,
74 saved=1, ts=int(time.time()), include_project=False)
75
76 (summary, description, name, default_col_spec) = self._ParseMetaData(
77 post_data, mr)
78 is_private = post_data.get('is_private') != 'no'
79
80 if not mr.errors.AnyErrors():
81 self.services.features.UpdateHotlist(
82 mr.cnxn, mr.hotlist.hotlist_id, name=name, summary=summary,
83 description=description, is_private=is_private,
84 default_col_spec=default_col_spec)
85
86 if mr.errors.AnyErrors():
87 self.PleaseCorrect(
88 mr, initial_summary=summary, initial_description=description,
89 initial_name=name, initial_default_col_spec=default_col_spec)
90 else:
91 return framework_helpers.FormatAbsoluteURL(
92 mr, '/u/%s/hotlists/%s%s' % (
93 mr.auth.user_id, mr.hotlist_id, urls.HOTLIST_DETAIL),
94 saved=1, ts=int(time.time()),
95 include_project=False)
96
97 def _ParseMetaData(self, post_data, mr):
98 """Process a POST on the hotlist metadata."""
99 summary = None
100 description = ''
101 name = None
102 default_col_spec = None
103
104 if 'summary' in post_data:
105 summary = post_data['summary']
106 if not summary:
107 mr.errors.summary = _MSG_SUMMARY_MISSING
108 if 'description' in post_data:
109 description = post_data['description']
110 if 'name' in post_data:
111 name = post_data['name']
112 if not name:
113 mr.errors.name = _MSG_NAME_MISSING
114 else:
115 if not framework_bizobj.IsValidHotlistName(name):
116 mr.errors.name = _MSG_INVALID_HOTLIST_NAME
117 elif self.services.features.LookupHotlistIDs(
118 mr.cnxn, [name], [mr.auth.user_id]) and (
119 mr.hotlist.name.lower() != name.lower()):
120 mr.errors.name = _MSG_HOTLIST_NAME_NOT_AVAIL
121 if 'default_col_spec' in post_data:
122 default_col_spec = post_data['default_col_spec']
123 return summary, description, name, default_col_spec