Files
odoo_source/addons/rating/controllers/main.py
T
Thibault Delavallée cce67a0a4d [REF] rating, various: cleanup rating from routes
PURPOSE

Cleanup code and flow of customer rating: routes, rating_apply, rating and
message creation and update.

SPECIFICATIONS

Cleanup code about rating controllers and rating_apply. Notably correctly
link a message to a rating once a feedback is posted, whatever the flow.

Update ``rating_apply`` to use either a token, either an existing rating to
udpate it and post a message. Always link the message and the rating to
be sure DB data are coherent.

This requires an update in several demo data to correctly set tokens on
ratings and use it in calls to ``rating_apply`` done in xml files.

Extract default subtype computation of rating_apply in a sub-method to allow
setting this parameter without having to deal with ``rating_apply`` details.

CODE CLEANUP

Split behavior that is generic but actually used only in project about stage
update based on rating. Move it directly in project. As it is used only once
and as generic approach is hardcoded based on fields names better keep it
localized.

Task-2812665 (Rating: Cleanup rating flow code)

Part-of: odoo/odoo#80707
2022-04-04 17:48:23 +02:00

74 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 = request.env['rating.rating'].sudo().search([('access_token', '=', token)])
if not rating:
return werkzeug.exceptions.Forbidden()
record_sudo = request.env[rating.res_model].sudo().browse(rating.res_id)
if not record_sudo.exists():
return werkzeug.exceptions.Forbidden()
rating.write({'rating': rate, 'consumed': 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 = request.env['rating.rating'].sudo().search([('access_token', '=', token)])
if not rating:
return werkzeug.exceptions.Forbidden()
record_sudo = request.env[rating.res_model].sudo().browse(rating.res_id)
if not record_sudo.exists():
return werkzeug.exceptions.Forbidden()
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,
})