<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>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<br>
to subsequent filters. <br>
</p>
<p>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. </p>
<p>You may have a look to the The AtlasPrint plugin
(<a class="moz-txt-link-freetext" href="https://github.com/3liz/qgis-atlasprint">https://github.com/3liz/qgis-atlasprint</a>) use that use this method
for his custom `getatlasprint` implementation. </p>
<p>Your custom service, may then call the native `getprint` request
directly with the WMS service handler
(<a class="moz-txt-link-freetext" href="https://qgis.org/pyqgis/3.42/server/QgsService.html">https://qgis.org/pyqgis/3.42/server/QgsService.html</a>) and the
service registry
(<a class="moz-txt-link-freetext" href="https://qgis.org/pyqgis/3.42/server/QgsServiceRegistry.html">https://qgis.org/pyqgis/3.42/server/QgsServiceRegistry.html</a>)<br>
</p>
<p>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<br>
project, prefer a custom service implementation where you get the
project as an input parameter of the `executeRequest` method.<br>
</p>
<p>David Marteau</p>
<div class="moz-cite-prefix">Le 31/03/2025 à 22:05, Ricardo Filipe
Soares Garcia da via QGIS-Developer a écrit :<br>
</div>
<blockquote type="cite"
cite="mid:CAFcNcQbRJzknCC2eWXfXwiiSTAGRxF4_zZ+yA6gZyGKJUm_tbg@mail.gmail.com">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<div dir="ltr">
<div>Hi all</div>
<div><br>
</div>
<div>I am implementing a custom plugin for QGIS Server that uses
a QgsServerFilter subclass.</div>
<div><br>
</div>
<div>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`.</div>
<div><br>
</div>
<div>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.</div>
<div><br>
</div>
<div>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.</div>
<div><br>
</div>
<div>Is it possible to get the current instance of QgsProject in
the context of QGIS server?</div>
<div><br>
</div>
<div>At first I tried to do it as I would on a plugin for the
desktop:</div>
<div><br>
</div>
<div>```</div>
<div>qgis_project = QgsProject.instance()</div>
<div>layout_manager = qgis_project.layoutManager()</div>
<div>my_layout = layout_manager.getLayoutByName("the_name")</div>
<div># this fails, my_layout is None</div>
<div>```</div>
<div><br>
</div>
<div>This approach does not work, as the resulting qgis_project
is not the one I am using in my request.</div>
<div><br>
</div>
<div>Then I searched through the properties of
`QgsServerInterface`, hoping to find some sort of
`getCurrentProject()` method - but this does not seem to
exist.</div>
<div><br>
</div>
<div>I also tried loading the project from disk in my plugin
with</div>
<div><br>
</div>
<div>```</div>
<div>current_project = QgsProject.instance()</div>
<div>current_project.read(server_iface..configFilePath())</div>
<div>```</div>
<div><br>
</div>
<div>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.</div>
<div><br>
</div>
<div>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.</div>
<div><br>
</div>
<div>Is it at all possible to do what I am trying to do? Or is
there maybe a more proper way?</div>
<div><br>
</div>
<div>Thanks in advance</div>
<br>
<span class="gmail_signature_prefix">-- </span><br>
<div dir="ltr" class="gmail_signature"
data-smartmail="gmail_signature">___________________________
___ __<br>
Ricardo Garcia Silva</div>
</div>
<br>
<fieldset class="moz-mime-attachment-header"></fieldset>
<pre wrap="" class="moz-quote-pre">_______________________________________________
QGIS-Developer mailing list
<a class="moz-txt-link-abbreviated" href="mailto:QGIS-Developer@lists.osgeo.org">QGIS-Developer@lists.osgeo.org</a>
List info: <a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/qgis-developer">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a>
Unsubscribe: <a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/qgis-developer">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a>
</pre>
</blockquote>
</body>
</html>