diff --git a/addons/website_mail/controllers/main.py b/addons/website_mail/controllers/main.py index 5ae3b6a0734..1d98899ba4e 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 3ddb5e2ce90..8005cb16677 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(' '); // 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 2e246ac18c2..e30f8ed5764 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. -->
-
+
@@ -51,6 +52,7 @@ + diff --git a/addons/website_sale/controllers/website_mail.py b/addons/website_sale/controllers/website_mail.py index 937adbb3d0f..dd6b0b7757e 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 dfd39c2542a..bc01e1abc64 100644 --- a/addons/website_sale/views/templates.xml +++ b/addons/website_sale/views/templates.xml @@ -574,6 +574,7 @@ +