From 7974bf54411f189d1d52da90beae95da51d90dd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9rome=20Maes?= <jem@openerp.com> Date: Mon, 21 Sep 2015 17:54:36 +0200 Subject: [PATCH] [FIX] website_mail, website_sale : redirect to login page to post comment Add 'force_display' param for the frontend chatter to allow public user to see the textarea. When submitting his comment, he will be redirect to login page (like it was before generic chatter) in both mode (json and post mode). This required changing the error handeling of json mode : when a login is required, the user switch to post mode to allow http redirect, keeping its submitted params (rating, comment, ...). --- addons/website_mail/controllers/main.py | 55 ++++++++++++++----- .../static/src/js/message_post.js | 15 +++-- addons/website_mail/views/website_mail.xml | 4 +- .../website_sale/controllers/website_mail.py | 12 ++-- addons/website_sale/views/templates.xml | 1 + 5 files changed, 60 insertions(+), 27 deletions(-) diff --git a/addons/website_mail/controllers/main.py b/addons/website_mail/controllers/main.py index 5ae3b6a07346..1d98899ba4e0 100644 --- a/addons/website_mail/controllers/main.py +++ b/addons/website_mail/controllers/main.py @@ -1,10 +1,14 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. + +import urllib + from hashlib import sha1 from time import time from werkzeug.exceptions import NotFound +from werkzeug import url_encode -from openerp import SUPERUSER_ID +from openerp import SUPERUSER_ID, _ from openerp.addons.web import http from openerp.addons.web.http import request @@ -65,6 +69,7 @@ def _message_post_helper(res_model='', res_id=None, message='', token='', token_ res = res.sudo() else: raise NotFound() + kw.pop('force_display', False) return res.with_context({'mail_create_nosubscribe': nosubscribe}).message_post(body=message, message_type=kw.pop('message_type', False) or "comment", subtype=kw.pop('subtype', False) or "mt_comment", @@ -147,27 +152,47 @@ class WebsiteMail(http.Controller): ], context=context)) == 1 return values + def _generate_login_url(self): + url_params = request.params + url_params.pop('force_display') + url_params.update(redirect=request.httprequest.referrer) + redirect_login_url = '/website_mail/post/post' + '?' + url_encode(url_params) + url = '/web/login?redirect=%s' % urllib.quote(redirect_login_url) + return url + @http.route(['/website_mail/post/json'], type='json', auth='public', website=True) def chatter_json(self, res_model='', res_id=None, message='', **kw): + # if the chatter is displayed but the user is not logged, redirect him to login page + if kw.get('force_display') and not request.session.uid: + return {'redirect': self._generate_login_url()} + # otherwise, try to post the comment res_id = int(res_id) try: - msg = _message_post_helper(res_model, res_id, message, **kw) - data = { - 'id': msg.id, - 'body': msg.body, - 'date': msg.date, - 'author': msg.author_id.name, - 'image_url': request.website.image_url(msg.author_id, 'image_small') - } - return data + if message: + msg = _message_post_helper(res_model, res_id, message, **kw) + data = { + 'id': msg.id, + 'body': msg.body, + 'date': msg.date, + 'author': msg.author_id.name, + 'image_url': request.website.image_url(msg.author_id, 'image_small') + } + return data + else: + return {'error': _('Empty comments are not allowed. Please fill the textearea.')} except Exception: - return False + return {'error': _('Oops! Something went wrong. Try to reload the page and to log in.')} @http.route(['/website_mail/post/post'], type='http', method=['POST'], auth='public', website=True) def chatter_post(self, res_model='', res_id=None, message='', redirect=None, **kw): - res_id = int(res_id) url = request.httprequest.referrer - if message: - message = _message_post_helper(res_model, res_id, message, **kw) - url = url + "#message-%s" % (message.id,) + if kw.get('force_display') and not request.session.uid: + # set the redirection param to be redirect here when logged in + url = self._generate_login_url() + else: + res_id = int(res_id) + url = redirect or url + if message: + message = _message_post_helper(res_model, res_id, message, **kw) + url = url + "#message-%s" % (message.id,) return request.redirect(url) diff --git a/addons/website_mail/static/src/js/message_post.js b/addons/website_mail/static/src/js/message_post.js index 3ddb5e2ce908..8005cb16677c 100644 --- a/addons/website_mail/static/src/js/message_post.js +++ b/addons/website_mail/static/src/js/message_post.js @@ -38,11 +38,16 @@ odoo.define('website_mail.thread', function(require) { $button.prepend('<i class="fa fa-refresh fa-spin"></i> '); // post message, shw/hide error message and empty textarea ajax.jsonRpc(action, 'call', data).then(function (result) { - if (result) { - $error.fadeOut(); - self.prepend_message(result); - $form.find('textarea').val(''); - } else { + if (!result['error']) { + if(result['redirect']){ + window.location.replace(result['redirect']); + }else{ + $error.fadeOut(); + self.prepend_message(result); + $form.find('textarea').val(''); + } + } else { // display error message from server + $error.html(result['error']); $error.fadeIn(); } $button.html(button_bk); diff --git a/addons/website_mail/views/website_mail.xml b/addons/website_mail/views/website_mail.xml index 2e246ac18c25..e30f8ed57641 100644 --- a/addons/website_mail/views/website_mail.xml +++ b/addons/website_mail/views/website_mail.xml @@ -38,10 +38,11 @@ object_shasign function defined in the main controller of website_portal :sha_time string (optional): timestamp of the shasign generation :chatter_mode string: 'json' or 'post' depending on what kind of request you want + :force_display : True if the chatter should be display, but posting a message required a logged user. Otherwise, the user will be redirect to the login page. --> <section id="discussion" class="hidden-print oe_website_portal o_website_mail_thread"> <section class="mb32 css_editable_mode_hidden hidden-print"> - <form class="o_website_chatter_form" t-attf-action="/website_mail/post/#{chatter_mode}" method="POST" t-if="website.env.user != request.env['ir.model.data'].xmlid_to_object('base.public_user') or token or sha_in"> + <form class="o_website_chatter_form" t-attf-action="/website_mail/post/#{chatter_mode}" method="POST" t-if="website.env.user != request.env['ir.model.data'].xmlid_to_object('base.public_user') or token or sha_in or force_display"> <img class="img pull-left img-circle" t-attf-src="/web/image/res.partner/#{user_id.partner_id.id}/image_small/50x50" style="width: 50px; margin-right: 10px;"/> <div class="pull-left mb32" style="width: 75%%"> <textarea rows="4" name="message" class="form-control" placeholder="Write a message..."></textarea> @@ -51,6 +52,7 @@ <input type='hidden' name="sha_in" t-att-value="sha_in" t-if="sha_in"/> <input type='hidden' name="sha_time" t-att-value="sha_time" t-if="sha_time"/> <input type='hidden' name="token_field" t-att-value="token_field" t-if="token_field"/> + <input type='hidden' name="force_display" t-att-value="force_display" t-if="force_display"/> <div class="alert alert-danger mt8 mb0 o_website_chatter_error" style="display:none;"> Oops! Something went wrong. Try to reload the page and to log in. </div> diff --git a/addons/website_sale/controllers/website_mail.py b/addons/website_sale/controllers/website_mail.py index 937adbb3d0f3..dd6b0b7757ed 100644 --- a/addons/website_sale/controllers/website_mail.py +++ b/addons/website_sale/controllers/website_mail.py @@ -15,24 +15,24 @@ class WebsiteMailController(WebsiteMail): def chatter_json(self, res_model='', res_id=None, message='', **kw): params = kw.copy() params.pop('rating', False) - message_data = super(WebsiteMailController, self).chatter_json(res_model=res_model, res_id=res_id, message=message, **params) - if message_data and kw.get('rating') and res_model == 'product.template': # restrict rating only for product template + result = super(WebsiteMailController, self).chatter_json(res_model=res_model, res_id=res_id, message=message, **params) + if result and result.get('id') and kw.get('rating') and res_model == 'product.template': # restrict rating only for product template rating = request.env['rating.rating'].create({ 'rating': float(kw.get('rating')), 'res_model': res_model, 'res_id': res_id, - 'message_id': message_data['id'], + 'message_id': result['id'], }) - message_data.update({ + result.update({ 'rating_default_value': rating.rating, 'rating_disabled': True, }) - return message_data + return result @http.route(['/website_mail/post/post'], type='http', method=['POST'], auth='public', website=True) def chatter_post(self, res_model='', res_id=None, message='', redirect=None, **kw): params = kw.copy() - params.pop('rating') + params.pop('rating', False) response = super(WebsiteMailController, self).chatter_post(res_model=res_model, res_id=res_id, message=message, redirect=redirect, **params) if kw.get('rating') and res_model == 'product.template': # restrict rating only for product template try: diff --git a/addons/website_sale/views/templates.xml b/addons/website_sale/views/templates.xml index dfd39c2542a9..bc01e1abc64d 100644 --- a/addons/website_sale/views/templates.xml +++ b/addons/website_sale/views/templates.xml @@ -574,6 +574,7 @@ <t t-set="object" t-value="product"/> <t t-set="chatter_mode" t-value="'json'"/> <t t-set="rating_enable" t-value="True"/> + <t t-set="force_display" t-value="True"/> </t> </div> </section> -- GitLab