Skip to content

Commit 0705fee

Browse files
authored
Merge pull request #9688 from elpaso/server-locale-override-backport-3_4
[server] Allow locale and group separator override
2 parents 0b1556d + 00f49a0 commit 0705fee

12 files changed

+857
-24
lines changed

python/server/auto_generated/qgsserversettings.sip.in

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Load settings according to current environment variables.
3939
%Docstring
4040
Load setting for a specific environment variable name.
4141

42-
:return: true if loading is successful, false in case of an invalid name.
42+
:return: TRUE if loading is successful, FALSE in case of an invalid name.
4343
%End
4444

4545
void logSummary() const;
@@ -58,7 +58,7 @@ Returns the ini file loaded by QSetting.
5858
%Docstring
5959
Returns parallel rendering setting.
6060

61-
:return: true if parallel rendering is activated, false otherwise.
61+
:return: TRUE if parallel rendering is activated, FALSE otherwise.
6262
%End
6363

6464
int maxThreads() const;
@@ -117,6 +117,24 @@ Returns the cache size.
117117
Returns the cache directory.
118118

119119
:return: the directory.
120+
%End
121+
122+
QString overrideSystemLocale() const;
123+
%Docstring
124+
Overrides system locale
125+
126+
:return: the optional override for system locale.
127+
128+
.. versionadded:: 3.8
129+
%End
130+
131+
bool showGroupSeparator() const;
132+
%Docstring
133+
Show group (thousand) separator
134+
135+
:return: if group separator must be shown, default to FALSE.
136+
137+
.. versionadded:: 3.8
120138
%End
121139

122140
};

src/server/qgsserver.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,26 @@ QString QgsServer::configPath( const QString &defaultConfigPath, const QString &
160160
return cfPath;
161161
}
162162

163+
void QgsServer::initLocale()
164+
{
165+
// System locale override
166+
if ( ! sSettings.overrideSystemLocale().isEmpty() )
167+
{
168+
QLocale::setDefault( QLocale( sSettings.overrideSystemLocale() ) );
169+
}
170+
// Number group separator settings
171+
QLocale currentLocale;
172+
if ( sSettings.showGroupSeparator() )
173+
{
174+
currentLocale.setNumberOptions( currentLocale.numberOptions() &= ~QLocale::NumberOption::OmitGroupSeparator );
175+
}
176+
else
177+
{
178+
currentLocale.setNumberOptions( currentLocale.numberOptions() |= QLocale::NumberOption::OmitGroupSeparator );
179+
}
180+
QLocale::setDefault( currentLocale );
181+
}
182+
163183
bool QgsServer::init()
164184
{
165185
if ( sInitialized )
@@ -195,6 +215,9 @@ bool QgsServer::init()
195215
QgsServerLogger::instance()->setLogStderr();
196216
}
197217

218+
// Configure locale
219+
initLocale();
220+
198221
// log settings currently used
199222
sSettings.logSummary();
200223

src/server/qgsserver.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,5 +148,8 @@ class SERVER_EXPORT QgsServer
148148

149149
//! cache
150150
QgsConfigCache *mConfigCache = nullptr;
151+
152+
//! Initialize locale
153+
static void initLocale();
151154
};
152155
#endif // QGSSERVER_H

src/server/qgsserversettings.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,29 @@ void QgsServerSettings::initSettings()
141141
QVariant()
142142
};
143143
mSettings[ sCacheSize.envVar ] = sCacheSize;
144+
145+
// system locale override
146+
const Setting sOverrideSystemLocale = { QgsServerSettingsEnv::QGIS_SERVER_OVERRIDE_SYSTEM_LOCALE,
147+
QgsServerSettingsEnv::DEFAULT_VALUE,
148+
QStringLiteral( "Override system locale" ),
149+
QStringLiteral( "/locale/userLocale" ),
150+
QVariant::String,
151+
QVariant( "" ),
152+
QVariant()
153+
};
154+
mSettings[ sOverrideSystemLocale.envVar ] = sOverrideSystemLocale;
155+
156+
// show group separator
157+
const Setting sShowGroupSeparator = { QgsServerSettingsEnv::QGIS_SERVER_SHOW_GROUP_SEPARATOR,
158+
QgsServerSettingsEnv::DEFAULT_VALUE,
159+
QStringLiteral( "Show group (thousands) separator" ),
160+
QStringLiteral( "/locale/showGroupSeparator" ),
161+
QVariant::String,
162+
QVariant( false ),
163+
QVariant()
164+
};
165+
mSettings[ sShowGroupSeparator.envVar ] = sShowGroupSeparator;
166+
144167
}
145168

