From 44b4e5fbff765bc8af779649c712fae63af46e23 Mon Sep 17 00:00:00 2001 From: Etienne Trimaille Date: Thu, 31 Oct 2024 16:50:38 +0100 Subject: [PATCH] Remove the use of deprecated methods from the QGIS API --- lizmap_server/core.py | 5 ++++- lizmap_server/expression_service.py | 9 +++++++-- lizmap_server/get_feature_info.py | 7 ++++++- lizmap_server/legend_onoff_filter.py | 19 +++++++++++++++---- test/test_server_core.py | 17 +++++++++++++---- 5 files changed, 45 insertions(+), 12 deletions(-) diff --git a/lizmap_server/core.py b/lizmap_server/core.py index 45b7a7b1..67a04d34 100755 --- a/lizmap_server/core.py +++ b/lizmap_server/core.py @@ -60,7 +60,10 @@ def find_layer(layer_name: str, project: QgsProject) -> Optional[QgsMapLayer]: break # check short name - if layer.shortName() == layer_name: + if Qgis.versionInt() < 33800 and layer.shortName() == layer_name: + found = layer + break + elif Qgis.versionInt() >= 33800 and layer.serverProperties().shortName() == layer_name: found = layer break diff --git a/lizmap_server/expression_service.py b/lizmap_server/expression_service.py index 951a442b..ed84b5c3 100755 --- a/lizmap_server/expression_service.py +++ b/lizmap_server/expression_service.py @@ -8,6 +8,7 @@ from typing import Dict from qgis.core import ( + Qgis, QgsDistanceArea, QgsExpression, QgsExpressionContext, @@ -20,7 +21,7 @@ QgsJsonUtils, QgsProject, ) -from qgis.PyQt.QtCore import QTextCodec, QVariant +from qgis.PyQt.QtCore import QMetaType, QTextCodec, QVariant from qgis.server import ( QgsRequestHandler, QgsServerRequest, @@ -494,7 +495,11 @@ def replace_expression_text( exporter.setSourceCrs(layer.crs()) geojson_fields = QgsFields() for k in str_map.keys(): - geojson_fields.append(QgsField(str(k), QVariant.String)) + if Qgis.versionInt() < 33800: + field = QgsField(str(k), QVariant.String) + else: + field = QgsField(str(k), QMetaType.QString) + geojson_fields.append(field) else: exporter = None geojson_fields = None diff --git a/lizmap_server/get_feature_info.py b/lizmap_server/get_feature_info.py index b0d68f94..3e534f4c 100755 --- a/lizmap_server/get_feature_info.py +++ b/lizmap_server/get_feature_info.py @@ -11,6 +11,7 @@ from typing import Generator, List, Tuple, Union from qgis.core import ( + Qgis, QgsDistanceArea, QgsEditFormConfig, QgsExpression, @@ -258,7 +259,11 @@ def responseComplete(self): ) continue - layer_name = result.layer.shortName() + if Qgis.versionInt() < 33800: + layer_name = result.layer.shortName() + else: + layer_name = result.layer.serverProperties().shortName() + if not layer_name: layer_name = result.layer.name() logger.info( diff --git a/lizmap_server/legend_onoff_filter.py b/lizmap_server/legend_onoff_filter.py index 5619d7ef..dfe05e45 100644 --- a/lizmap_server/legend_onoff_filter.py +++ b/lizmap_server/legend_onoff_filter.py @@ -5,7 +5,7 @@ # File adapted by @rldhont, 3Liz -from qgis.core import QgsMapLayer, QgsProject +from qgis.core import Qgis, QgsMapLayer, QgsProject from qgis.server import ( QgsAccessControlFilter, QgsServerFilter, @@ -25,13 +25,19 @@ def __init__(self, server_interface: QgsServerInterface): @staticmethod def _setup_legend(layer: QgsMapLayer, qs: str, onoff: bool): + + if Qgis.versionInt() < 33800: + layer_short_name = layer.shortName() + else: + layer_short_name = layer.serverProperties().shortName() + for legend_layer in qs.split(';'): layer_name, key_list = legend_layer.split(':') # not empty if layer_name == '' or key_list == '': continue # for the layer - if layer_name not in (layer.shortName(), layer.name(), layer.id()): + if layer_name not in (layer_short_name, layer.name(), layer.id()): continue for key in key_list.split(','): @@ -66,8 +72,13 @@ def layerPermissions(self, layer: QgsMapLayer) -> QgsAccessControlFilter.LayerPe style = sm.currentStyle() # check short name - if layer.shortName() in style_map: - style = style_map[layer.shortName()] + if Qgis.versionInt() < 33800: + layer_short_name = layer.shortName() + else: + layer_short_name = layer.serverProperties().shortName() + if layer_short_name in style_map: + style = style_map[layer_short_name] + # check layer name elif layer.name() in style_map: style = style_map[layer.name()] diff --git a/test/test_server_core.py b/test/test_server_core.py index 16a58547..2d31f164 100755 --- a/test/test_server_core.py +++ b/test/test_server_core.py @@ -4,8 +4,8 @@ import unittest import xml.etree.ElementTree as ET -from qgis.core import QgsField, QgsFields -from qgis.PyQt.QtCore import QVariant +from qgis.core import Qgis, QgsField, QgsFields +from qgis.PyQt.QtCore import QMetaType, QVariant from lizmap_server.core import ( _server_feature_id_expression, @@ -242,8 +242,17 @@ def test_edit_xml_get_feature_info_with_maptip(self): def test_feature_id_expression(self): """ Test the QgsServerFeatureId port from CPP into Python. """ fields = QgsFields() - fields.append(QgsField('field_1', type=QVariant.Double)) - fields.append(QgsField('field_2', type=QVariant.Double)) + if Qgis.versionInt() < 33800: + field = QgsField('field_1', QVariant.Double) + else: + field = QgsField('field_1', QMetaType.Double) + fields.append(field) + + if Qgis.versionInt() < 33800: + field = QgsField('field_2', QVariant.Double) + else: + field = QgsField('field_2', QMetaType.Double) + fields.append(field) self.assertEqual( "",