From: Martin Trigaux Date: Tue, 7 Oct 2014 16:14:32 +0000 (+0200) Subject: [IMP] gamification: avoid sending twice emails in cron X-Git-Tag: InsPy_8.0_01~4^2~61 X-Git-Url: http://git.inspyration.org/?a=commitdiff_plain;h=ea68690407203d1c4b6119980fff0530dc232b99;p=odoo%2Fodoo.git [IMP] gamification: avoid sending twice emails in cron When the cron is running on a database with a large number of goals (e.g. website_forum with thousands of users), it's possible the CPU time is exceeded and we may have a rollback after sending some emails (for granted badges). To avoid sending twice emails, commit in cron mode after each reward. --- diff --git a/addons/gamification/models/challenge.py b/addons/gamification/models/challenge.py index 4e128fc..c43b840 100644 --- a/addons/gamification/models/challenge.py +++ b/addons/gamification/models/challenge.py @@ -292,6 +292,9 @@ class gamification_challenge(osv.Model): :param list(int) ids: the ids of the challenges to update, if False will update only challenges in progress.""" + if not ids: + return True + if isinstance(ids, (int,long)): ids = [ids] @@ -705,6 +708,7 @@ class gamification_challenge(osv.Model): """ if isinstance(ids, (int,long)): ids = [ids] + commit = context.get('commit_gamification', False) for challenge in self.browse(cr, uid, ids, context=context): (start_date, end_date) = start_end_date_for_period(challenge.period, challenge.start_date, challenge.end_date) yesterday = date.today() - timedelta(days=1) @@ -733,6 +737,8 @@ class gamification_challenge(osv.Model): continue self.reward_user(cr, uid, user_id, challenge.reward_id.id, challenge.id, context=context) rewarded_users.append(user_id) + if commit: + cr.commit() if challenge_ended: # open chatter message @@ -765,6 +771,8 @@ class gamification_challenge(osv.Model): partner_ids=[user.partner_id.id for user in challenge.user_ids], body=message_body, context=context) + if commit: + cr.commit() return True