diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
| commit | 3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch) | |
| tree | a44932296ef4a9b71d5f010906253d8c53727726 /addons/rating/controllers/main.py | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/rating/controllers/main.py')
| -rw-r--r-- | addons/rating/controllers/main.py | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/addons/rating/controllers/main.py b/addons/rating/controllers/main.py new file mode 100644 index 00000000..a80d131b --- /dev/null +++ b/addons/rating/controllers/main.py @@ -0,0 +1,66 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +import logging + +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('/rating/<string:token>/<int:rate>', type='http', auth="public", website=True) + def open_rating(self, token, rate, **kwargs): + _logger.warning('/rating is deprecated, use /rate instead') + assert rate in (1, 5, 10), "Incorrect rating" + return self.action_open_rating(token, MAPPED_RATES.get(rate), **kwargs) + + @http.route(['/rating/<string:token>/submit_feedback'], type="http", auth="public", methods=['post'], website=True) + def submit_rating(self, token, **kwargs): + _logger.warning('/rating is deprecated, use /rate instead') + rate = int(kwargs.get('rate')) + assert rate in (1, 5, 10), "Incorrect rating" + kwargs['rate'] = MAPPED_RATES.gate(rate) + return self.action_submit_rating(token, **kwargs) + + @http.route('/rate/<string:token>/<int:rate>', type='http', auth="public", website=True) + def action_open_rating(self, token, rate, **kwargs): + assert rate in (1, 3, 5), "Incorrect rating" + rating = request.env['rating.rating'].sudo().search([('access_token', '=', token)]) + if not rating: + return request.not_found() + rate_names = { + 5: _("satisfied"), + 3: _("not satisfied"), + 1: _("highly dissatisfied") + } + 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': rate_names, 'rate': rate + }) + + @http.route(['/rate/<string:token>/submit_feedback'], type="http", auth="public", methods=['post'], website=True) + def action_submit_rating(self, token, **kwargs): + rate = int(kwargs.get('rate')) + assert rate in (1, 3, 5), "Incorrect rating" + rating = request.env['rating.rating'].sudo().search([('access_token', '=', token)]) + if not rating: + return request.not_found() + record_sudo = request.env[rating.res_model].sudo().browse(rating.res_id) + record_sudo.rating_apply(rate, token=token, feedback=kwargs.get('feedback')) + 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': request.env['ir.config_parameter'].sudo().get_param('web.base.url'), + 'rating': rating, + }) |
