Skip to content

Commit

Permalink
[server] Allow locale and group separator override
Browse files Browse the repository at this point in the history
cherry-picked from master 5b586e8
  • Loading branch information
elpaso committed Apr 2, 2019
1 parent 32081b6 commit 00f49a0
Show file tree
Hide file tree
Showing 12 changed files with 857 additions and 24 deletions.
22 changes: 20 additions & 2 deletions python/server/auto_generated/qgsserversettings.sip.in
Expand Up @@ -39,7 +39,7 @@ Load settings according to current environment variables.
%Docstring %Docstring
Load setting for a specific environment variable name. Load setting for a specific environment variable name.


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


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


:return: true if parallel rendering is activated, false otherwise. :return: TRUE if parallel rendering is activated, FALSE otherwise.
%End %End


int maxThreads() const; int maxThreads() const;
Expand Down Expand Up @@ -117,6 +117,24 @@ Returns the cache size.
Returns the cache directory. Returns the cache directory.


:return: the directory. :return: the directory.
%End

QString overrideSystemLocale() const;
%Docstring
Overrides system locale

:return: the optional override for system locale.

.. versionadded:: 3.8
%End

bool showGroupSeparator() const;
%Docstring
Show group (thousand) separator

:return: if group separator must be shown, default to FALSE.

.. versionadded:: 3.8
%End %End


}; };
Expand Down
23 changes: 23 additions & 0 deletions src/server/qgsserver.cpp
Expand Up @@ -160,6 +160,26 @@ QString QgsServer::configPath( const QString &defaultConfigPath, const QString &
return cfPath; return cfPath;
} }


void QgsServer::initLocale()
{
// System locale override
if ( ! sSettings.overrideSystemLocale().isEmpty() )
{
QLocale::setDefault( QLocale( sSettings.overrideSystemLocale() ) );
}
// Number group separator settings
QLocale currentLocale;
if ( sSettings.showGroupSeparator() )
{
currentLocale.setNumberOptions( currentLocale.numberOptions() &= ~QLocale::NumberOption::OmitGroupSeparator );
}
else
{
currentLocale.setNumberOptions( currentLocale.numberOptions() |= QLocale::NumberOption::OmitGroupSeparator );
}
QLocale::setDefault( currentLocale );
}

bool QgsServer::init() bool QgsServer::init()
{ {
if ( sInitialized ) if ( sInitialized )
Expand Down Expand Up @@ -195,6 +215,9 @@ bool QgsServer::init()
QgsServerLogger::instance()->setLogStderr(); QgsServerLogger::instance()->setLogStderr();
} }


// Configure locale
initLocale();

// log settings currently used // log settings currently used
sSettings.logSummary(); sSettings.logSummary();


Expand Down
3 changes: 3 additions & 0 deletions src/server/qgsserver.h
Expand Up @@ -148,5 +148,8 @@ class SERVER_EXPORT QgsServer


//! cache //! cache
QgsConfigCache *mConfigCache = nullptr; QgsConfigCache *mConfigCache = nullptr;

//! Initialize locale
static void initLocale();
}; };
#endif // QGSSERVER_H #endif // QGSSERVER_H
34 changes: 34 additions & 0 deletions src/server/qgsserversettings.cpp
Expand Up @@ -141,6 +141,29 @@ void QgsServerSettings::initSettings()
QVariant() QVariant()
}; };
mSettings[ sCacheSize.envVar ] = sCacheSize; mSettings[ sCacheSize.envVar ] = sCacheSize;

// system locale override
const Setting sOverrideSystemLocale = { QgsServerSettingsEnv::QGIS_SERVER_OVERRIDE_SYSTEM_LOCALE,
QgsServerSettingsEnv::DEFAULT_VALUE,
QStringLiteral( "Override system locale" ),
QStringLiteral( "/locale/userLocale" ),
QVariant::String,
QVariant( "" ),
QVariant()
};
mSettings[ sOverrideSystemLocale.envVar ] = sOverrideSystemLocale;

// show group separator
const Setting sShowGroupSeparator = { QgsServerSettingsEnv::QGIS_SERVER_SHOW_GROUP_SEPARATOR,
QgsServerSettingsEnv::DEFAULT_VALUE,
QStringLiteral( "Show group (thousands) separator" ),
QStringLiteral( "/locale/showGroupSeparator" ),
QVariant::String,
QVariant( false ),
QVariant()
};
mSettings[ sShowGroupSeparator.envVar ] = sShowGroupSeparator;

} }


