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