@@ -298,62 +298,80 @@ def schema_field_read(self, entity_type, field_name=None):
298
298
299
299
def find (self , entity_type , filters , fields = None , order = None , filter_operator = None , limit = 0 , retired_only = False , page = 0 ):
300
300
301
-
301
+
302
302
self .finds += 1
303
-
303
+
304
304
self ._validate_entity_type (entity_type )
305
305
# do not validate custom fields - this makes it hard to mock up a field quickly
306
306
#self._validate_entity_fields(entity_type, fields)
307
-
307
+
308
308
if isinstance (filters , dict ):
309
309
# complex filter style!
310
310
# {'conditions': [{'path': 'id', 'relation': 'is', 'values': [1]}], 'logical_operator': 'and'}
311
-
311
+
312
312
resolved_filters = []
313
313
for f in filters ["conditions" ]:
314
-
314
+
315
315
if f ["path" ].startswith ("$FROM$" ):
316
316
# special $FROM$Task.step.entity syntax
317
317
# skip this for now
318
318
continue
319
-
319
+
320
320
if len (f ["values" ]) != 1 :
321
321
# {'path': 'id', 'relation': 'in', 'values': [1,2,3]} --> ["id", "in", [1,2,3]]
322
322
resolved_filters .append ([ f ["path" ], f ["relation" ], f ["values" ] ])
323
323
else :
324
324
# {'path': 'id', 'relation': 'is', 'values': [3]} --> ["id", "is", 3]
325
325
resolved_filters .append ([ f ["path" ], f ["relation" ], f ["values" ][0 ] ])
326
-
326
+
327
327
else :
328
328
# traditiona style sg filters
329
- resolved_filters = filters
330
-
329
+ resolved_filters = filters
330
+
331
331
# now translate ["field", "in", 2,3,4] --> ["field", "in", [2, 3, 4]]
332
332
resolved_filters_2 = []
333
333
for f in resolved_filters :
334
-
334
+
335
335
if len (f ) > 3 :
336
336
# ["field", "in", 2,3,4] --> ["field", "in", [2, 3, 4]]
337
337
new_filter = [ f [0 ], f [1 ], f [2 :] ]
338
-
338
+
339
339
elif f [1 ] == "in" and not isinstance (f [2 ], list ):
340
340
# ["field", "in", 2] --> ["field", "in", [2]]
341
341
new_filter = [ f [0 ], f [1 ], [ f [2 ] ] ]
342
-
342
+
343
343
else :
344
344
new_filter = f
345
-
345
+
346
346
resolved_filters_2 .append (new_filter )
347
-
347
+
348
348
results = [row for row in self ._db [entity_type ].values () if self ._row_matches_filters (entity_type , row , resolved_filters_2 , filter_operator , retired_only )]
349
-
349
+
350
+ # handle the ordering of the recordset
351
+ if order :
352
+ # order: [{"field_name": "code", "direction": "asc"}, ... ]
353
+ for order_entry in order :
354
+ if "field_name" not in order_entry :
355
+ raise ValueError ("Order clauses must be list of dicts with keys 'field_name' and 'direction'!" )
356
+
357
+ order_field = order_entry ["field_name" ]
358
+ if order_entry ["direction" ] == "asc" :
359
+ desc_order = False
360
+ elif order_entry ["direction" ] == "desc" :
361
+ desc_order = True
362
+ else :
363
+ raise ValueError ("Unknown ordering direction" )
364
+
365
+ results = sorted (results , key = lambda k : k [order_field ], reverse = desc_order )
366
+
350
367
if fields is None :
351
368
fields = set (["type" , "id" ])
352
369
else :
353
370
fields = set (fields ) | set (["type" , "id" ])
354
-
371
+
372
+ # get the values requested
355
373
val = [dict ((field , self ._get_field_from_row (entity_type , row , field )) for field in fields ) for row in results ]
356
-
374
+
357
375
return val
358
376
359
377
0 commit comments