void QgsServerSettings::load() void QgsServerSettings::load()
Expand Down Expand Up @@ -321,3 +344,14 @@ QString QgsServerSettings::cacheDirectory() const
{ {
return value( QgsServerSettingsEnv::QGIS_SERVER_CACHE_DIRECTORY ).toString(); return value( QgsServerSettingsEnv::QGIS_SERVER_CACHE_DIRECTORY ).toString();
} }

QString QgsServerSettings::overrideSystemLocale() const
{
return value( QgsServerSettingsEnv::QGIS_SERVER_OVERRIDE_SYSTEM_LOCALE ).toString();
}

bool QgsServerSettings::showGroupSeparator() const
{
return value( QgsServerSettingsEnv::QGIS_SERVER_SHOW_GROUP_SEPARATOR ).toBool();
}

60 changes: 38 additions & 22 deletions src/server/qgsserversettings.h
Expand Up @@ -60,7 +60,9 @@ class SERVER_EXPORT QgsServerSettingsEnv : public QObject
QGIS_PROJECT_FILE, QGIS_PROJECT_FILE,
MAX_CACHE_LAYERS, MAX_CACHE_LAYERS,
QGIS_SERVER_CACHE_DIRECTORY, QGIS_SERVER_CACHE_DIRECTORY,
QGIS_SERVER_CACHE_SIZE QGIS_SERVER_CACHE_SIZE,
QGIS_SERVER_SHOW_GROUP_SEPARATOR, //! Show group (thousands) separator when formatting numeric values, defaults to FALSE (since QGIS 3.8)
QGIS_SERVER_OVERRIDE_SYSTEM_LOCALE, //! Override system locale (since QGIS 3.8)
}; };
Q_ENUM( EnvVar ) Q_ENUM( EnvVar )
}; };
Expand Down Expand Up @@ -88,41 +90,41 @@ class SERVER_EXPORT QgsServerSettings


/** /**
* Constructor. * Constructor.
*/ */
QgsServerSettings(); QgsServerSettings();


/** /**
* Load settings according to current environment variables. * Load settings according to current environment variables.
*/ */
void load(); void load();


/** /**
* Load setting for a specific environment variable name. * Load setting for a specific environment variable name.
* \returns true if loading is successful, false in case of an invalid name. * \returns TRUE if loading is successful, FALSE in case of an invalid name.
*/ */
bool load( const QString &envVarName ); bool load( const QString &envVarName );


/** /**
* Log a summary of settings currently loaded. * Log a summary of settings currently loaded.
*/ */
void logSummary() const; void logSummary() const;


/** /**
* Returns the ini file loaded by QSetting. * Returns the ini file loaded by QSetting.
* \returns the path of the ini file or an empty string if none is loaded. * \returns the path of the ini file or an empty string if none is loaded.
*/ */
QString iniFile() const; QString iniFile() const;


/** /**
* Returns parallel rendering setting. * Returns parallel rendering setting.
* \returns true if parallel rendering is activated, false otherwise. * \returns TRUE if parallel rendering is activated, FALSE otherwise.
*/ */
bool parallelRendering() const; bool parallelRendering() const;


/** /**
* Returns the maximum number of threads to use. * Returns the maximum number of threads to use.
* \returns the number of threads. * \returns the number of threads.
*/ */
int maxThreads() const; int maxThreads() const;


/** /**
Expand All @@ -133,20 +135,20 @@ class SERVER_EXPORT QgsServerSettings


/** /**
* Returns the log level. * Returns the log level.
* \returns the log level. * \returns the log level.
*/ */
Qgis::MessageLevel logLevel() const; Qgis::MessageLevel logLevel() const;


/** /**
* Returns the QGS project file to use. * Returns the QGS project file to use.
* \returns the path of the QGS project or an empty string if none is defined. * \returns the path of the QGS project or an empty string if none is defined.
*/ */
QString projectFile() const; QString projectFile() const;


/** /**
* Returns the log file. * Returns the log file.
* \returns the path of the log file or an empty string if none is defined. * \returns the path of the log file or an empty string if none is defined.
*/ */
QString logFile() const; QString logFile() const;


/** /**
Expand All @@ -158,16 +160,30 @@ class SERVER_EXPORT QgsServerSettings


/** /**
* Returns the cache size. * Returns the cache size.
* \returns the cache size. * \returns the cache size.
*/ */
qint64 cacheSize() const; qint64 cacheSize() const;


/** /**
* Returns the cache directory. * Returns the cache directory.
* \returns the directory. * \returns the directory.
*/ */
QString cacheDirectory() const; QString cacheDirectory() const;


