summaryrefslogtreecommitdiff
path: root/addons/website_profile
diff options
context:
space:
mode:
Diffstat (limited to 'addons/website_profile')
-rw-r--r--addons/website_profile/controllers/main.py46
-rw-r--r--addons/website_profile/views/website_profile.xml24
2 files changed, 60 insertions, 10 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
# --------------------------------------------------
diff --git a/addons/website_profile/views/website_profile.xml b/addons/website_profile/views/website_profile.xml
index 629119b98d0..9a8dacb8e2a 100644
--- a/addons/website_profile/views/website_profile.xml
+++ b/addons/website_profile/views/website_profile.xml
@@ -481,7 +481,15 @@
<div class="o_wprofile_all_users_header o_wprofile_gradient mb-n5 pb-5">
<t t-call="website_profile.user_profile_sub_nav"/>
<div class="container">
- <h1 class="py-4 text-white">All Users</h1>
+ <h1 class="py-4 text-white d-inline-block">All Users</h1>
+ <div class="form-inline py-4 float-right">
+ <strong class="form-group text-white mr-2">Rank by :</strong>
+ <div class="form-group btn-group">
+ <button type="button" t-attf-class="btn btn-secondary #{ 'active' if group_by == 'week' else ''}" onclick="location.href='/profile/users?group_by=week'">This week</button>
+ <button type="button" t-attf-class="btn btn-secondary #{ 'active' if group_by == 'month' else ''}" onclick="location.href='/profile/users?group_by=month'">This month</button>
+ <button type="button" t-attf-class="btn btn-secondary #{ 'active' if group_by == 'all' else ''}" onclick="location.href='/profile/users'">All time</button>
+ </div>
+ </div>
</div>
</div>
</template>
@@ -519,6 +527,9 @@
<h3 class="mt-2 mb-0" t-esc="user['name']"></h3>
<span class="badge badge-danger font-weight-normal px-2" t-if="not user['website_published']">Unpublished</span>
<strong class="text-muted" t-esc="user['rank']"/>
+ <div class="h3 my-2" t-if="user['karma_gain']">
+ <span class="badge badge-pill badge-success px-3 py-2" >+ <t t-esc="user['karma_gain']"/> XP</span>
+ </div>
</div>
<div class="row mx-0 o_wprofile_top3_card_footer text-nowrap">
<div class="col py-3"><b t-esc="user['karma']"/> <span class="text-muted">XP</span></div>
@@ -538,6 +549,17 @@
<span class="font-weight-bold" t-esc="user['name']"/><br/>
<span class="text-muted font-weight-bold" t-esc="user['rank']"></span>
</td>
+ <td class="align-middle text-nowrap">
+ <t t-if="user['karma_gain']">
+ <span class="badge badge-pill badge-success d-inline">+ <t t-esc="user['karma_gain']"/> XP</span>
+ <span class="text-muted pl-2 pr-3">
+ <t t-if="group_by == 'week'">Weekly </t>
+ <t t-elif="group_by == 'month'">Monthly </t>
+ <t t-else="">All time </t>
+ gain
+ </span>
+ </t>
+ </td>
<td t-if="not user['website_published']" class="align-middle font-weight-bold text-right text-nowrap">
<span class="badge badge-danger font-weight-normal px-2 py-1 m-1">Unpublished</span>
</td>