Skip to content
Permalink
Browse files

[FIX] website_slides: fix vote karma

Currently doing a like then dislike does not give a void karma total. Indeed karma is
removed / added only when the vote hits 1 or -1 but does not correctly take
into account the third (void) state.

Tests are added.
  • Loading branch information...
tde-banana-odoo committed Mar 25, 2019
1 parent 76a13c5 commit b5e5b17d41794b5ec8a32b86a884addda264f874
@@ -407,22 +407,25 @@ def _action_vote(self, upvote=True):
slide_id = slide_partners.mapped('slide_id')
new_slides = self_sudo - slide_id
channel = slide_id.channel_id
karma_to_add = 0

for slide_partner in slide_partners:
if upvote:
new_vote = 0 if slide_partner.vote == -1 else 1
else:
new_vote = 0 if slide_partner.vote == 1 else -1
slide_partner.vote = new_vote
self.env.user.add_karma(-channel.karma_gen_slide_vote if new_vote == 0 else channel.karma_gen_slide_vote)
karma_to_add += channel.karma_gen_slide_vote * (1 if upvote else -1)

for new_slide in new_slides:
new_vote = 1 if upvote else -1
new_slide.write({
'slide_partner_ids': [(0, 0, {'vote': new_vote, 'partner_id': self.env.user.partner_id.id})]
})
channel = new_slides.channel_id
self.env.user.add_karma(-channel.karma_gen_slide_vote if new_vote == 0 else channel.karma_gen_slide_vote)
karma_to_add += new_slide.channel_id.karma_gen_slide_vote * (1 if upvote else -1)

if karma_to_add:
self.env.user.add_karma(karma_to_add)

def action_set_viewed(self, quiz_attempts_inc=False):
if not all(slide.channel_id.is_member for slide in self):
@@ -47,6 +47,9 @@ def setUp(self):
'enroll': 'public',
'visibility': 'public',
'website_published': True,
'karma_gen_channel_finish': 100,
'karma_gen_slide_vote': 5,
'karma_gen_channel_rank': 10,
})
self.slide = self.env['slide.slide'].sudo(self.user_publisher).create({
'name': 'How To Cook Humans',
@@ -3,6 +3,7 @@

from odoo.addons.website_slides.tests import common
from odoo.tests import tagged
from odoo.tests.common import users
from odoo.tools import mute_logger


@@ -20,74 +21,74 @@ def setUp(self):
'visibility': 'public',
'website_published': True,
'karma_gen_channel_finish': 100,
'karma_gen_slide_vote': 5,
'karma_gen_channel_rank': 10,
})

self.slide_2 = self.env['slide.slide'].sudo(self.user_publisher).create({
self.slide_2_0 = self.env['slide.slide'].sudo(self.user_publisher).create({
'name': 'How to travel through space and time',
'channel_id': self.channel_2.id,
'slide_type': 'presentation',
'website_published': True,
'completion_time': 2.0,
})

self.channel_3 = self.env['slide.channel'].sudo(self.user_publisher).create({
'name': 'Test Channel 3',
'channel_type': 'training',
'promote_strategy': 'most_voted',
'enroll': 'public',
'visibility': 'public',
'website_published': True,
'karma_gen_channel_finish': 50,
})

self.slide_3 = self.env['slide.slide'].sudo(self.user_publisher).create({
self.slide_2_1 = self.env['slide.slide'].sudo(self.user_publisher).create({
'name': 'How to duplicate yourself',
'channel_id': self.channel_3.id,
'channel_id': self.channel_2.id,
'slide_type': 'presentation',
'website_published': True,
'completion_time': 2.0,
})

def karma_gain_test(self, user):
# Add the user to the course
self.channel.sudo()._action_add_members(user.partner_id)
@mute_logger('odoo.models')
@users('user_emp', 'user_portal', 'user_publisher')
def test_karma_gain(self):
user = self.env.user
user.write({'karma': 0})
computed_karma = 0

# Init user env
channel = self.channel.sudo(user)
slide = self.slide.sudo(user)
# Add the user to the course
(self.channel | self.channel_2)._action_add_members(user.partner_id)
self.assertEqual(user.karma, 0)

# Finish the Course
karma = channel.karma_gen_channel_finish
slide.action_set_completed()
self.assertEqual(user.karma, karma)
self.slide.sudo(user).action_set_completed()
self.assertTrue(self.channel.sudo(user).completed)
computed_karma += self.channel.karma_gen_channel_finish
self.assertEqual(user.karma, computed_karma)

# Vote for a slide
karma = karma + channel.karma_gen_slide_vote
slide.action_like()
self.assertEqual(user.karma, karma)
slide.action_dislike()
self.assertEqual(user.karma, karma - channel.karma_gen_slide_vote)
slide.action_dislike()
self.assertEqual(user.karma, karma)
# Begin then finish the second Course
self.slide_2_0.sudo(user).action_set_completed()
self.assertFalse(self.channel_2.sudo(user).completed)
self.assertEqual(user.karma, computed_karma)

# Finish two course at the same time (should not ever happen but hey, we never know)
self.channel_2.sudo()._action_add_members(user.partner_id)
self.channel_3.sudo()._action_add_members(user.partner_id)
self.slide_2_1.sudo(user).action_set_completed()
self.assertTrue(self.channel_2.sudo(user).completed)
computed_karma += self.channel_2.karma_gen_channel_finish
self.assertEqual(user.karma, computed_karma)

karma = karma + self.channel_2.karma_gen_channel_finish + self.channel_3.karma_gen_channel_finish
slides = self.slide_2.sudo(user) | self.slide_3.sudo(user)
slides.action_set_completed()
self.assertEqual(user.karma, karma)
# Vote for a slide
slide_user = self.slide.sudo(user)
slide_user.action_like()
computed_karma += self.channel.karma_gen_slide_vote
self.assertEqual(user.karma, computed_karma)
slide_user.action_dislike()
computed_karma -= self.channel.karma_gen_slide_vote
self.assertEqual(user.karma, computed_karma)
slide_user.action_dislike()
computed_karma -= self.channel.karma_gen_slide_vote
self.assertEqual(user.karma, computed_karma)

@mute_logger('odoo.models')
def test_users_karma_gain(self):
self.karma_gain_test(self.user_emp)
@users('user_emp', 'user_portal', 'user_publisher')
def test_karma_gain_multiple_course(self):
user = self.env.user
user.write({'karma': 0})
computed_karma = 0

@mute_logger('odoo.models')
def test_user_publisher_karma_gain(self):
self.karma_gain_test(self.user_publisher)
# Finish two course at the same time (should not ever happen but hey, we never know)
(self.channel | self.channel_2)._action_add_members(user.partner_id)

@mute_logger('odoo.models')
def test_user_portal_karma_gain(self):
self.karma_gain_test(self.user_portal)
(self.slide | self.slide_2_0 | self.slide_2_1).sudo(user).action_set_completed()
computed_karma += self.channel.karma_gen_channel_finish + self.channel_2.karma_gen_channel_finish
self.assertEqual(user.karma, computed_karma)

0 comments on commit b5e5b17

Please sign in to comment.
You can’t perform that action at this time.