blob: a9853938c967ae823a567662d4f1480631685056 [file] [log] [blame]
# Copyright 2016 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 spam flagging features.
import httplib
import logging
from framework import actionlimit
from framework import framework_helpers
from framework import paginate
from framework import permissions
from framework import urls
from framework import servlet
from framework import template_helpers
from framework import xsrf
from tracker import spam_helpers
from tracker import tracker_bizobj
class FlagSpamForm(servlet.Servlet):
"""Flag or un-flag the specified issue/comment for the logged in user."""
def ProcessFormData(self, mr, post_data):
"""Process the flagging request.
mr: commonly used info parsed from the request.
A redirect URL to either the original issue page or to issue list.
comment_id = post_data.get('comment_id', 0) or None
flagged_spam = post_data['spam'] == 'true'
flag_count = 1
if mr.local_id_list is not None:
flag_count = len(mr.local_id_list)
self.CountRateLimitedActions(mr, {actionlimit.FLAG_SPAM: flag_count})
# Has the side effect of checking soft limits and returning an error page
# when the user hits the limit.
# Check perms here for both the single Issue and Comment case.
if mr.local_id is not None:
issue =
mr.cnxn, mr.project_id, mr.local_id, use_cache=False)
perms = self.MakePagePerms(
mr, issue, permissions.FLAG_SPAM, permissions.VERDICT_SPAM)
if not perms.FlagSpam:
logging.error('User %d not allowed to flag %d/%r as spam.' % (
mr.auth.user_id, mr.local_id, comment_id))
raise permissions.PermissionException(
'User lacks permission to flag spam')
# Prevent users who can't view issue from being able to mark it as spam.
config =, mr.project_id)
granted_perms = tracker_bizobj.GetGrantedPerms(
issue, mr.auth.effective_ids, config)
permit_view = permissions.CanViewIssue(
mr.auth.effective_ids, mr.perms, mr.project, issue,
if not permit_view:
logging.warning('Issue is %r', issue)
raise permissions.PermissionException(
'User is not allowed to view this issue, so cannot flag it as spam.')
# TODO: Check for exceeding the max number of flags, issue verdict then too.
issue_list = []
# Flag a single comment.
if comment_id is not None:
comment =, comment_id)
if perms.VerdictSpam:,,, comment_id,
mr.auth.user_id, flagged_spam), issue.issue_id,,
comment.user_id, mr.auth.user_id, flagged_spam)
elif mr.local_id is not None:
issue_list = [issue]
elif mr.local_id_list is not None:
issue_list =
mr.cnxn, mr.project_id, mr.local_id_list, use_cache=False)
self.response.status = httplib.BAD_REQUEST
flag_issues = []
verdict_issues = []
for issue in issue_list:
perms = self.MakePagePerms(mr, issue, permissions.FLAG_SPAM,
if perms.VerdictSpam:
if perms.FlagSpam:
if len(verdict_issues) > 0:,,
verdict_issues, mr.auth.user_id, flagged_spam)
if len(flag_issues) > 0:,, flag_issues,
mr.auth.user_id, flagged_spam)
# TODO(seanmccullough): Make this an ajax request instead of a redirect.
if mr.local_id_list is not None:
return framework_helpers.FormatAbsoluteURL(mr, urls.ISSUE_LIST)
return framework_helpers.FormatAbsoluteURL(
mr, urls.ISSUE_DETAIL, id=mr.local_id)
class ModerationQueue(servlet.Servlet):
_PAGE_TEMPLATE = 'tracker/spam-moderation-queue.ezt'
def ProcessFormData(self, mr, post_data):
if not self.CheckPerm(mr, permissions.MODERATE_SPAM):
raise permissions.PermissionException()
issue_local_ids = [int(iid) for iid in post_data.getall("issue_local_id")]
mark_spam = "mark_spam" in post_data
issues =,
mr.project.project_id, issue_local_ids, use_cache=False),, issues, mr.auth.user_id, mark_spam)
return framework_helpers.FormatAbsoluteURL(mr, urls.SPAM_MODERATION_QUEUE)
def GatherPageData(self, mr):
if not self.CheckPerm(mr, permissions.MODERATE_SPAM):
raise permissions.PermissionException()
page_perms = self.MakePagePerms(
mr, None, permissions.MODERATE_SPAM,
permissions.EDIT_ISSUE, permissions.CREATE_ISSUE,
# TODO(seanmccullough): Figure out how to get the IssueFlagQueue either
# integrated into this page data, or on its own subtab of spam moderation.
# Also figure out the same for Comments.
issue_items, total_count =
mr.cnxn,, mr.project.project_id, mr.start, mr.num)
issue_queue = spam_helpers.DecorateIssueClassifierQueue(mr.cnxn,,,,
p = paginate.ArtifactPagination(mr, [], mr.num, urls.SPAM_MODERATION_QUEUE,
return {
'issue_queue': issue_queue,
'projectname': mr.project.project_name,
'pagination': p,
'page_perms': page_perms,
'moderate_spam_token': xsrf.GenerateToken(
mr.auth.user_id, '/p/' % (
mr.project_name, urls.SPAM_MODERATION_QUEUE)),