Skip to content

Commit

Permalink
ADD: Optional choice to set the order of requests
Browse files Browse the repository at this point in the history
  • Loading branch information
neo-garaix committed Nov 7, 2024
1 parent 08867ce commit ebcd6c6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lizmap_server/expression_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,8 @@ def virtualFields(params: Dict[str, str], response: QgsServerResponse, project:
FIELDS=list of requested field separated by comma
WITH_GEOMETRY=False
LIMIT=number of features to return or nothing to return all
SORTING_ORDER=asc or desc, default = asc
SORTING_FIELD=field name to sort by
"""
logger = Logger()
layer_name = params.get('LAYER', '')
Expand Down Expand Up @@ -876,6 +878,22 @@ def virtualFields(params: Dict[str, str], response: QgsServerResponse, project:
f"Invalid LIMIT for 'VirtualFields': \"{req_limit}\"",
400)

# set orderby
req_sorting_order = params.get('SORTING_ORDER', '')

if req_sorting_order in ['asc', 'desc']:
req_sorting_order = req_sorting_order == 'asc'
elif req_sorting_order != '' :
raise ExpressionServiceError(
"Bad request error",
f"Invalid SORTING_ORDER for 'VirtualFields': \"{req_sorting_order}\"",
400)

req_sorting_field = params.get('SORTING_FIELD', '')

if type(req_sorting_order) is bool :
req.setOrderBy(QgsFeatureRequest.OrderBy([QgsFeatureRequest.OrderByClause(req_sorting_field, req_sorting_order)]))

# get filter
req_filter = params.get('FILTER', '')
if req_filter:
Expand Down
41 changes: 41 additions & 0 deletions test/test_expression_service_virtualfields.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,44 @@ def test_request_limit(client):
assert b['features'][0]['properties']['a'] == 1
assert 'b' in b['features'][0]['properties']
assert b['features'][0]['properties']['b'] == 2


def test_request_order(client):
""" Test Expression VirtualFields request
"""
projectfile = "france_parts.qgs"

# Make a request
qs = "?SERVICE=EXPRESSION&REQUEST=VirtualFields&MAP=france_parts.qgs&LAYER=france_parts"
qs += "&VIRTUALS={\"a\":\"%s\", \"b\":\"%s\"}" % (
quote('1', safe=''), quote('1 + 1', safe=''))
qs += "&SORTING_ORDER=desc"
qs += "&SORTING_FIELD=NAME_1"
rv = client.get(qs, projectfile)
assert rv.status_code == 200
assert rv.headers.get('Content-Type', '').find('application/json') == 0

b = json.loads(rv.content.decode('utf-8'))
assert 'type' in b
assert b['type'] == 'FeatureCollection'

assert 'features' in b
assert len(b['features']) == 4

assert 'type' in b['features'][0]
assert b['features'][0]['type'] == 'Feature'

assert 'geometry' in b['features'][0]
assert b['features'][0]['geometry'] is None

assert 'properties' in b['features'][0]
assert 'NAME_1' in b['features'][0]['properties']
assert 'Region' in b['features'][0]['properties']

assert 'a' in b['features'][0]['properties']
assert b['features'][0]['properties']['a'] == 1
assert 'b' in b['features'][0]['properties']
assert b['features'][0]['properties']['b'] == 2

assert b['features'][0]['id'] == 'france_parts.2'
assert b['features'][3]['id'] == 'france_parts.0'

0 comments on commit ebcd6c6

Please sign in to comment.