blob: 4b28dd683270591ff1fc722543e7b7a46a19e6e0 [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# 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.
Copybara854996b2021-09-07 19:36:02 +00004
5"""Servlet for creating new hotlists."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
10import logging
11import time
12import re
13
Copybara854996b2021-09-07 19:36:02 +000014from features import hotlist_helpers
15from framework import exceptions
16from framework import framework_bizobj
17from framework import framework_helpers
18from framework import permissions
19from framework import servlet
Copybara854996b2021-09-07 19:36:02 +000020from services import features_svc
Copybara854996b2021-09-07 19:36:02 +000021
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ínezf19ea432024-01-23 20:20:52 +010031class HotlistCreate(servlet.Servlet):
Copybara854996b2021-09-07 19:36:02 +000032 """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ínezde942802022-07-15 14:06:55 +0200114
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +0200115 def GetCreateHotlist(self, **kwargs):
116 return self.handler(**kwargs)
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +0200117
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +0200118 def PostCreateHotlist(self, **kwargs):
119 return self.handler(**kwargs)