One morning I shot an elephant in my pajamas. How he got in my pajamas, I don't know.
bzr revid: mat@openerp.com-20140415131929-6pktlodmlgr2fuwz
This commit is contained in:
parent
15306066d0
commit
bc0738e47e
|
@ -348,7 +348,7 @@ class WebsiteForum(http.Controller):
|
|||
return {'error': 'own_post'}
|
||||
user = request.registry['res.users'].browse(request.cr, SUPERUSER_ID, request.uid, context=request.context)
|
||||
if user.karma <= 5:
|
||||
return {'error': 'not_enough_karma', 'karma': 5}
|
||||
return {'error': 'not_enough_karma', 'karma': 1}
|
||||
return request.registry['forum.post'].vote(request.cr, request.uid, [post.id], upvote=True, context=request.context)
|
||||
|
||||
@http.route('/forum/<model("forum.forum"):forum>/post/<model("forum.post"):post>/downvote', type='json', auth="public", multilang=True, website=True)
|
||||
|
|
|
@ -176,15 +176,14 @@ class Post(osv.Model):
|
|||
context = {}
|
||||
create_context = dict(context, mail_create_nolog=True)
|
||||
post_id = super(Post, self).create(cr, uid, vals, context=create_context)
|
||||
post = self.browse(cr, uid, post_id, context=context)
|
||||
# post message + subtype depending on parent_id
|
||||
if vals.get("parent_id"):
|
||||
parent = self.browse(cr, SUPERUSER_ID, vals['parent_id'], context=context)
|
||||
body = _('<p><a href="forum/%s/question/%s">New Answer Posted</a></p>' % (slug(parent.forum_id), slug(parent)))
|
||||
self.message_post(cr, uid, parent.id, subject=_('Re: %s') % parent.name, body=body, subtype='website_forum.mt_answer_new', context=context)
|
||||
else:
|
||||
self.message_post(cr, uid, post.id, subject=post.name, body=_('New Question Created'), subtype='website_forum.mt_question_new', context=context)
|
||||
self.pool['res.users'].write(cr, SUPERUSER_ID, [post.create_uid.id], {'karma': 2}, context=context)
|
||||
self.message_post(cr, uid, post_id, subject=vals.get('name', ''), body=_('New Question Created'), subtype='website_forum.mt_question_new', context=context)
|
||||
self.pool['res.users'].add_karma(cr, SUPERUSER_ID, [uid], 2, context=context)
|
||||
return post_id
|
||||
|
||||
def write(self, cr, uid, ids, vals, context=None):
|
||||
|
@ -203,7 +202,7 @@ class Post(osv.Model):
|
|||
if 'correct' in vals:
|
||||
for post in self.browse(cr, uid, ids, context=context):
|
||||
karma_value = 15 if vals.get('correct') else -15
|
||||
self.pool['res.users'].write(cr, SUPERUSER_ID, [post.create_uid.id], {'karma': karma_value}, context=context)
|
||||
self.pool['res.users'].add_karma(cr, SUPERUSER_ID, [post.create_uid.id], {'karma': karma_value}, context=context)
|
||||
return res
|
||||
|
||||
def vote(self, cr, uid, ids, upvote=True, context=None):
|
||||
|
@ -241,7 +240,7 @@ class Vote(osv.Model):
|
|||
_description = 'Vote'
|
||||
_columns = {
|
||||
'post_id': fields.many2one('forum.post', 'Post', ondelete='cascade', required=True),
|
||||
'user_id': fields.many2one('res.users', 'User'),
|
||||
'user_id': fields.many2one('res.users', 'User', required=True),
|
||||
'vote': fields.selection([('1', '1'), ('-1', '-1'), ('0', '0')], 'Vote', required=True),
|
||||
'create_date': fields.datetime('Create Date', select=True, readonly=True),
|
||||
}
|
||||
|
@ -250,23 +249,19 @@ class Vote(osv.Model):
|
|||
'vote': lambda *args: '1',
|
||||
}
|
||||
|
||||
def update_karma(self, cr, uid, ids, new_vote='0', old_vote='0', context=None):
|
||||
karma_value = (int(new_vote) - int(old_vote)) * 10
|
||||
if karma_value:
|
||||
for vote in self.browse(cr, uid, ids, context=context):
|
||||
self.pool['res.users'].add_karma(cr, SUPERUSER_ID, [vote.post_id.create_uid.id], karma_value, context=context)
|
||||
return True
|
||||
|
||||
def create(self, cr, uid, vals, context=None):
|
||||
vote_id = super(Vote, self).create(cr, uid, vals, context=context)
|
||||
self.update_karma(cr, uid, [vote_id], new_vote=vals.get('vote', '1'), context=context)
|
||||
karma_value = int(vals.get('vote', '1')) * 10
|
||||
post = self.pool['forum.post'].browse(cr, uid, vals.get('post_id'), context=context)
|
||||
self.pool['res.users'].add_karma(cr, SUPERUSER_ID, post.create_uid.id, karma_value, context=context)
|
||||
return vote_id
|
||||
|
||||
def write(self, cr, uid, ids, values, context=None):
|
||||
res = super(Vote, self).write(cr, uid, ids, values, context=context)
|
||||
if 'vote' in values:
|
||||
for vote in self.browse(cr, uid, ids, context=context):
|
||||
self.update_karma(cr, uid, ids, new_vote=values['vote'], old_vote=vote.vote, context=context)
|
||||
karma_value = (int(values.get('vote')) - int(vote.vote)) * 10
|
||||
self.pool['res.users'].add_karma(cr, SUPERUSER_ID, vote.post_id.create_uid.id, karma_value, context=context)
|
||||
res = super(Vote, self).write(cr, uid, ids, values, context=context)
|
||||
return res
|
||||
|
||||
|
||||
|
|
|
@ -32,6 +32,8 @@ class Users(osv.Model):
|
|||
}
|
||||
|
||||
def add_karma(self, cr, uid, ids, karma, context=None):
|
||||
if isinstance(ids, (int, long)):
|
||||
ids = [ids]
|
||||
for user in self.browse(cr, uid, ids, context=context):
|
||||
self.write(cr, uid, [user.id], {'karma': user.karma + karma}, context=context)
|
||||
return True
|
||||
|
|
Loading…
Reference in New Issue