This commit provides 1 more field on the rating mixin: the average of the ratings of the document. Before that, the only way to get the average was to call `rating_get_stats` record per record. As we are on a mixin, this new field is not stored to avoid recomputation each time a rating is added. To improve performance, we decided to compute `rating_count` and `rating_avg` in one `read_group`. This commit also set an extendable method to get the domain of the rating to include in those statistics computation, called `_rating_domain`. This way, each model inheriting the rating.mixin can define the subset of pertinent ratings. As consequence, this commit uniformized the way average is displayed: instead of taking the closer value to the 0.5 value, it only round it at 2 digits. Task-1902304
30 lines
1.2 KiB
Python
30 lines
1.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from odoo import http
|
|
from odoo.http import request
|
|
|
|
from odoo.addons.portal.controllers.mail import PortalChatter
|
|
|
|
|
|
class WebsiteRating(PortalChatter):
|
|
|
|
@http.route()
|
|
def portal_chatter_init(self, res_model, res_id, domain=False, limit=False, **kwargs):
|
|
result = super(WebsiteRating, self).portal_chatter_init(res_model, res_id, domain=domain, limit=limit, **kwargs)
|
|
# get the rating statistics about the record
|
|
if kwargs.get('rating_include'):
|
|
record = request.env[res_model].browse(res_id)
|
|
if hasattr(record, 'rating_get_stats'):
|
|
result['rating_stats'] = record.rating_get_stats()
|
|
return result
|
|
|
|
@http.route()
|
|
def portal_message_fetch(self, res_model, res_id, domain=False, limit=False, offset=False, **kw):
|
|
# add 'rating_include' in context, to fetch them in portal_message_format
|
|
if kw.get('rating_include'):
|
|
context = dict(request.context)
|
|
context['rating_include'] = True
|
|
request.context = context
|
|
return super(WebsiteRating, self).portal_message_fetch(res_model, res_id, domain=domain, limit=limit, offset=offset, **kw)
|