Skip to content
Snippets Groups Projects
Commit 7974bf54 authored by Jérome Maes's avatar Jérome Maes
Browse files

[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, ...).
parent 6ed93f17
No related branches found
No related tags found
No related merge requests found
# -*- 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)
......@@ -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);
......
......@@ -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>
......
......@@ -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:
......
......@@ -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>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment