Skip to content

Commit

Permalink
Remove the use of deprecated methods from the QGIS API
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry committed Oct 31, 2024
1 parent b08e749 commit 44b4e5f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 12 deletions.
5 changes: 4 additions & 1 deletion lizmap_server/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 7 additions & 2 deletions lizmap_server/expression_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import Dict

from qgis.core import (
Qgis,
QgsDistanceArea,
QgsExpression,
QgsExpressionContext,
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion lizmap_server/get_feature_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from typing import Generator, List, Tuple, Union

from qgis.core import (
Qgis,
QgsDistanceArea,
QgsEditFormConfig,
QgsExpression,
Expand Down Expand Up @@ -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(
Expand Down
19 changes: 15 additions & 4 deletions lizmap_server/legend_onoff_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(','):
Expand Down Expand Up @@ -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()]
Expand Down
17 changes: 13 additions & 4 deletions test/test_server_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
"",
Expand Down

0 comments on commit 44b4e5f

Please sign in to comment.