summaryrefslogtreecommitdiff
path: root/addons
diff options
context:
space:
mode:
authorDavid Beguin <dbe@odoo.com>2019-01-23 17:39:52 +0000
committerThibault Delavallée <tde@odoo.com>2019-02-07 12:11:44 +0000
commit3b9dcb6d8d8c50000828f3503e69c567d0daa98c (patch)
tree5f29517a258f01f9ff81e50ccab0428abf0f0c8d /addons
parentca25a692bd19fdca2b2600f2054eb419aae28999 (diff)
[REF] gamification, website_forum: move karma, badge and badge level to gamification
Purpose of this commit is to prepare addition of gamification in slides / eLearning platform. In order to be able to use karma and the badges in other modules we move those models in gamification. Partial commit linked to eLearning project. Main specifications related to gamification and user profile can be found on task 1922159 (PR #30514). Main specifications related to eLearning can be found on task 1902304 (PR #29876).
Diffstat (limited to 'addons')
-rw-r--r--addons/gamification/models/__init__.py2
-rw-r--r--addons/gamification/models/badge.py32
-rw-r--r--addons/gamification/models/gamification_karma_rank.py9
-rw-r--r--addons/gamification/models/res_users.py44
-rw-r--r--addons/gamification/views/badge.xml1
-rw-r--r--addons/lunch/models/lunch_product.py1
-rw-r--r--addons/website_forum/models/forum.py7
-rw-r--r--addons/website_forum/models/gamification.py17
-rw-r--r--addons/website_forum/models/res_users.py35
-rw-r--r--addons/website_forum/tests/test_forum.py2
-rw-r--r--addons/website_forum/views/forum.xml13
11 files changed, 88 insertions, 75 deletions
diff --git a/addons/gamification/models/__init__.py b/addons/gamification/models/__init__.py
index c43ef590c9b..926a5af2a07 100644
--- a/addons/gamification/models/__init__.py
+++ b/addons/gamification/models/__init__.py
@@ -4,3 +4,5 @@
from . import goal
from . import challenge
from . import badge
+from . import gamification_karma_rank
+from . import res_users
diff --git a/addons/gamification/models/badge.py b/addons/gamification/models/badge.py
index cf750aaa5f2..849f1744f46 100644
--- a/addons/gamification/models/badge.py
+++ b/addons/gamification/models/badge.py
@@ -4,10 +4,11 @@
import logging
from datetime import date
-from odoo import api, fields, models, _, exceptions
+from odoo import api, tools, fields, models, _, exceptions
_logger = logging.getLogger(__name__)
+
class BadgeUser(models.Model):
"""User having received a badge"""
@@ -22,6 +23,9 @@ class BadgeUser(models.Model):
challenge_id = fields.Many2one('gamification.challenge', string='Challenge originating', help="If this badge was rewarded through a challenge")
comment = fields.Text('Comment')
badge_name = fields.Char(related='badge_id.name', string="Badge Name", readonly=False)
+ level = fields.Selection(
+ [('bronze', 'Bronze'), ('silver', 'Silver'), ('gold', 'Gold')],
+ string='Badge Level', related="badge_id.level", store=True, readonly=True)
def _send_badge(self):
"""Send a notification to a user for receiving a badge
@@ -66,7 +70,20 @@ class GamificationBadge(models.Model):
name = fields.Char('Badge', required=True, translate=True)
active = fields.Boolean('Active', default=True)
description = fields.Text('Description', translate=True)
- image = fields.Binary("Image", help="This field holds the image used for the badge, limited to 256x256")
+ image = fields.Binary("Image", help="This field holds the image used for the badge.")
+ image_medium = fields.Binary(
+ "Medium-sized badge image",
+ help="Medium-sized image of the badge. It is automatically "
+ "resized as a 128x128px image, with aspect ratio preserved. "
+ "Use this field in form views or some kanban views.")
+ image_small = fields.Binary(
+ "Small-sized badge image",
+ help="Small-sized image of the badge. It is automatically "
+ "resized as a 64x64px image, with aspect ratio preserved. "
+ "Use this field anywhere a small image is required.")
+ level = fields.Selection([
+ ('bronze', 'Bronze'), ('silver', 'Silver'), ('gold', 'Gold')],
+ string='Forum Badge Level', default='bronze')
rule_auth = fields.Selection([
('everyone', 'Everyone'),
@@ -120,6 +137,17 @@ class GamificationBadge(models.Model):
"Remaining Sending Allowed", compute='_remaining_sending_calc',
help="If a maximum is set")
+ @api.model_create_multi
+ def create(self, values_list):
+ for vals in values_list:
+ tools.image_resize_images(vals)
+ return super(GamificationBadge, self).create(values_list)
+
+ @api.multi
+ def write(self, vals):
+ tools.image_resize_images(vals)
+ return super(GamificationBadge, self).write(vals)
+
@api.depends('owner_ids')
def _get_owners_info(self):
"""Return:
diff --git a/addons/gamification/models/gamification_karma_rank.py b/addons/gamification/models/gamification_karma_rank.py
new file mode 100644
index 00000000000..061082545b4
--- /dev/null
+++ b/addons/gamification/models/gamification_karma_rank.py
@@ -0,0 +1,9 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from werkzeug.exceptions import Forbidden
+
+
+class KarmaError(Forbidden):
+ """ Karma-related error, used for forum and posts. """
+ pass
diff --git a/addons/gamification/models/res_users.py b/addons/gamification/models/res_users.py
new file mode 100644
index 00000000000..fccddf8bfb5
--- /dev/null
+++ b/addons/gamification/models/res_users.py
@@ -0,0 +1,44 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import api, fields, models
+
+
+class Users(models.Model):
+ _inherit = 'res.users'
+
+ karma = fields.Integer('Karma', default=0)
+ badge_ids = fields.One2many('gamification.badge.user', 'user_id', string='Badges', copy=False)
+ gold_badge = fields.Integer('Gold badges count', compute="_get_user_badge_level")
+ silver_badge = fields.Integer('Silver badges count', compute="_get_user_badge_level")
+ bronze_badge = fields.Integer('Bronze badges count', compute="_get_user_badge_level")
+
+ @api.multi
+ def add_karma(self, karma):
+ for user in self:
+ user.karma += karma
+ return True
+
+ @api.multi
+ @api.depends('badge_ids')
+ def _get_user_badge_level(self):
+ """ Return total badge per level of users
+ TDE CLEANME: shouldn't check type is forum ? """
+ for user in self:
+ user.gold_badge = 0
+ user.silver_badge = 0
+ user.bronze_badge = 0
+
+ self.env.cr.execute("""
+ SELECT bu.user_id, b.level, count(1)
+ FROM gamification_badge_user bu, gamification_badge b
+ WHERE bu.user_id IN %s
+ AND bu.badge_id = b.id
+ AND b.level IS NOT NULL
+ GROUP BY bu.user_id, b.level
+ ORDER BY bu.user_id;
+ """, [tuple(self.ids)])
+
+ for (user_id, level, count) in self.env.cr.fetchall():
+ # levels are gold, silver, bronze but fields have _badge postfix
+ self.browse(user_id)['{}_badge'.format(level)] = count
diff --git a/addons/gamification/views/badge.xml b/addons/gamification/views/badge.xml
index 69c08fb5f68..8caf7408281 100644
--- a/addons/gamification/views/badge.xml
+++ b/addons/gamification/views/badge.xml
@@ -73,6 +73,7 @@
</group>
<group string="Rewards for challenges">
<field name="challenge_ids" mode="kanban" widget="many2many" nolabel="1" context="{'default_reward_id': active_id}" />
+ <field name="level"/>
</group>
<group string="Statistics">
<group>
diff --git a/addons/lunch/models/lunch_product.py b/addons/lunch/models/lunch_product.py
index 0bc595c135c..6d4132d9bde 100644
--- a/addons/lunch/models/lunch_product.py
+++ b/addons/lunch/models/lunch_product.py
@@ -109,7 +109,6 @@ class LunchProduct(models.Model):
new_until = fields.Date('New Until')
favorite_user_ids = fields.Many2many('res.users', 'lunch_product_favorite_user_rel', 'product_id', 'user_id')
-
@api.model_create_multi
def create(self, vals_list):
for values in vals_list:
diff --git a/addons/website_forum/models/forum.py b/addons/website_forum/models/forum.py
index ee9098dd614..f515f022961 100644
--- a/addons/website_forum/models/forum.py
+++ b/addons/website_forum/models/forum.py
@@ -7,7 +7,7 @@ import re
import uuid
from datetime import datetime
-from werkzeug.exceptions import Forbidden
+from odoo.addons.gamification.models.gamification_karma_rank import KarmaError
from odoo import api, fields, models, tools, SUPERUSER_ID, _
from odoo.exceptions import UserError, ValidationError
@@ -16,11 +16,6 @@ from odoo.tools import misc
_logger = logging.getLogger(__name__)
-class KarmaError(Forbidden):
- """ Karma-related error, used for forum and posts. """
- pass
-
-
class Forum(models.Model):
_name = 'forum.forum'
_description = 'Forum'
diff --git a/addons/website_forum/models/gamification.py b/addons/website_forum/models/gamification.py
index 5c83dcc1ad6..49544fae28f 100644
--- a/addons/website_forum/models/gamification.py
+++ b/addons/website_forum/models/gamification.py
@@ -8,20 +8,3 @@ class Challenge(models.Model):
_inherit = 'gamification.challenge'
category = fields.Selection(selection_add=[('forum', 'Website / Forum')])
-
-
-class Badge(models.Model):
- _inherit = 'gamification.badge'
-
- level = fields.Selection([('bronze', 'bronze'), ('silver', 'silver'), ('gold', 'gold')], string='Forum Badge Level')
-
-
-class UserBadge(models.Model):
- _inherit = 'gamification.badge.user'
-
- level = fields.Selection(
- [('bronze', 'bronze'),
- ('silver', 'silver'),
- ('gold', 'gold')],
- string='Forum Badge Level',
- related="badge_id.level", store=True, readonly=False)
diff --git a/addons/website_forum/models/res_users.py b/addons/website_forum/models/res_users.py
index 1149fbd9708..7482d4a4a9d 100644
--- a/addons/website_forum/models/res_users.py
+++ b/addons/website_forum/models/res_users.py
@@ -22,38 +22,9 @@ class Users(models.Model):
return init_res
create_date = fields.Datetime('Create Date', readonly=True, index=True)
- karma = fields.Integer('Karma', default=0)
- badge_ids = fields.One2many('gamification.badge.user', 'user_id', string='Badges', copy=False)
- gold_badge = fields.Integer('Gold badges count', compute="_get_user_badge_level")
- silver_badge = fields.Integer('Silver badges count', compute="_get_user_badge_level")
- bronze_badge = fields.Integer('Bronze badges count', compute="_get_user_badge_level")
forum_waiting_posts_count = fields.Integer('Waiting post', compute="_get_user_waiting_post")
@api.multi
- @api.depends('badge_ids')
- def _get_user_badge_level(self):
- """ Return total badge per level of users
- TDE CLEANME: shouldn't check type is forum ? """
- for user in self:
- user.gold_badge = 0
- user.silver_badge = 0
- user.bronze_badge = 0
-
- self.env.cr.execute("""
- SELECT bu.user_id, b.level, count(1)
- FROM gamification_badge_user bu, gamification_badge b
- WHERE bu.user_id IN %s
- AND bu.badge_id = b.id
- AND b.level IS NOT NULL
- GROUP BY bu.user_id, b.level
- ORDER BY bu.user_id;
- """, [tuple(self.ids)])
-
- for (user_id, level, count) in self.env.cr.fetchall():
- # levels are gold, silver, bronze but fields have _badge postfix
- self.browse(user_id)['{}_badge'.format(level)] = count
-
- @api.multi
def _get_user_waiting_post(self):
for user in self:
Post = self.env['forum.post']
@@ -111,12 +82,6 @@ class Users(models.Model):
return self.write({'karma': karma})
return False
- @api.multi
- def add_karma(self, karma):
- for user in self:
- user.karma += karma
- return True
-
# Wrapper for call_kw with inherits
@api.multi
def open_website_url(self):
diff --git a/addons/website_forum/tests/test_forum.py b/addons/website_forum/tests/test_forum.py
index 4037dc3b783..91d72c5dff5 100644
--- a/addons/website_forum/tests/test_forum.py
+++ b/addons/website_forum/tests/test_forum.py
@@ -2,7 +2,7 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from .common import KARMA, TestForumCommon
-from ..models.forum import KarmaError
+from odoo.addons.gamification.models.gamification_karma_rank import KarmaError
from odoo.exceptions import UserError, AccessError
from odoo.tools import mute_logger
from psycopg2 import IntegrityError
diff --git a/addons/website_forum/views/forum.xml b/addons/website_forum/views/forum.xml
index fe9188af31e..6a168613cdc 100644
--- a/addons/website_forum/views/forum.xml
+++ b/addons/website_forum/views/forum.xml
@@ -8,19 +8,6 @@
<menuitem name="Forum" id="menu_website_forum_global"
parent="website.menu_website_global_configuration" sequence="170" groups="website.group_website_designer"/>
- <record id="badge_form_view_forum" model="ir.ui.view">
- <field name="name">forum.badge_form_view</field>
- <field name="model">gamification.badge</field>
- <field name="inherit_id" ref="gamification.badge_form_view"/>
- <field name="arch" type="xml">
- <field name="challenge_ids" position="after">
- <group>
- <field name="level" />
- </group>
- </field>
- </field>
- </record>
-
<!-- FORUM VIEWS -->
<record id="view_forum_forum_list" model="ir.ui.view">
<field name="name">forum.forum.list</field>