Skip to content

Commit

Permalink
Some Python minor fixes on the code
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry committed Jan 26, 2024
1 parent 285324d commit 5bcad3e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 23 deletions.
12 changes: 9 additions & 3 deletions lizmap_server/legend_onoff_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
# File adapted by @rldhont, 3Liz

from qgis.core import QgsMapLayer, QgsProject
from qgis.server import QgsServerFilter, QgsAccessControlFilter, QgsServerInterface
from qgis.server import (
QgsAccessControlFilter,
QgsServerFilter,
QgsServerInterface,
)

from lizmap_server.core import find_vector_layer
from lizmap_server.logger import Logger, exception_handler
Expand All @@ -19,7 +23,8 @@ def __init__(self, server_interface: QgsServerInterface):

self.iface = server_interface

def _setup_legend(self, layer, qs, onoff):
@staticmethod
def _setup_legend(layer: QgsMapLayer, qs: str, onoff: bool):
for legend_layer in qs.split(';'):
layer_name, key_list = legend_layer.split(':')
# not empty
Expand Down Expand Up @@ -91,7 +96,8 @@ class LegendOnOffFilter(QgsServerFilter):
def __init__(self, server_interface: QgsServerInterface):
super().__init__(server_interface)

def _reset_legend(self, qs: str, project: QgsProject):
@staticmethod
def _reset_legend(qs: str, project: QgsProject):
if not qs or ':' not in qs:
return

Expand Down
5 changes: 4 additions & 1 deletion lizmap_server/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
from lizmap_server.expression_service import ExpressionService
from lizmap_server.get_feature_info import GetFeatureInfoFilter
from lizmap_server.get_legend_graphic import GetLegendGraphicFilter
from lizmap_server.legend_onoff_filter import LegendOnOffAccessControl, LegendOnOffFilter
from lizmap_server.legend_onoff_filter import (
LegendOnOffAccessControl,
LegendOnOffFilter,
)
from lizmap_server.lizmap_accesscontrol import LizmapAccessControlFilter
from lizmap_server.lizmap_filter import LizmapFilter
from lizmap_server.lizmap_service import LizmapService
Expand Down
21 changes: 13 additions & 8 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def getplugin(self, name) -> Any:
def getprojectpath(self, name: str) -> str:
return self.datapath.join(name)

def getproject(self, name: str) -> QgsProject:
def get_project(self, name: str) -> QgsProject:
projectpath = self.getprojectpath(name)
if Qgis.QGIS_VERSION_INT >= 32601:
qgsproject = QgsProject(capabilities=Qgis.ProjectCapabilities())
Expand All @@ -160,24 +160,29 @@ def getproject(self, name: str) -> QgsProject:
raise ValueError("Error reading project '%s':" % projectpath.strpath)
return qgsproject

def get(self, query: str, project: str=None, headers: Dict[str, str]={}) -> OWSResponse:
def get(self, query: str, project: str = None, headers: Dict[str, str] = None) -> OWSResponse:
""" Return server response from query
"""
request = QgsBufferServerRequest(query, QgsServerRequest.GetMethod, headers, None)
if headers is None:
headers = {}
server_request = QgsBufferServerRequest(query, QgsServerRequest.GetMethod, headers, None)
response = QgsBufferServerResponse()
if project is not None and not os.path.isabs(project):
qgsproject = self.getproject(project)
qgsproject = self.get_project(project)
else:
qgsproject = None
self.server.handleRequest(request, response, project=qgsproject)
self.server.handleRequest(server_request, response, project=qgsproject)
return OWSResponse(response)

def getWithProject(self, query: str, project: QgsProject, headers: Dict[str, str]={}) -> OWSResponse:
def get_with_project(self, query: str, project: QgsProject, headers: Dict[str, str] = None) -> OWSResponse:
""" Return server response from query
"""
request = QgsBufferServerRequest(query, QgsServerRequest.GetMethod, headers, None)
if headers is None:
headers = {}

