Afaik, there is no way to access the project in the `onRequestReady()` because you may change or set the targeted project  at this point before passing the request
to subsequent filters.

One possibility to get around this is to use the filter to route your request to a custom service implementation where it will be have access the project as an input parameter of the `executeRequest` method.

You may have a look to the  The AtlasPrint plugin (https://github.com/3liz/qgis-atlasprint) use that use this method for his custom `getatlasprint` implementation.

Your custom service, may then call  the native `getprint` request directly with the WMS  service handler (https://qgis.org/pyqgis/3.42/server/QgsService.html) and the service registry (https://qgis.org/pyqgis/3.42/server/QgsServiceRegistry.html)

Imho, you shouldn't rely on the QgsProject::instance() when implementing plugin filters, which may lead to undefined behaviors in some cases. If you need to acces the project, prefer a custom service implementation where you get the project as an input parameter of the `executeRequest` method.

David Marteau

Le 31/03/2025 à 22:05, Ricardo Filipe Soares Garcia da via QGIS-Developer a écrit :
Hi all

I am implementing a custom plugin for QGIS Server that uses a QgsServerFilter subclass.

The purpose of this plugin is to use QGIS Server to respond to `GetPrint` requests. However, I need to pass some runtime parameters to my map layout before handing it over to `GetPrint`.

I think the place where I should do these preparatory steps, which ultimately include loading some layers and setting some layout variables, is in my `onRequestReady()` method.

The problem: I need to be able to set properties on the relevant QgsPrintLayout instance, but in order to do that I would need to retrieve it from the current QgsProject instance, and I can't figure out how to get a hold of it.

Is it possible to get the current instance of QgsProject in the context of QGIS server?

At first I tried to do it as I would on a plugin for the desktop:

```
qgis_project = QgsProject.instance()
layout_manager = qgis_project.layoutManager()
my_layout = layout_manager.getLayoutByName("the_name")
# this fails, my_layout is None
```

This approach does not work, as the resulting qgis_project is not the one I am using in my request.

Then I searched through the properties of `QgsServerInterface`, hoping to find some sort of `getCurrentProject()` method - but this does not seem to exist.

I also tried loading the project from disk in my plugin with

```
current_project = QgsProject.instance()
current_project.read(server_iface..configFilePath())
```

This second approach does allow me to load up the project, but any changes I set at runtime are not propagated to subsequent handlers in the QGIS Server workflow, meaning that the `GetPrint` handler does not see my changes to the project.

I don't want to save the changes, just set them at runtime and use them. This is because other (potentially parallel) requests will also need to modify the aforementioned layout in order to customize their printing needs.

Is it at all possible to do what I am trying to do? Or is there maybe a more proper way?

Thanks in advance

--
___________________________ ___ __
Ricardo Garcia Silva

_______________________________________________
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info:https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe:https://lists.osgeo.org/mailman/listinfo/qgis-developer
_______________________________________________
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
  • [QGIS-Developer] QGIS S... Ricardo Filipe Soares Garcia da via QGIS-Developer
    • Re: [QGIS-Develope... David Marteau via QGIS-Developer

Reply via email to