Files
odoo_source/addons/rating/controllers/main.py
T
std-odooandThibault Delavallée 6ebb72332e [IMP] rating: improve rating notification with delaying until feedback
Purpose
=======

When someone receives an email with a rating request, he can click on a
smiley. But if he does not write a feedback, nobody is notified. This means
some rating as somehow lost and people are not notified of it.

Specifications
==============

Now, when the user clicks on the smiley, a message is posted but notifications
are not yet sent. This uses the new ``mail.message.schedule`` mechanism added
in this PR. It gives the user some time to write their feedback and send it.

If they write and submit a feedback, the notification process is launched.
Emails and inbox notifications are created and sent.

If they don't write a feedback notification process is launch after 2 hours.

Testing
=======

Some cleaning is done in tests, notably to split some fields tests from
performance test. A bit performance test is also split into sub tests in
order to better understand queries.

Task-2207626 (Rating: Log ratings, post feedbacks)

Part-of: odoo/odoo#95623
Co-authored-by: Thibault Delavallée <tde@odoo.com>
2022-08-25 19:57:17 +02:00

79 lines
2.6 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import logging
import werkzeug
from odoo import http
from odoo.http import request
from odoo.tools.translate import _
from odoo.tools.misc import get_lang
_logger = logging.getLogger(__name__)
MAPPED_RATES = {
1: 1,
5: 3,
10: 5,
}
class Rating(http.Controller):
@http.route('/rate/<string:token>/<int:rate>', type='http', auth="public", website=True)
def action_open_rating(self, token, rate, **kwargs):
if rate not in (1, 3, 5):
raise ValueError(_("Incorrect rating: should be 1, 3 or 5 (received %d)"), rate)
rating, record_sudo = self._get_rating_and_record(token)
record_sudo.rating_apply(
rate,
rating=rating,
feedback=_('Customer rated %r.', record_sudo.display_name),
subtype_xmlid=None,
notify_delay_send=True,
)
lang = rating.partner_id.lang or get_lang(request.env).code
return request.env['ir.ui.view'].with_context(lang=lang)._render_template('rating.rating_external_page_submit', {
'rating': rating,
'token': token,
'rate_names': {
5: _("Satisfied"),
3: _("Okay"),
1: _("Dissatisfied"),
},
'rate': rate,
})
@http.route(['/rate/<string:token>/submit_feedback'], type="http", auth="public", methods=['post'], website=True)
def action_submit_rating(self, token, rate, **kwargs):
rate = int(rate)
if rate not in (1, 3, 5):
raise ValueError(_("Incorrect rating: should be 1, 3 or 5 (received %d)"), rate)
rating, record_sudo = self._get_rating_and_record(token)
record_sudo.rating_apply(
rate,
rating=rating,
feedback=kwargs.get('feedback'),
subtype_xmlid=None, # force default subtype choice
)
lang = rating.partner_id.lang or get_lang(request.env).code
return request.env['ir.ui.view'].with_context(lang=lang)._render_template('rating.rating_external_page_view', {
'web_base_url': rating.get_base_url(),
'rating': rating,
})
def _get_rating_and_record(self, token):
rating_sudo = request.env['rating.rating'].sudo().search([('access_token', '=', token)])
if not rating_sudo:
raise werkzeug.exceptions.NotFound()
record_sudo = request.env[rating_sudo.res_model].sudo().browse(rating_sudo.res_id)
if not record_sudo.exists():
raise werkzeug.exceptions.NotFound()
return rating_sudo, record_sudo