Skip to content

Commit

Permalink
FIX : Limit & Sort with Filter
Browse files Browse the repository at this point in the history
  • Loading branch information
neo-garaix committed Nov 8, 2024
1 parent b7e8464 commit fe448cc
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions lizmap_server/expression_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,30 +868,6 @@ def virtualFields(params: Dict[str, str], response: QgsServerResponse, project:

req = QgsFeatureRequest()

# set limit
req_limit = params.get('LIMIT', '-1')
try:
req.setLimit(int(req_limit))
except ValueError:
raise ExpressionServiceError(
"Bad request error",
f"Invalid LIMIT for 'VirtualFields': \"{req_limit}\"",
400)

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

if req_sorting_order_param in ('asc', 'desc'):
# QGIS expects a boolean to know how to sort
req_sorting_field = params.get('SORTING_FIELD', '')
order_by_clause = QgsFeatureRequest.OrderByClause(req_sorting_field, req_sorting_order_param == 'asc')
req.setOrderBy(QgsFeatureRequest.OrderBy([order_by_clause]))
elif req_sorting_order_param != '' :
raise ExpressionServiceError(
"Bad request error",
f"Invalid SORTING_ORDER for 'VirtualFields': \"{req_sorting_order_param}\"",
400)

# get filter
req_filter = params.get('FILTER', '')
if req_filter:
Expand All @@ -916,6 +892,30 @@ def virtualFields(params: Dict[str, str], response: QgsServerResponse, project:
req_exp.prepare(exp_context)
req = QgsFeatureRequest(req_exp, exp_context)

# set limit
req_limit = params.get('LIMIT', '-1')
try:
req.setLimit(int(req_limit))
except ValueError:
raise ExpressionServiceError(
"Bad request error",
f"Invalid LIMIT for 'VirtualFields': \"{req_limit}\"",
400)

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

if req_sorting_order_param in ('asc', 'desc'):
# QGIS expects a boolean to know how to sort
req_sorting_field = params.get('SORTING_FIELD', '')
order_by_clause = QgsFeatureRequest.OrderByClause(req_sorting_field, req_sorting_order_param == 'asc')
req.setOrderBy(QgsFeatureRequest.OrderBy([order_by_clause]))
elif req_sorting_order_param != '' :
raise ExpressionServiceError(
"Bad request error",
f"Invalid SORTING_ORDER for 'VirtualFields': \"{req_sorting_order_param}\"",
400)

# With geometry
with_geom = to_bool(params.get('WITH_GEOMETRY'))
if not with_geom:
Expand Down

0 comments on commit fe448cc

Please sign in to comment.