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 | """Servlet for creating new hotlists.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import logging |
| 12 | import time |
| 13 | import re |
| 14 | |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 15 | from features import hotlist_helpers |
| 16 | from framework import exceptions |
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_bizobj |
| 19 | from framework import framework_helpers |
| 20 | from framework import permissions |
| 21 | from framework import servlet |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 22 | from services import features_svc |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 23 | |
| 24 | |
| 25 | _MSG_HOTLIST_NAME_NOT_AVAIL = 'You already have a hotlist with that name.' |
| 26 | _MSG_MISSING_HOTLIST_NAME = 'Missing hotlist name' |
| 27 | _MSG_INVALID_HOTLIST_NAME = 'Invalid hotlist name' |
| 28 | _MSG_MISSING_HOTLIST_SUMMARY = 'Missing hotlist summary' |
| 29 | _MSG_INVALID_ISSUES_INPUT = 'Issues input is invalid' |
| 30 | _MSG_INVALID_MEMBERS_INPUT = 'One or more editor emails is not valid.' |
| 31 | |
| 32 | |
| 33 | class HotlistCreate(servlet.Servlet): |
| 34 | """HotlistCreate shows a simple page with a form to create a hotlist.""" |
| 35 | |
| 36 | _PAGE_TEMPLATE = 'features/hotlist-create-page.ezt' |
| 37 | |
| 38 | def AssertBasePermission(self, mr): |
| 39 | """Check whether the user has any permission to visit this page. |
| 40 | |
| 41 | Args: |
| 42 | mr: commonly used info parsed from the request. |
| 43 | """ |
| 44 | super(HotlistCreate, self).AssertBasePermission(mr) |
| 45 | if not permissions.CanCreateHotlist(mr.perms): |
| 46 | raise permissions.PermissionException( |
| 47 | 'User is not allowed to create a hotlist.') |
| 48 | |
| 49 | def GatherPageData(self, mr): |
| 50 | return { |
| 51 | 'user_tab_mode': 'st6', |
| 52 | 'initial_name': '', |
| 53 | 'initial_summary': '', |
| 54 | 'initial_description': '', |
| 55 | 'initial_editors': '', |
| 56 | 'initial_privacy': 'no', |
| 57 | } |
| 58 | |
| 59 | def ProcessFormData(self, mr, post_data): |
| 60 | """Process the hotlist create form. |
| 61 | |
| 62 | Args: |
| 63 | mr: commonly used info parsed from the request. |
| 64 | post_data: The post_data dict for the current request. |
| 65 | |
| 66 | Returns: |
| 67 | String URL to redirect the user to after processing. |
| 68 | """ |
| 69 | hotlist_name = post_data.get('hotlistname') |
| 70 | if not hotlist_name: |
| 71 | mr.errors.hotlistname = _MSG_MISSING_HOTLIST_NAME |
| 72 | elif not framework_bizobj.IsValidHotlistName(hotlist_name): |
| 73 | mr.errors.hotlistname = _MSG_INVALID_HOTLIST_NAME |
| 74 | |
| 75 | summary = post_data.get('summary') |
| 76 | if not summary: |
| 77 | mr.errors.summary = _MSG_MISSING_HOTLIST_SUMMARY |
| 78 | |
| 79 | description = post_data.get('description', '') |
| 80 | |
| 81 | editors = post_data.get('editors', '') |
| 82 | editor_ids = [] |
| 83 | if editors: |
| 84 | editor_emails = [ |
| 85 | email.strip() for email in editors.split(',')] |
| 86 | try: |
| 87 | editor_dict = self.services.user.LookupUserIDs(mr.cnxn, editor_emails) |
| 88 | editor_ids = list(editor_dict.values()) |
| 89 | except exceptions.NoSuchUserException: |
| 90 | mr.errors.editors = _MSG_INVALID_MEMBERS_INPUT |
| 91 | # In case the logged-in user specifies themselves as an editor, ignore it. |
| 92 | editor_ids = [eid for eid in editor_ids if eid != mr.auth.user_id] |
| 93 | |
| 94 | is_private = post_data.get('is_private') |
| 95 | |
| 96 | if not mr.errors.AnyErrors(): |
| 97 | try: |
| 98 | hotlist = self.services.features.CreateHotlist( |
| 99 | mr.cnxn, hotlist_name, summary, description, |
| 100 | owner_ids=[mr.auth.user_id], editor_ids=editor_ids, |
| 101 | is_private=(is_private == 'yes'), |
| 102 | ts=int(time.time())) |
| 103 | except features_svc.HotlistAlreadyExists: |
| 104 | mr.errors.hotlistname = _MSG_HOTLIST_NAME_NOT_AVAIL |
| 105 | |
| 106 | if mr.errors.AnyErrors(): |
| 107 | self.PleaseCorrect( |
| 108 | mr, initial_name=hotlist_name, initial_summary=summary, |
| 109 | initial_description=description, |
| 110 | initial_editors=editors, initial_privacy=is_private) |
| 111 | else: |
| 112 | return framework_helpers.FormatAbsoluteURL( |
| 113 | mr, hotlist_helpers.GetURLOfHotlist( |
| 114 | mr.cnxn, hotlist, self.services.user), |
| 115 | include_project=False) |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 116 | |
| 117 | # def GetCreateHotlist(self, **kwargs): |
| 118 | # return self.handler(**kwargs) |
| 119 | |
| 120 | # def PostCreateHotlist(self, **kwargs): |
| 121 | # return self.handler(**kwargs) |