[FIX] gamification: compute badge stats wihtout error

Since 960360a, when computing badge stats, a datetime was compared to a
string leading to a traceback.  e.g. when clicking in employess >
badges.

Also, due to a typo, the compute method was assigning the stats result
to the wrong attributes.  e.g. 'stats_my_this_month' instead of
'stat_my_this_month'.

With this commit, the above mentioned issues are fixed and a test is
covering these issues to avoid regression.
This commit is contained in:
Christophe Monniez
2018-08-22 09:28:16 +02:00
parent 0b2d6d7487
commit 3ff0948b0a
2 changed files with 38 additions and 16 deletions
+7 -7
View File
@@ -157,18 +157,18 @@ class GamificationBadge(models.Model):
@api.depends('owner_ids.badge_id', 'owner_ids.create_date', 'owner_ids.user_id')
def _get_badge_user_stats(self):
"""Return stats related to badge users"""
first_month_day = fields.Date.to_string(date.today().replace(day=1))
first_month_day = date.today().replace(day=1)
for badge in self:
owners = badge.owner_ids
badge.stats_my = sum(o.user_id == self.env.user for o in owners)
badge.stats_this_month = sum(o.create_date >= first_month_day for o in owners)
badge.stats_my_this_month = sum(
o.user_id == self.env.user and o.create_date >= first_month_day
badge.stat_my = sum(o.user_id == self.env.user for o in owners)
badge.stat_this_month = sum(o.create_date.date() >= first_month_day for o in owners)
badge.stat_my_this_month = sum(
o.user_id == self.env.user and o.create_date.date() >= first_month_day
for o in owners
)
badge.stats_my_monthly_sending = sum(
o.create_uid == self.env.user and o.create_date >= first_month_day
badge.stat_my_monthly_sending = sum(
o.create_uid == self.env.user and o.create_date.date() >= first_month_day
for o in owners
)
+31 -9
View File
@@ -1,27 +1,34 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.exceptions import UserError
from odoo.tests import common
class test_challenge(common.TransactionCase):
class TestGamificationCommon(common.TransactionCase):
def test_00_join_challenge(self):
def setUp(self):
super(TestGamificationCommon, self).setUp()
employees_group = self.env.ref('base.group_user')
user_ids = employees_group.users
challenge = self.env.ref('gamification.challenge_base_discover')
self.user_ids = employees_group.users
self.assertGreaterEqual(len(challenge.user_ids), len(user_ids), "Not enough users in base challenge")
self.env['res.users'].with_context(no_reset_password=True).create({
self.robot = self.env['res.users'].with_context(no_reset_password=True).create({
'name': 'R2D2',
'login': 'r2d2@openerp.com',
'email': 'r2d2@openerp.com',
'groups_id': [(6, 0, [employees_group.id])]
})
self.badge_good_job = self.env.ref('gamification.badge_good_job')
class test_challenge(TestGamificationCommon):
def test_00_join_challenge(self):
challenge = self.env.ref('gamification.challenge_base_discover')
self.assertGreaterEqual(len(challenge.user_ids), len(self.user_ids), "Not enough users in base challenge")
challenge._update_all()
self.assertGreaterEqual(len(challenge.user_ids), len(user_ids)+1, "These are not droids you are looking for")
self.assertGreaterEqual(len(challenge.user_ids), len(self.user_ids)+1, "These are not droids you are looking for")
def test_10_reach_challenge(self):
Goals = self.env['gamification.goal']
@@ -44,9 +51,24 @@ class test_challenge(common.TransactionCase):
self.assertFalse(missed, "Not every goal was reached after changing timezone")
# reward for two firsts as admin may have timezone
badge_id = self.env.ref('gamification.badge_good_job').id
badge_id = self.badge_good_job.id
challenge.write({'reward_first_id': badge_id, 'reward_second_id': badge_id})
challenge.state = 'done'
badge_ids = self.env['gamification.badge.user'].search([('badge_id', '=', badge_id), ('user_id', '=', demo.id)])
self.assertEqual(len(badge_ids), 1, "Demo user has not received the badge")
class test_badge_wizard(TestGamificationCommon):
def test_grant_badge(self):
wiz = self.env['gamification.badge.user.wizard'].create({
'user_id': self.env.user.id,
'badge_id': self.badge_good_job.id,
})
with self.assertRaises(UserError, msg="A user cannot grant a badge to himself"):
wiz.action_grant_badge()
wiz.user_id = self.robot.id
self.assertTrue(wiz.action_grant_badge(), "Could not grant badge")
self.assertEqual(self.badge_good_job.stat_this_month, 1)