Skip to content

Commit d663929

Browse files
committed
Add different output format to the GetSubsetString request
1 parent a5cbd4e commit d663929

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
* Add `FILTER_TYPE` to the `GETSUBSETSTRING` request with different values : `SQL` default value, `SAFESQL` or `EXPRESSION`
6+
57
## 2.7.0 - 2023-03-16
68

79
* Always provide a name with the version 'not found' when fetching the list of plugin

lizmap_server/lizmap_service.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,22 @@ def polygon_filter(
142142
return
143143

144144
try:
145+
filter_type_param = params.get('FILTER_TYPE', '').upper()
146+
if filter_type_param == 'SQL':
147+
filter_type = FilterType.PlainSqlQuery
148+
elif filter_type_param == 'SAFESQL':
149+
filter_type = FilterType.SafeSqlQuery
150+
elif filter_type_param == 'EXPRESSION':
151+
filter_type = FilterType.QgisExpression
152+
else:
153+
filter_type = FilterType.PlainSqlQuery
154+
145155
edition_context = is_editing_context(self.server_iface.requestHandler())
146156
filter_polygon_config = FilterByPolygon(
147157
cfg.get("filter_by_polygon"),
148158
layer,
149159
edition_context,
150-
filter_type=FilterType.PlainSqlQuery,
160+
filter_type=filter_type,
151161
)
152162
if filter_polygon_config.is_filtered():
153163
if not filter_polygon_config.is_valid():

test/test_lizmap_service.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def test_lizmap_getserversettings(client):
6161

6262

6363
def test_lizmap_service_filter_polygon_with_user(client):
64-
""" Test get polygon filter with the Lizmap service with a user. """
64+
""" Test get polygon filter with the Lizmap service with a user as SQL or QGIS expression. """
6565
project_file = "test_filter_layer_data_by_polygon_for_groups.qgs"
6666

6767
qs = (
@@ -70,7 +70,8 @@ def test_lizmap_service_filter_polygon_with_user(client):
7070
"REQUEST=GETSUBSETSTRING&"
7171
"MAP=france_parts.qgs&"
7272
"LAYER=shop_bakery&"
73-
"LIZMAP_USER_GROUPS=montferrier-sur-lez"
73+
# "FILTER_TYPE=SQL&" SQL must be the default value when not provided
74+
"LIZMAP_USER_GROUPS=montferrier-sur-lez&"
7475
)
7576
rv = client.get(qs, project_file)
7677
assert rv.status_code == 200
@@ -79,10 +80,22 @@ def test_lizmap_service_filter_polygon_with_user(client):
7980

8081
b = json.loads(rv.content.decode('utf-8'))
8182

83+
# SQL as output, the default format
8284
assert b['filter'] == '"id" IN ( 68 )'
8385
assert b['status'] == 'success'
8486
assert b['polygons'].startswith('SRID=3857;MultiPolygon')
8587

88+
# QGIS expression
89+
qs += 'FILTER_TYPE=expression&'
90+
rv = client.get(qs, project_file)
91+
assert rv.status_code == 200
92+
assert rv.headers.get('Content-Type', '').find('application/json') == 0
93+
b = json.loads(rv.content.decode('utf-8'))
94+
# geom_from_wkt is a QGIS expression subset
95+
assert 'geom_from_wkt' in b['filter']
96+
assert b['status'] == 'success'
97+
assert b['polygons'].startswith('SRID=3857;MultiPolygon')
98+
8699

87100
def test_lizmap_service_filter_polygon_without_user(client):
88101
""" Test get polygon filter with the Lizmap service without a user. """

0 commit comments

Comments
 (0)