| 1 |
from odoo import api, fields, models, exceptions, _ |
| 2 |
|
| 3 |
|
| 4 |
class RatingRating(models.Model): |
| 5 |
_inherit = 'rating.rating' |
| 6 |
|
| 7 |
# Adding information for comment a rating message |
| 8 |
publisher_comment = fields.Text("Publisher comment") |
| 9 |
publisher_id = fields.Many2one('res.partner', 'Commented by', |
| 10 |
ondelete='set null', readonly=True, |
| 11 |
index='btree_not_null') |
| 12 |
publisher_datetime = fields.Datetime("Commented on", readonly=True) |
| 13 |
|
| 14 |
@api.model_create_multi |
| 15 |
def create(self, vals_list): |
| 16 |
for values in vals_list: |
| 17 |
self._synchronize_publisher_values(values) |
| 18 |
ratings = super().create(vals_list) |
| 19 |
if any(rating.publisher_comment for rating in ratings): |
| 20 |
ratings._check_synchronize_publisher_values() |
| 21 |
return ratings |
| 22 |
|
| 23 |
def write(self, vals): |
| 24 |
self._synchronize_publisher_values(vals) |
| 25 |
return super().write(vals) |
| 26 |
|
| 27 |
def _check_synchronize_publisher_values(self): |
| 28 |
""" Either current user is a member of website restricted editor group |
| 29 |
(done here by fetching the group record then using has_group, as it may |
| 30 |
not be defined and we do not want to make a complete bridge module just |
| 31 |
for that). Either write access on document is granted. """ |
| 32 |
editor_group = self.env['ir.model.data']._xmlid_to_res_id('website.group_website_restricted_editor') |
| 33 |
if editor_group and self.env.user.has_group('website.group_website_restricted_editor'): |
| 34 |
return |
| 35 |
for model, model_data in self._classify_by_model().items(): |
| 36 |
records = self.env[model].browse(model_data['record_ids']) |
| 37 |
try: |
| 38 |
records.check_access('write') |
| 39 |
except exceptions.AccessError as e: |
| 40 |
raise exceptions.AccessError( |
| 41 |
_("Updating rating comment require write access on related record") |
| 42 |
) from e |
| 43 |
|
| 44 |
def _synchronize_publisher_values(self, values): |
| 45 |
""" Force publisher partner and date if not given in order to have |
| 46 |
coherent values. Those fields are readonly as they are not meant |
| 47 |
to be modified manually, behaving like a tracking. """ |
| 48 |
if values.get('publisher_comment'): |
| 49 |
self._check_synchronize_publisher_values() |
| 50 |
if not values.get('publisher_datetime'): |
| 51 |
values['publisher_datetime'] = fields.Datetime.now() |
| 52 |
if not values.get('publisher_id'): |
| 53 |
values['publisher_id'] = self.env.user.partner_id.id |
| 54 |
return values |