[FIX] website: get visitor tz directly from cookie
Since tz is now (from6eb4762fee) added to the client cookie, visitor timezone can directly be set when creating the visitor, using the request. Jstz can be removed as even for module in ENT that used this lib, we can get the timezone using Intl lib instead. This will also reduce the number of request made only to get visitor's timezone. This commit reverts part of17e8402523Linked ENT PR: 12963 Task ID: 2333825 closes odoo/odoo#59501 X-original-commit: 3d9a2de3575777c87327691319d89f9de6b8eada Related: odoo/enterprise#13918 Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
This commit is contained in:
@@ -502,15 +502,6 @@ class Website(Home):
|
||||
request.env['web_editor.assets'].make_scss_customization(url, values)
|
||||
return True
|
||||
|
||||
@http.route(['/website/update_visitor_timezone'], type='json', auth="public", website=True)
|
||||
def update_visitor_timezone(self, timezone):
|
||||
visitor_sudo = request.env['website.visitor']._get_visitor_from_request()
|
||||
if visitor_sudo:
|
||||
if timezone in pytz.all_timezones:
|
||||
visitor_sudo.write({'timezone': timezone})
|
||||
return True
|
||||
return False
|
||||
|
||||
# ------------------------------------------------------
|
||||
# Server actions
|
||||
# ------------------------------------------------------
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
import uuid
|
||||
import pytz
|
||||
|
||||
from odoo import fields, models, api, _
|
||||
from odoo.addons.base.models.res_partner import _tz_get
|
||||
@@ -185,7 +186,11 @@ class WebsiteVisitor(models.Model):
|
||||
# Cookie associated to a Partner
|
||||
visitor = Visitor
|
||||
|
||||
if force_create and not visitor:
|
||||
if visitor and not visitor.timezone:
|
||||
tz = self._get_visitor_timezone()
|
||||
if tz:
|
||||
visitor.timezone = tz
|
||||
if not visitor and force_create:
|
||||
visitor = self._create_visitor()
|
||||
|
||||
return visitor
|
||||
@@ -235,6 +240,11 @@ class WebsiteVisitor(models.Model):
|
||||
'country_id': country_id,
|
||||
'website_id': request.website.id,
|
||||
}
|
||||
|
||||
tz = self._get_visitor_timezone()
|
||||
if tz:
|
||||
vals['timezone'] = tz
|
||||
|
||||
if not self.env.user._is_public():
|
||||
vals['partner_id'] = self.env.user.partner_id.id
|
||||
vals['name'] = self.env.user.partner_id.name
|
||||
@@ -298,3 +308,12 @@ class WebsiteVisitor(models.Model):
|
||||
self.env.cr.execute(query, (date_now, self.id), log_exceptions=False)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def _get_visitor_timezone(self):
|
||||
tz = request.httprequest.cookies.get('tz') if request else None
|
||||
if tz in pytz.all_timezones:
|
||||
return tz
|
||||
elif not self.env.user._is_public():
|
||||
return self.env.user.tz
|
||||
else:
|
||||
return None
|
||||
|
||||
+1
-2
File diff suppressed because one or more lines are too long
@@ -1,36 +1 @@
|
||||
//
|
||||
// This file is meant to determine the timezone of a website visitor
|
||||
// If the visitor already exists, no need to keep the timezone cookie
|
||||
// as the timezone is set on the visitor.
|
||||
//
|
||||
odoo.define('website.visitor_timezone', function (require) {
|
||||
'use strict';
|
||||
|
||||
var ajax = require('web.ajax');
|
||||
var utils = require('web.utils');
|
||||
var publicWidget = require('web.public.widget');
|
||||
|
||||
publicWidget.registry.visitorTimezone = publicWidget.Widget.extend({
|
||||
selector: '#wrapwrap',
|
||||
|
||||
start: function () {
|
||||
if (!localStorage.getItem('website.found_visitor_timezone')) {
|
||||
var timezone = jstz.determine().name();
|
||||
this._rpc({
|
||||
route: '/website/update_visitor_timezone',
|
||||
params: {
|
||||
'timezone': timezone,
|
||||
},
|
||||
}).then(function (result) {
|
||||
if (result) {
|
||||
localStorage.setItem('website.found_visitor_timezone', true);
|
||||
}
|
||||
});
|
||||
}
|
||||
return this._super.apply(this, arguments);
|
||||
},
|
||||
});
|
||||
|
||||
return publicWidget.registry.visitorTimezone;
|
||||
|
||||
});
|
||||
// To remove after v14.0
|
||||
@@ -75,7 +75,6 @@
|
||||
</xpath>
|
||||
<xpath expr="//script[last()]" position="after">
|
||||
<script type="text/javascript" src="/website/static/src/js/set_view_track.js"/>
|
||||
<script type="text/javascript" src="/website/static/lib/jstz.min.js"/>
|
||||
<script type="text/javascript" src="/website/static/src/js/utils.js"/>
|
||||
|
||||
<script type="text/javascript" src="/website/static/src/js/content/website_root.js"/>
|
||||
@@ -85,7 +84,6 @@
|
||||
|
||||
<script type="text/javascript" src="/website/static/src/js/menu/navbar.js"/>
|
||||
|
||||
<script type="text/javascript" src="/website/static/src/js/visitor_timezone.js"/>
|
||||
<script type="text/javascript" src="/website/static/src/js/show_password.js"/>
|
||||
<script type="text/javascript" src="/website/static/src/js/post_link.js"/>
|
||||
<!-- Custom empty file for user javascript -->
|
||||
|
||||
Reference in New Issue
Block a user