| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 3 |
|
| 4 |
from odoo import models |
| 5 |
from odoo.tools import format_datetime |
| 6 |
|
| 7 |
|
| 8 |
class MailMessage(models.Model): |
| 9 |
_inherit = 'mail.message' |
| 10 |
|
| 11 |
def _portal_get_default_format_properties_names(self, options=None): |
| 12 |
""" Add request for rating information |
| 13 |
|
| 14 |
:param dict options: supports 'rating_include' option allowing to |
| 15 |
conditionally include rating information; |
| 16 |
""" |
| 17 |
properties_names = super()._portal_get_default_format_properties_names() |
| 18 |
if options and options.get('rating_include'): |
| 19 |
properties_names |= { |
| 20 |
'rating', |
| 21 |
'rating_value', |
| 22 |
} |
| 23 |
return properties_names |
| 24 |
|
| 25 |
def _portal_message_format(self, properties_names, options=None): |
| 26 |
""" If requested, add rating information to returned formatted values. |
| 27 |
|
| 28 |
Note: rating information combine both statistics (see 'rating_get_stats' |
| 29 |
if available on model) and rating / publication information. """ |
| 30 |
vals_list = super()._portal_message_format(properties_names, options=options) |
| 31 |
if not 'rating' in properties_names: |
| 32 |
return vals_list |
| 33 |
|
| 34 |
related_rating = self.env['rating.rating'].sudo().search_read( |
| 35 |
[('message_id', 'in', self.ids)], |
| 36 |
["id", "publisher_comment", "publisher_id", "publisher_datetime", "message_id"] |
| 37 |
) |
| 38 |
message_to_rating = { |
| 39 |
rating['message_id'][0]: self._portal_message_format_rating(rating) |
| 40 |
for rating in related_rating |
| 41 |
} |
| 42 |
|
| 43 |
for message, values in zip(self, vals_list): |
| 44 |
values["rating_id"] = message_to_rating.get(message.id, {}) |
| 45 |
|
| 46 |
record = self.env[message.model].browse(message.res_id) |
| 47 |
if hasattr(record, 'rating_get_stats'): |
| 48 |
values['rating_stats'] = record.sudo().rating_get_stats() |
| 49 |
|
| 50 |
return vals_list |
| 51 |
|
| 52 |
def _portal_message_format_rating(self, rating_values): |
| 53 |
""" From 'rating_values' get an updated version formatted for frontend |
| 54 |
display. |
| 55 |
|
| 56 |
:param dict rating_values: values coming from reading ratings |
| 57 |
in database; |
| 58 |
|
| 59 |
:returns: updated rating_values |
| 60 |
:rtype: dict |
| 61 |
""" |
| 62 |
publisher_id, publisher_name = rating_values['publisher_id'] or [False, ''] |
| 63 |
rating_values['publisher_avatar'] = f'/web/image/res.partner/{publisher_id}/avatar_128/50x50' if publisher_id else '' |
| 64 |
rating_values['publisher_comment'] = rating_values['publisher_comment'] or '' |
| 65 |
rating_values['publisher_datetime'] = format_datetime(self.env, rating_values['publisher_datetime']) |
| 66 |
rating_values['publisher_id'] = publisher_id |
| 67 |
rating_values['publisher_name'] = publisher_name |
| 68 |
return rating_values |