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