Skip to content

Commit

Permalink
[FIX] forum: closing spam/offensive questions should give author karm…
Browse files Browse the repository at this point in the history
…a penalty

This is a partial patch for issue odoo#3460, pending more
improvements and refinements in master.

Currently the karma penalty is hardcoded to 5*downvote penalty,
which may or may not be sufficient to prevent posting, depending
on the other karma levels.
  • Loading branch information
odony committed Nov 5, 2014
1 parent d6cfd8e commit 6c55dab
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion addons/website_forum/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def question_close(self, forum, question, **post):

@http.route('/forum/<model("forum.forum"):forum>/question/<model("forum.post"):question>/reopen', type='http', auth="user", methods=['POST'], website=True)
def question_reopen(self, forum, question, **kwarg):
request.registry['forum.post'].write(request.cr, request.uid, [question.id], {'state': 'active'}, context=request.context)
request.registry['forum.post'].reopen(request.cr, request.uid, [question.id], context=request.context)
return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), slug(question)))

@http.route('/forum/<model("forum.forum"):forum>/question/<model("forum.post"):question>/delete', type='http', auth="user", methods=['POST'], website=True)
Expand Down
35 changes: 34 additions & 1 deletion addons/website_forum/models/forum.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import uuid
from werkzeug.exceptions import Forbidden

import logging
import openerp

from openerp import api, tools
from openerp import SUPERUSER_ID
from openerp.addons.website.models.website import slug
Expand All @@ -13,6 +15,7 @@
from openerp.tools import html2plaintext
from openerp.tools.translate import _

_logger = logging.getLogger(__name__)

class KarmaError(Forbidden):
""" Karma-related error, used for forum and posts. """
Expand Down Expand Up @@ -379,10 +382,40 @@ def write(self, cr, uid, ids, vals, context=None):
self.message_post(cr, uid, obj_id, body=body, subtype=subtype, context=context)
return res


def reopen(self, cr, uid, ids, context=None):
if any(post.parent_id or post.state != 'close'
for post in self.browse(cr, uid, ids, context=context)):
return False

reason_offensive = self.pool['ir.model.data'].xmlid_to_res_id(cr, uid, 'website_forum.reason_7')
reason_spam = self.pool['ir.model.data'].xmlid_to_res_id(cr, uid, 'website_forum.reason_8')
for post in self.browse(cr, uid, ids, context=context):
if post.closed_reason_id.id in (reason_offensive, reason_spam):
_logger.info('Upvoting user <%s>, reopening spam/offensive question',
post.create_uid.login)
# TODO: in master, consider making this a tunable karma parameter
self.pool['res.users'].add_karma(cr, SUPERUSER_ID, [post.create_uid.id],
post.forum_id.karma_gen_question_downvote * -5,
context=context)
self.pool['forum.post'].write(cr, SUPERUSER_ID, ids, {'state': 'active'}, context=context)

def close(self, cr, uid, ids, reason_id, context=None):
if any(post.parent_id for post in self.browse(cr, uid, ids, context=context)):
return False
return self.pool['forum.post'].write(cr, uid, ids, {

reason_offensive = self.pool['ir.model.data'].xmlid_to_res_id(cr, uid, 'website_forum.reason_7')
reason_spam = self.pool['ir.model.data'].xmlid_to_res_id(cr, uid, 'website_forum.reason_8')
if reason_id in (reason_offensive, reason_spam):
for post in self.browse(cr, uid, ids, context=context):
_logger.info('Downvoting user <%s> for posting spam/offensive contents',
post.create_uid.login)
# TODO: in master, consider making this a tunable karma parameter
self.pool['res.users'].add_karma(cr, SUPERUSER_ID, [post.create_uid.id],
post.forum_id.karma_gen_question_downvote * 5,
context=context)

self.pool['forum.post'].write(cr, uid, ids, {
'state': 'close',
'closed_uid': uid,
'closed_date': datetime.today().strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT),
Expand Down

0 comments on commit 6c55dab

Please sign in to comment.