[IMP] base, website: prefill the country phone code in website forms

Before this commit the phone fields inside forms did start in a blank
state.

After this commit the empty phone fields are pre-filled with "+" and the
country phone code.
The country is determined based on the IP address with the GeoIP
database.  The phone code is obtained from `res.country` where it is
kept in cache.

task-2463563

closes odoo/odoo#78068

Signed-off-by: Romain Derie (rde) <rde@odoo.com>
Co-authored-by: Benoit Socias <bso@odoo.com>
This commit is contained in:
jpr-odoo
2022-02-04 16:53:54 +00:00
committed by Romain Derie
co-authored by Benoit Socias
parent f914d42fee
commit 1bd4f3c7bd
3 changed files with 22 additions and 5 deletions
+4 -1
View File
@@ -438,9 +438,12 @@ class Http(models.AbstractModel):
@api.model
def get_frontend_session_info(self):
session_info = super(Http, self).get_frontend_session_info()
geoip_country_code = request.session.get('geoip', {}).get('country_code')
geoip_phone_code = request.env['res.country']._phone_code_for(geoip_country_code) if geoip_country_code else None
session_info.update({
'is_website_user': request.env.user.id == request.website.user_id.id,
'geoip_country_code': request.session.get('geoip', {}).get('country_code'),
'geoip_country_code': geoip_country_code,
'geoip_phone_code': geoip_phone_code,
})
if request.env.user.has_group('website.group_website_publisher'):
session_info.update({
@@ -68,7 +68,6 @@ odoo.define('website.s_website_form', function (require) {
args: [session.user_id, this._getUserPreFillFields()],
}))[0] || {};
}
return res;
},
start: function () {
@@ -140,7 +139,13 @@ odoo.define('website.s_website_form', function (require) {
$field.data('website_form_original_default_value', $field.val());
});
}
if (session.geoip_phone_code) {
this.el.querySelectorAll('input[type="tel"]').forEach(telField => {
if (!telField.value) {
telField.value = '+' + session.geoip_phone_code;
}
});
}
// Check disabled states
this.inputEls = this.$target[0].querySelectorAll('.s_website_form_field.s_website_form_field_hidden_if .s_website_form_input');
this._disabledStates = new Map();
+11 -2
View File
@@ -3,7 +3,7 @@
import re
import logging
from odoo import api, fields, models
from odoo import api, fields, models, tools
from odoo.osv import expression
from odoo.exceptions import UserError
from psycopg2 import IntegrityError
@@ -96,6 +96,11 @@ class Country(models.Model):
return ids
@api.model
@tools.ormcache('code')
def _phone_code_for(self, code):
return self.search([('code', '=', code)]).phone_code
@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
@@ -106,7 +111,11 @@ class Country(models.Model):
def write(self, vals):
if vals.get('code'):
vals['code'] = vals['code'].upper()
return super(Country, self).write(vals)
res = super().write(vals)
if ('code' in vals or 'phone_code' in vals):
# Intentionally simplified by not clearing the cache in create and unlink.
self.clear_caches()
return res
def get_address_fields(self):
self.ensure_one()