146169
void QgsServerSettings::load()
@@ -321,3 +344,14 @@ QString QgsServerSettings::cacheDirectory() const
321344
{
322345
return value( QgsServerSettingsEnv::QGIS_SERVER_CACHE_DIRECTORY ).toString();
323346
}
347+
348+
QString QgsServerSettings::overrideSystemLocale() const
349+
{
350+
return value( QgsServerSettingsEnv::QGIS_SERVER_OVERRIDE_SYSTEM_LOCALE ).toString();
351+
}
352+
353+
bool QgsServerSettings::showGroupSeparator() const
354+
{
355+
return value( QgsServerSettingsEnv::QGIS_SERVER_SHOW_GROUP_SEPARATOR ).toBool();
356+
}
357+

src/server/qgsserversettings.h

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ class SERVER_EXPORT QgsServerSettingsEnv : public QObject
6060
QGIS_PROJECT_FILE,
6161
MAX_CACHE_LAYERS,
6262
QGIS_SERVER_CACHE_DIRECTORY,
63-
QGIS_SERVER_CACHE_SIZE
63+
QGIS_SERVER_CACHE_SIZE,
64+
QGIS_SERVER_SHOW_GROUP_SEPARATOR, //! Show group (thousands) separator when formatting numeric values, defaults to FALSE (since QGIS 3.8)
65+
QGIS_SERVER_OVERRIDE_SYSTEM_LOCALE, //! Override system locale (since QGIS 3.8)
6466
};
6567
Q_ENUM( EnvVar )
6668
};
@@ -88,41 +90,41 @@ class SERVER_EXPORT QgsServerSettings
8890

8991
/**
9092
* Constructor.
91-
*/
93+
*/
9294
QgsServerSettings();
9395

9496
/**
9597
* Load settings according to current environment variables.
96-
*/
98+
*/
9799
void load();
98100

99101
/**
100102
* Load setting for a specific environment variable name.
101-
* \returns true if loading is successful, false in case of an invalid name.
102-
*/
103+
* \returns TRUE if loading is successful, FALSE in case of an invalid name.
104+
*/
103105
bool load( const QString &envVarName );
104106

105107
/**
106108
* Log a summary of settings currently loaded.
107-
*/
109+
*/
108110
void logSummary() const;
109111

110112
/**
111113
* Returns the ini file loaded by QSetting.
112-
* \returns the path of the ini file or an empty string if none is loaded.
113-
*/
114+
* \returns the path of the ini file or an empty string if none is loaded.
115+
*/
114116
QString iniFile() const;
115117

116118
/**
117119
* Returns parallel rendering setting.
118-
* \returns true if parallel rendering is activated, false otherwise.
119-
*/
120+
* \returns TRUE if parallel rendering is activated, FALSE otherwise.
121+
*/
120122
bool parallelRendering() const;
121123

122124
/**
123125
* Returns the maximum number of threads to use.
124-
* \returns the number of threads.
125-
*/
126+
* \returns the number of threads.
127+
*/
126128
int maxThreads() const;
127129

128130
/**
@@ -133,20 +135,20 @@ class SERVER_EXPORT QgsServerSettings
133135

134136
/**
135137
* Returns the log level.
136-
* \returns the log level.
137-
*/
138+
* \returns the log level.
139+
*/
138140
Qgis::MessageLevel logLevel() const;
139141

140142
/**
141143
* Returns the QGS project file to use.
142-
* \returns the path of the QGS project or an empty string if none is defined.
143-
*/
144+
* \returns the path of the QGS project or an empty string if none is defined.
145+
*/
144146
QString projectFile() const;
145147

146148
/**
147149
* Returns the log file.
148-
* \returns the path of the log file or an empty string if none is defined.
149-
*/
150+
* \returns the path of the log file or an empty string if none is defined.
151+
*/
150152
QString logFile() const;
151153

152154
/**
@@ -158,16 +160,30 @@ class SERVER_EXPORT QgsServerSettings
158160

159161
/**
160162
* Returns the cache size.
161-
* \returns the cache size.
162-
*/
163+
* \returns the cache size.
164+
*/
163165
qint64 cacheSize() const;
164166

165167
/**
166168
* Returns the cache directory.
167-
* \returns the directory.
168-
*/
169+
* \returns the directory.
170+
*/
169171
QString cacheDirectory() const;
170172

