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 hotlist details main subtab.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import time |
| 12 | |
| 13 | import ezt |
| 14 | |
| 15 | from features import hotlist_helpers |
| 16 | from framework import framework_bizobj |
| 17 | from framework import framework_helpers |
| 18 | from framework import servlet |
| 19 | from framework import permissions |
| 20 | from 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 | |
| 31 | class 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 |