/**
* Overrides system locale
* \returns the optional override for system locale.
* \since QGIS 3.8
*/
QString overrideSystemLocale() const;

/**
* Show group (thousand) separator
* \returns if group separator must be shown, default to FALSE.
* \since QGIS 3.8
*/
bool showGroupSeparator() const;

private: private:
void initSettings(); void initSettings();
QVariant value( QgsServerSettingsEnv::EnvVar envVar ) const; QVariant value( QgsServerSettingsEnv::EnvVar envVar ) const;
Expand Down
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -283,6 +283,7 @@ IF (WITH_SERVER)
ADD_PYTHON_TEST(PyQgsServerWMTS test_qgsserver_wmts.py) ADD_PYTHON_TEST(PyQgsServerWMTS test_qgsserver_wmts.py)
ADD_PYTHON_TEST(PyQgsServerWFS test_qgsserver_wfs.py) ADD_PYTHON_TEST(PyQgsServerWFS test_qgsserver_wfs.py)
ADD_PYTHON_TEST(PyQgsServerWFST test_qgsserver_wfst.py) ADD_PYTHON_TEST(PyQgsServerWFST test_qgsserver_wfst.py)
ADD_PYTHON_TEST(PyQgsServerLocaleOverride test_qgsserver_locale_override.py)
ADD_PYTHON_TEST(PyQgsOfflineEditingWFS test_offline_editing_wfs.py) ADD_PYTHON_TEST(PyQgsOfflineEditingWFS test_offline_editing_wfs.py)
ADD_PYTHON_TEST(PyQgsAuthManagerPasswordOWSTest test_authmanager_password_ows.py) ADD_PYTHON_TEST(PyQgsAuthManagerPasswordOWSTest test_authmanager_password_ows.py)
ADD_PYTHON_TEST(PyQgsAuthManagerPKIOWSTest test_authmanager_pki_ows.py) ADD_PYTHON_TEST(PyQgsAuthManagerPKIOWSTest test_authmanager_pki_ows.py)
Expand Down
81 changes: 81 additions & 0 deletions tests/src/python/test_qgsserver_locale_override.py
@@ -0,0 +1,81 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsServer Locale Override Options.
From build dir, run: ctest -R PyQgsServerLocaleOverride -V
.. note:: This test needs env vars to be set before the server is
configured for the first time, for this
reason it cannot run as a test case of another server
test.
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
__author__ = 'Alessandro Pasotti'
__date__ = '01/04/2019'
__copyright__ = 'Copyright 2019, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import os

# Needed on Qt 5 so that the serialization of XML is consistent among all
# executions
os.environ['QT_HASH_SEED'] = '1'

from utilities import (
unitTestDataPath,
)
from qgis.testing import unittest

from test_qgsserver_wms import TestQgsServerWMSTestBase
from qgis.core import QgsProject, QgsFontUtils
from qgis.server import QgsServer


class TestQgsServerWMSLocaleOverride(TestQgsServerWMSTestBase):
"""QGIS Server WMS Tests for GetFeatureInfo request"""

# Set to True to re-generate reference files for this class
regenerate_reference = False

def setUp(self):
"""Create the server instance"""
self.fontFamily = QgsFontUtils.standardTestFontFamily()
QgsFontUtils.loadStandardTestFonts(['All'])

self.testdata_path = unitTestDataPath('qgis_server') + '/'

d = unitTestDataPath('qgis_server') + '/'
self.projectPath = os.path.join(d, "project.qgs")

# Clean env just to be sure
env_vars = ['QUERY_STRING', 'QGIS_PROJECT_FILE']
for ev in env_vars:
try:
del os.environ[ev]
except KeyError:
pass

os.environ['QGIS_SERVER_OVERRIDE_SYSTEM_LOCALE'] = 'EN_us'
os.environ['QGIS_SERVER_SHOW_GROUP_SEPARATOR'] = '0'

self.server = QgsServer()

def testGetFeatureInfoThousandSeparator(self):

self.wms_request_compare('GetFeatureInfo',
'&layers=testlayer_thousands&styles=&' +
'info_format=text%2Fxml&transparent=true&' +
'width=600&height=400&srs=EPSG%3A3857&bbox=913190.6389747962%2C' +
'5606005.488876367%2C913235.426296057%2C5606035.347090538&' +
'query_layers=testlayer_thousands&X=190&Y=320',
'wms_getfeatureinfo-thousands-text-xml',
project='test_project_gfi_thousands.qgs',)


if __name__ == '__main__':
unittest.main()

0 comments on commit 00f49a0

Please sign in to comment.