blob: 6913c19d44be0bfd23ceb8d5acd59e0e2638f550 [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
Copybara854996b2021-09-07 19:36:02 +000015from features import hotlist_helpers
16from framework import exceptions
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020017from framework import flaskservlet
Copybara854996b2021-09-07 19:36:02 +000018from framework import framework_bizobj
19from framework import framework_helpers
20from framework import permissions
21from framework import servlet
Copybara854996b2021-09-07 19:36:02 +000022from services import features_svc
Copybara854996b2021-09-07 19:36:02 +000023
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
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020033class HotlistCreate(flaskservlet.FlaskServlet):
Copybara854996b2021-09-07 19:36:02 +000034 """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ínezde942802022-07-15 14:06:55 +0200116
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +0200117 def GetCreateHotlist(self, **kwargs):
118 return self.handler(**kwargs)
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +0200119
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +0200120 def PostCreateHotlist(self, **kwargs):
121 return self.handler(**kwargs)