Skip to content

Commit e4c6870

Browse files
committed
STAC API: fix sortby handling in POST mode (#1200)
1 parent f9916be commit e4c6870

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

pycsw/stac/api.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,15 @@ def items(self, headers_, json_post_data, args, collection='metadata:main'):
446446
})
447447

448448
if 'limit' in json_post_data2:
449-
LOGGER.debug('Detected limit query parameter')
449+
LOGGER.debug('Detected limit parameter')
450450
args['limit'] = json_post_data2.pop('limit')
451451

452+
if 'sortby' in json_post_data2:
453+
LOGGER.debug('Detected sortby parameter')
454+
args['sortby'] = json_post_data2['sortby'][0]['field']
455+
if json_post_data2['sortby'][0].get('direction', 'asc') == 'desc':
456+
args['sortby'] = f"-{args['sortby']}"
457+
452458
if 'collections' in json_post_data2:
453459
LOGGER.debug('Detected collections query parameter')
454460
cql_ops.append({

tests/functionaltests/suites/stac_api/test_stac_api_functional.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,31 @@ def test_items(config):
791791
content = json.loads(api.items({}, cql_json, {})[2])
792792
assert content['numberMatched'] == 1
793793

794+
cql_json = {
795+
'filter-lang': 'cql2-json',
796+
'filter': {
797+
'op': 'and',
798+
'args': [
799+
{
800+
'op': '=',
801+
'args': [
802+
{
803+
'property': 'instrument'
804+
},
805+
'msi'
806+
]
807+
}
808+
]
809+
},
810+
'sortby': [{'field': 'datetime', 'direction': 'asc'}]
811+
}
812+
813+
content = json.loads(api.items({}, cql_json, {})[2])
814+
assert content['features'][0]['id'] == 'S2B_MSIL1C_20190910T095029_N0208_R079_T33TWN_20190910T120910.SAFE' # noqa
815+
cql_json['sortby'][0]['direction'] = 'desc'
816+
content = json.loads(api.items({}, cql_json, {})[2])
817+
assert content['features'][0]['id'] == 'S2A_MSIL2A_20241128T092331_R093_T34SEJ_20241128T122153' # noqa
818+
794819

795820
def test_item(config):
796821
api = STACAPI(config)

0 commit comments

Comments
 (0)