server_request = QgsBufferServerRequest(query, QgsServerRequest.GetMethod, headers, None)
response = QgsBufferServerResponse()
self.server.handleRequest(request, response, project=project)
self.server.handleRequest(server_request, response, project=project)
return OWSResponse(response)

return _Client()
Expand Down
22 changes: 11 additions & 11 deletions test/test_legend_onoff.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import io
import logging

from PIL import Image

from test.utils import _build_query_string

from PIL import Image

LOGGER = logging.getLogger('server')

__copyright__ = 'Copyright 2023, 3Liz'
__copyright__ = 'Copyright 2024, 3Liz'
__license__ = 'GPL version 3'
__email__ = '[email protected]'

Expand All @@ -31,11 +31,11 @@

def test_categorized_symbol(client):
""" Test categorized symbol for layer. """
project = client.getproject(PROJECT)
project = client.get_project(PROJECT)

qs = dict(BASE_QUERY)
qs['LAYERS'] = 'categorized'
rv = client.getWithProject(_build_query_string(qs), project)
rv = client.get_with_project(_build_query_string(qs), project)
assert rv.status_code == 200
assert rv.headers.get('Content-Type', '').find('image/png') == 0

Expand All @@ -56,7 +56,7 @@ def test_categorized_symbol(client):
qs['LAYERS'] = 'categorized'
qs['LEGEND_ON'] = 'categorized:1'
qs['LEGEND_OFF'] = 'categorized:0,2,3,4'
rv = client.getWithProject(_build_query_string(qs), project)
rv = client.get_with_project(_build_query_string(qs), project)
assert rv.status_code == 200
assert rv.headers.get('Content-Type', '').find('image/png') == 0

Expand All @@ -76,7 +76,7 @@ def test_categorized_symbol(client):

qs = dict(BASE_QUERY)
qs['LAYERS'] = 'categorized'
rv = client.getWithProject(_build_query_string(qs), project)
rv = client.get_with_project(_build_query_string(qs), project)
assert rv.status_code == 200
assert rv.headers.get('Content-Type', '').find('image/png') == 0

Expand All @@ -97,11 +97,11 @@ def test_categorized_symbol(client):

def test_simple_rule_based(client):
""" Test rule based layer, simple conversion from categorized. """
project = client.getproject(PROJECT)
project = client.get_project(PROJECT)

qs = dict(BASE_QUERY)
qs['LAYERS'] = 'rule_based'
rv = client.getWithProject(_build_query_string(qs), project)
rv = client.get_with_project(_build_query_string(qs), project)
assert rv.status_code == 200
assert rv.headers.get('Content-Type', '').find('image/png') == 0

Expand All @@ -122,7 +122,7 @@ def test_simple_rule_based(client):
qs['LAYERS'] = 'rule_based'
qs['LEGEND_ON'] = 'rule_based:{49db22fd-3aed-495d-9140-4b82f50fdcfd}'
qs['LEGEND_OFF'] = 'rule_based:{1e75ef9b-1c18-46c1-b7f7-b16efc5bb791},{37b9b766-5309-4617-b0a4-1122168cbfd0},{bd0ace82-eee5-46c3-ad70-f8ecb7d50bb3},{77b34ffc-2198-4450-8e4d-270df282a81b}'
rv = client.getWithProject(_build_query_string(qs), project)
rv = client.get_with_project(_build_query_string(qs), project)
assert rv.status_code == 200
assert rv.headers.get('Content-Type', '').find('image/png') == 0

Expand All @@ -142,7 +142,7 @@ def test_simple_rule_based(client):

qs = dict(BASE_QUERY)
qs['LAYERS'] = 'categorized'
rv = client.getWithProject(_build_query_string(qs), project)
rv = client.get_with_project(_build_query_string(qs), project)
assert rv.status_code == 200
assert rv.headers.get('Content-Type', '').find('image/png') == 0

Expand Down

0 comments on commit 5bcad3e

Please sign in to comment.