misc: add support for a country to use as default in geocoding (#37048)

This commit is contained in:
Frédéric Péters 2021-04-18 18:33:43 +02:00
parent e47209ac47
commit 1da13f2f32
3 changed files with 23 additions and 0 deletions

View File

@ -3942,6 +3942,17 @@ def test_form_map_geolocation_text_field(pub):
formdef.data_class().wipe()
assert 'qommon.map.js' in resp.text
assert 'qommon.geolocation.js' in resp.text
assert 'WCS_DEFAULT_GEOCODING_COUNTRY' not in resp.text
# check page has default geocoding country in a javascript variable if set
if not pub.site_options.has_section('options'):
pub.site_options.add_section('options')
pub.site_options.set('options', 'default-geocoding-country', 'France')
with open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') as fd:
pub.site_options.write(fd)
resp = get_app(pub).get('/test/')
assert 'WCS_DEFAULT_GEOCODING_COUNTRY' in resp.text
def test_form_map_field_prefill_address(pub):

View File

@ -125,6 +125,11 @@ class HTTPResponse(quixote.http_response.HTTPResponse):
self.add_javascript_code(
'var WCS_ROOT_URL = "%s";\n' % get_publisher().get_frontoffice_url()
)
default_geocoding_country = get_publisher().get_site_option('default-geocoding-country')
if default_geocoding_country:
self.add_javascript_code(
'var WCS_DEFAULT_GEOCODING_COUNTRY = "%s";' % default_geocoding_country
)
if script_name == 'wcs.listing.js':
self.add_javascript(['../../i18n.js', 'jquery.js', 'jquery-ui.js'])
if script_name == 'qommon.admin.js':

View File

@ -129,6 +129,7 @@ function geoloc_prefill(element_type, element_value)
$form[0].wait_for_changes = true;
var address = '';
var found_city = false;
var found_country = false;
$(['number-and-street', 'house', 'road', 'postcode', 'city', 'country']).each(function(idx, elem) {
var part = $('div[data-geolocation="' + elem + '"]').find('input, textarea, select').val();
if (part) {
@ -139,9 +140,15 @@ function geoloc_prefill(element_type, element_value)
if (elem == 'postcode' || elem == 'city') {
found_city = true;
}
if (elem == 'country') {
found_country = true;
}
}
});
if (found_city) {
if (!found_country && typeof(WCS_DEFAULT_GEOCODING_COUNTRY) !== 'undefined') {
address += WCS_DEFAULT_GEOCODING_COUNTRY;
}
$.getJSON(WCS_ROOT_URL + '/api/geocoding?q=' + address, function(data) {
if (data && $(data).length > 0) {
var coords = {lat: data[0].lat, lng: data[0].lon};