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