diff options
| author | Patrick Hoste <pko@odoo.com> | 2019-06-27 07:54:04 +0000 |
|---|---|---|
| committer | Thibault Delavallée <tde@odoo.com> | 2019-11-05 15:08:34 +0000 |
| commit | bfbc7c6a184b2b507b1bf4ad778430bc2f3a0e7f (patch) | |
| tree | 76cc91bd7b3b2011c7930e084ed77cc12dcac980 /addons/website_profile/controllers | |
| parent | c27ba2af1c640d91c6d7605412228687cc99cbd0 (diff) | |
[IMP] gamification: track karma change on users
PURPOSE
Allow karma gain tracking enabling notably display of top users based on
weekly / monthly gain in website profile.
SPECIFCIATIONS
Each time a user gains karma a record is created in the gamification karma
tracking model. Scheduled activity runs to consolidate the records into
monthly gain records to avoid having crowdy table and unnecessary noise
in karma gain.
This model is made private and only accessible through some dedicated
compute methods / controllers used in website profile.
In website profile module buttons are added to see users ranking based
on their total karma (like before) but also by last week and last month
gains (using the newly introduced tracking model).
LINKS
Task ID 2003505
PR #34594
Diffstat (limited to 'addons/website_profile/controllers')
| -rw-r--r-- | addons/website_profile/controllers/main.py | 46 |
1 files changed, 37 insertions, 9 deletions
diff --git a/addons/website_profile/controllers/main.py b/addons/website_profile/controllers/main.py index eef5d1d4dc8..703fc250f96 100644 --- a/addons/website_profile/controllers/main.py +++ b/addons/website_profile/controllers/main.py @@ -8,7 +8,10 @@ import werkzeug.urls import werkzeug.wrappers import math -from odoo import http, modules, tools +from dateutil.relativedelta import relativedelta +from operator import itemgetter + +from odoo import fields, http, modules, tools from odoo.http import request from odoo.osv import expression @@ -205,12 +208,13 @@ class WebsiteProfile(http.Controller): @http.route(['/profile/users', '/profile/users/page/<int:page>'], type='http', auth="public", website=True, sitemap=True) - def view_all_users_page(self, page=1, **searches): + def view_all_users_page(self, page=1, **kwargs): User = request.env['res.users'] dom = [('karma', '>', 1), ('website_published', '=', True)] # Searches - search_term = searches.get('search') + search_term = kwargs.get('search') + group_by = kwargs.get('group_by', False) if search_term: dom = expression.AND([['|', ('name', 'ilike', search_term), ('company_id.name', 'ilike', search_term)], dom]) @@ -225,12 +229,24 @@ class WebsiteProfile(http.Controller): user_values = self._prepare_all_users_values(users) # Get karma position for users (only website_published) - position_domain = [('karma', '>', 1), ('website_published', '=', True)] - position_map = self._get_users_karma_position(position_domain, users.ids) - for user in user_values: - user['position'] = position_map.get(user['id'], 0) + if group_by: + position_domain = [('karma', '>', 1), ('website_published', '=', True)] + position_map = self._get_user_tracking_karma_gain_position(position_domain, users.ids, group_by) + max_position = max([user_data['karma_position'] for user_data in position_map.values()], default=1) + for user in user_values: + user_data = position_map.get(user['id'], dict()) + user['position'] = user_data.get('karma_position', max_position + 1) + user['karma_gain'] = user_data.get('karma_gain_total', 0) + user_values.sort(key=itemgetter('position')) + else: + position_domain = [('karma', '>', 1), ('website_published', '=', True)] + position_map = self._get_users_karma_position(position_domain, users.ids) + for user in user_values: + user['position'] = position_map.get(user['id'], 0) + user['karma_gain'] = False values = { + 'group_by': group_by or 'all', 'top3_users': user_values[:3] if not search_term and page == 1 else None, 'users': user_values[3:] if not search_term and page == 1 else user_values, 'pager': pager @@ -244,8 +260,7 @@ class WebsiteProfile(http.Controller): if not user_ids: return {} - Users = request.env['res.users'] - where_query = Users._where_calc(domain) + where_query = request.env['res.users']._where_calc(domain) from_clause, where_clause, where_clause_params = where_query.get_sql() # we search on every user in the DB to get the real positioning (not the one inside the subset) @@ -264,6 +279,19 @@ class WebsiteProfile(http.Controller): return {item['id']: item['karma_position'] for item in request.env.cr.dictfetchall()} + def _get_user_tracking_karma_gain_position(self, domain, user_ids, group_by): + """ Helper method computing boundaries to give to _get_tracking_karma_gain_position. + See that method for more details. """ + to_date = fields.Date.today() + if group_by == 'week': + from_date = to_date - relativedelta(weeks=1) + elif group_by == 'month': + from_date = to_date - relativedelta(months=1) + else: + from_date = None + results = request.env['res.users'].browse(user_ids)._get_tracking_karma_gain_position(domain, from_date=from_date, to_date=to_date) + return dict((item['user_id'], dict(item)) for item in results) + # User and validation # -------------------------------------------------- |
