Skip to content
Permalink
Browse files

Set config path before calling plugin method

  • Loading branch information
dmarteau committed Apr 11, 2019
1 parent 3c6299a commit 7ce5fb2d023034fd8b370d8c07c346401070809d
Showing with 67 additions and 9 deletions.
  1. +7 −0 src/server/qgsrequesthandler.cpp
  2. +15 −9 src/server/qgsserver.cpp
  3. +45 −0 tests/src/python/test_qgsserver_plugins.py
@@ -259,6 +259,13 @@ void QgsRequestHandler::setParameter( const QString &key, const QString &value )
{
if ( !( key.isEmpty() || value.isEmpty() ) )
{
// Warn for potential breaking change if plugin set the MAP parameter
// expecting changing the config file path, see PR #9773
if ( key.compare( QLatin1String( "MAP" ), Qt::CaseInsensitive ) == 0 )
{
QgsMessageLog::logMessage( QStringLiteral( "Changing the 'MAP' parameter will have no effect on config path: use QgsSerververInterface::setConfigFilePath instead" ),
"Server", Qgis::Warning );
}
mRequest.setParameter( key, value );
}
}
@@ -319,6 +319,20 @@ void QgsServer::handleRequest( QgsServerRequest &request, QgsServerResponse &res
// Set the request handler into the interface for plugins to manipulate it
sServerInterface->setRequestHandler( &requestHandler );

// Initialize configfilepath so that is is available
// before calling plugin methods
// Note that plugins may still change that value using
// setConfigFilePath() interface method
if ( ! project )
{
QString configFilePath = configPath( *sConfigFilePath, request.serverParameters().map() );
sServerInterface->setConfigFilePath( configFilePath );
}
else
{
sServerInterface->setConfigFilePath( project->fileName() );
}

// Call requestReady() method (if enabled)
responseDecorator.start();

@@ -333,20 +347,12 @@ void QgsServer::handleRequest( QgsServerRequest &request, QgsServerResponse &res
//Config file path
if ( ! project )
{
QString configFilePath = configPath( *sConfigFilePath, params.map() );

// load the project if needed and not empty
project = mConfigCache->project( configFilePath );
project = mConfigCache->project( sServerInterface->configFilePath() );
if ( ! project )
{
throw QgsServerException( QStringLiteral( "Project file error" ) );
}

sServerInterface->setConfigFilePath( configFilePath );
}
else
{
sServerInterface->setConfigFilePath( project->fileName() );
}

if ( ! params.fileName().isEmpty() )
@@ -177,6 +177,51 @@ def responseComplete(self):
self.assertEqual(response, expected)
self.assertEqual(headers2, {'Content-type': 'text/plain'})

def test_configpath(self):
""" Test plugin can read confif path
"""
try:
from qgis.server import QgsServerFilter
from qgis.core import QgsProject
except ImportError:
print("QGIS Server plugins are not compiled. Skipping test")
return

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

# global to be modified inside plugin filters
globals()['configFilePath2'] = None

class Filter0(QgsServerFilter):
"""Body setter, clear body, keep headers"""

def requestReady(self):
global configFilePath2
configFilePath2 = self.serverInterface().configFilePath()

serverIface = self.server.serverInterface()
serverIface.registerFilter(Filter0(serverIface), 100)

# Test using MAP
self._execute_request('?service=simple&MAP=%s' % self.projectPath)

# Check config file path
self.assertEqual(configFilePath2, self.projectPath)

# Reset result
globals()['configFilePath2'] = None

# Test with prqject as argument
project = QgsProject()
project.read(self.projectPath)

self._execute_request_project('?service=simple', project=project)

# Check config file path
self.assertEqual(configFilePath2, project.fileName())


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

0 comments on commit 7ce5fb2

Please sign in to comment.
You can’t perform that action at this time.