Files
odoo_source/addons/rating/models/mail_message.py
T
Patrick Hoste d9247094c2 [FIX] rating: fix search on rating
Fix the access error you could get when filtering on ratings when not logged
in or when the user has no rights to access ratings.

How to reproduce: filter on channel review ratings in eLearning frontend
when being anonymous le portal.

As rating is a technical model, we have to first retrieve messages linked
to that rating value and afterwards return a new domain used to build
the final search.

Task ID : 2167776
PR : #42847

closes odoo/odoo#44380

X-original-commit: 7a241e9c21046dababc2744dd94b0c47e2d8a931
Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
2020-01-31 12:38:33 +00:00

28 lines
1.1 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models
class MailMessage(models.Model):
_inherit = 'mail.message'
rating_ids = fields.One2many('rating.rating', 'message_id', groups='base.group_user', string='Related ratings')
rating_value = fields.Float(
'Rating Value', compute='_compute_rating_value', compute_sudo=True,
store=False, search='_search_rating_value')
@api.depends('rating_ids', 'rating_ids.rating')
def _compute_rating_value(self):
ratings = self.env['rating.rating'].search([('message_id', 'in', self.ids), ('consumed', '=', True)], order='create_date DESC')
mapping = dict((r.message_id.id, r.rating) for r in ratings)
for message in self:
message.rating_value = mapping.get(message.id, 0.0)
def _search_rating_value(self, operator, operand):
ratings = self.env['rating.rating'].sudo().search([
('rating', operator, operand),
('message_id', '!=', False)
])
return [('id', 'in', ratings.mapped('message_id').ids)]