Skip to content

Commit cf22401

Browse files
committed
#35786 Adds basic find() ordering support to mockgun.
This minor change extends the existing mockgun library with order support for the find() method. Previously, any order directives were ignored by mockgun. Please note that mockgun is still highly experimental at this point. Closes #113
1 parent 54c68db commit cf22401

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

shotgun_api3/lib/mockgun.py

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -298,62 +298,80 @@ def schema_field_read(self, entity_type, field_name=None):
298298

299299
def find(self, entity_type, filters, fields=None, order=None, filter_operator=None, limit=0, retired_only=False, page=0):
300300

301-
301+
302302
self.finds += 1
303-
303+
304304
self._validate_entity_type(entity_type)
305305
# do not validate custom fields - this makes it hard to mock up a field quickly
306306
#self._validate_entity_fields(entity_type, fields)
307-
307+
308308
if isinstance(filters, dict):
309309
# complex filter style!
310310
# {'conditions': [{'path': 'id', 'relation': 'is', 'values': [1]}], 'logical_operator': 'and'}
311-
311+
312312
resolved_filters = []
313313
for f in filters["conditions"]:
314-
314+
315315
if f["path"].startswith("$FROM$"):
316316
# special $FROM$Task.step.entity syntax
317317
# skip this for now
318318
continue
319-
319+
320320
if len(f["values"]) != 1:
321321
# {'path': 'id', 'relation': 'in', 'values': [1,2,3]} --> ["id", "in", [1,2,3]]
322322
resolved_filters.append([ f["path"], f["relation"], f["values"] ])
323323
else:
324324
# {'path': 'id', 'relation': 'is', 'values': [3]} --> ["id", "is", 3]
325325
resolved_filters.append([ f["path"], f["relation"], f["values"][0] ])
326-
326+
327327
else:
328328
# traditiona style sg filters
329-
resolved_filters = filters
330-
329+
resolved_filters = filters
330+
331331
# now translate ["field", "in", 2,3,4] --> ["field", "in", [2, 3, 4]]
332332
resolved_filters_2 = []
333333
for f in resolved_filters:
334-
334+
335335
if len(f) > 3:
336336
# ["field", "in", 2,3,4] --> ["field", "in", [2, 3, 4]]
337337
new_filter = [ f[0], f[1], f[2:] ]
338-
338+
339339
elif f[1] == "in" and not isinstance(f[2], list):
340340
# ["field", "in", 2] --> ["field", "in", [2]]
341341
new_filter = [ f[0], f[1], [ f[2] ] ]
342-
342+
343343
else:
344344
new_filter = f
345-
345+
346346
resolved_filters_2.append(new_filter)
347-
347+
348348
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+
350367
if fields is None:
351368
fields = set(["type", "id"])
352369
else:
353370
fields = set(fields) | set(["type", "id"])
354-
371+
372+
# get the values requested
355373
val = [dict((field, self._get_field_from_row(entity_type, row, field)) for field in fields) for row in results]
356-
374+
357375
return val
358376

359377

0 commit comments

Comments
 (0)