173+
/**
174+
* Overrides system locale
175+
* \returns the optional override for system locale.
176+
* \since QGIS 3.8
177+
*/
178+
QString overrideSystemLocale() const;
179+
180+
/**
181+
* Show group (thousand) separator
182+
* \returns if group separator must be shown, default to FALSE.
183+
* \since QGIS 3.8
184+
*/
185+
bool showGroupSeparator() const;
186+
171187
private:
172188
void initSettings();
173189
QVariant value( QgsServerSettingsEnv::EnvVar envVar ) const;

tests/src/python/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ IF (WITH_SERVER)
283283
ADD_PYTHON_TEST(PyQgsServerWMTS test_qgsserver_wmts.py)
284284
ADD_PYTHON_TEST(PyQgsServerWFS test_qgsserver_wfs.py)
285285
ADD_PYTHON_TEST(PyQgsServerWFST test_qgsserver_wfst.py)
286+
ADD_PYTHON_TEST(PyQgsServerLocaleOverride test_qgsserver_locale_override.py)
286287
ADD_PYTHON_TEST(PyQgsOfflineEditingWFS test_offline_editing_wfs.py)
287288
ADD_PYTHON_TEST(PyQgsAuthManagerPasswordOWSTest test_authmanager_password_ows.py)
288289
ADD_PYTHON_TEST(PyQgsAuthManagerPKIOWSTest test_authmanager_pki_ows.py)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# -*- coding: utf-8 -*-
2+
"""QGIS Unit tests for QgsServer Locale Override Options.
3+
4+
From build dir, run: ctest -R PyQgsServerLocaleOverride -V
5+
6+
.. note:: This test needs env vars to be set before the server is
7+
configured for the first time, for this
8+
reason it cannot run as a test case of another server
9+
test.
10+
11+
.. note:: This program is free software; you can redistribute it and/or modify
12+
it under the terms of the GNU General Public License as published by
13+
the Free Software Foundation; either version 2 of the License, or
14+
(at your option) any later version.
15+
16+
"""
17+
__author__ = 'Alessandro Pasotti'
18+
__date__ = '01/04/2019'
19+
__copyright__ = 'Copyright 2019, The QGIS Project'
20+
# This will get replaced with a git SHA1 when you do a git archive
21+
__revision__ = '$Format:%H$'
22+
23+
import os
24+
25+
# Needed on Qt 5 so that the serialization of XML is consistent among all
26+
# executions
27+
os.environ['QT_HASH_SEED'] = '1'
28+
29+
from utilities import (
30+
unitTestDataPath,
31+
)
32+
from qgis.testing import unittest
33+
34+
from test_qgsserver_wms import TestQgsServerWMSTestBase
35+
from qgis.core import QgsProject, QgsFontUtils
36+
from qgis.server import QgsServer
37+
38+
39+
class TestQgsServerWMSLocaleOverride(TestQgsServerWMSTestBase):
40+
"""QGIS Server WMS Tests for GetFeatureInfo request"""
41+
42+
# Set to True to re-generate reference files for this class
43+
regenerate_reference = False
44+
45+
def setUp(self):
46+
"""Create the server instance"""
47+
self.fontFamily = QgsFontUtils.standardTestFontFamily()
48+
QgsFontUtils.loadStandardTestFonts(['All'])
49+
50+
self.testdata_path = unitTestDataPath('qgis_server') + '/'
51+
52+
d = unitTestDataPath('qgis_server') + '/'
53+
self.projectPath = os.path.join(d, "project.qgs")
54+
55+
# Clean env just to be sure
56+
env_vars = ['QUERY_STRING', 'QGIS_PROJECT_FILE']
57+
for ev in env_vars:
58+
try:
59+
del os.environ[ev]
60+
except KeyError:
61+
pass
62+
63+
os.environ['QGIS_SERVER_OVERRIDE_SYSTEM_LOCALE'] = 'EN_us'
64+
os.environ['QGIS_SERVER_SHOW_GROUP_SEPARATOR'] = '0'
65+
66+
self.server = QgsServer()
67+
68+
def testGetFeatureInfoThousandSeparator(self):
69+
70+
self.wms_request_compare('GetFeatureInfo',
71+
'&layers=testlayer_thousands&styles=&' +
72+
'info_format=text%2Fxml&transparent=true&' +
73+
'width=600&height=400&srs=EPSG%3A3857&bbox=913190.6389747962%2C' +
74+
'5606005.488876367%2C913235.426296057%2C5606035.347090538&' +
75+
'query_layers=testlayer_thousands&X=190&Y=320',
76+
'wms_getfeatureinfo-thousands-text-xml',
77+
project='test_project_gfi_thousands.qgs',)
78+
79+
80+
if __name__ == '__main__':
81+
unittest.main()

0 commit comments

Comments
 (0)