blob: ab129ce47cff1c1c45c39f37623c0c7e39cac61d [file] [log] [blame]
# Copyright 2017 The Chromium Authors. All rights reserved.
# Use of this source code is govered by a BSD-style
# license that can be found in the LICENSE file or at
"""Classes that implement adding issues to hotlists from /issues/ pages."""
import logging
import time
from features import hotlist_helpers
from framework import jsonfeed
from framework import permissions
class UpdateHotlists(jsonfeed.JsonFeed):
"""AddToHotlist is a servlet which adds issues to hotlists."""
def AssertBasePermission(self, mr):
super(UpdateHotlists, self).AssertBasePermission(mr)
hotlist_ids = (mr.hotlist_ids_add or []) + (mr.hotlist_ids_remove or [])
if hotlist_ids:
hotlist_ids_dict =
mr.cnxn, hotlist_ids)
for _id, hotlist in hotlist_ids_dict.iteritems():
if not permissions.CanEditHotlist(mr.auth.effective_ids, hotlist):
raise permissions.PermissionException(
'You are not allowed to edit hotlist %s' %
if not permissions.CanCreateHotlist(mr.perms):
raise permissions.PermissionException(
'User is not allowed to create a hotlist.')
def createNewHotlist(self, mr):
hotlist_name = 'Hotlist-1'
count = 1
taken_names = [ for h in
mr.cnxn, mr.auth.user_id)]
while hotlist_name in taken_names:
count += 1
hotlist_name = 'Hotlist-%d' % count
mr.cnxn, hotlist_name, 'Hotlist of bulk added issues', '',
[mr.auth.user_id], [])
def HandleRequest(self, mr):
project_names = []
refs = []
for issue_ref in mr.issue_refs:
issue_split = issue_ref.split(':')
refs.append((issue_split[0], int(issue_split[1])))
ref_projects =
mr.cnxn, project_names)
default_project_name = ''
selected_iids, misses =
mr.cnxn, ref_projects, default_project_name, refs)
# Start updating hotlists.
hotlist_ids_add = mr.hotlist_ids_add or []
hotlist_ids_remove = mr.hotlist_ids_remove or []
if (not hotlist_ids_add and not hotlist_ids_remove):
hotlist_ids_add = [self.createNewHotlist(mr).hotlist_id]
# Add issues to hotlists.
added_tuples = [(issue_id, mr.auth.user_id,
int(time.time()), '') for issue_id in
mr.cnxn, hotlist_ids_add, added_tuples,,
# Remove issues from hotlists.
for hotlist_id in hotlist_ids_remove:, hotlist_id,
# Organize response data.
missed = []
for miss in misses:
project_name =
mr.cnxn, miss[0]).project_name
missed.append(('%s:%d' % (project_name, miss[1])))
updated_hotlist_pbs = [
mr.cnxn, hotlist_id) for hotlist_id in
hotlist_ids_add + hotlist_ids_remove]
user_issue_hotlists = []
user_remaining_hotlists = []
if selected_iids:
# For updating user's hotlist when adding issues via the issue
# details page. selected_iids should only contain one issue in this case.
user_hotlists =
mr.cnxn, mr.auth.user_id)
user_issue_hotlists = list(set(user_hotlists) & set(
mr.cnxn, selected_iids[0])))
user_remaining_hotlists = list(
return {'missed': missed, # missed issues
# names of hotlists that issues were added to or a new hotlist
'updatedHotlistNames': [ for h in updated_hotlist_pbs],
'issueHotlistIds': [h.hotlist_id for h in user_issue_hotlists],
'issueHotlistNames': [ for h in user_issue_hotlists],
'issueHotlistUrls': [hotlist_helpers.GetURLOfHotlist(
mr.cnxn, hotlist, for
hotlist in user_issue_hotlists],
'remainingHotlistNames': [ for h in user_remaining_hotlists],
'remainingHotlistIds': [h.hotlist_id for h in