|
@@ -250,10 +250,14 @@ class _LeCrud(object):
|
250
|
250
|
#
|
251
|
251
|
# @param query_filters list : list of string of query filters (or tuple (FIELD, OPERATOR, VALUE) ) see @ref leobject_filters
|
252
|
252
|
# @param field_list list|None : list of string representing fields see @ref leobject_filters
|
|
253
|
+ # @param order list : A list of field names or tuple (FIELDNAME, [ASC | DESC])
|
|
254
|
+ # @param groups list : A list of field names or tuple (FIELDNAME, [ASC | DESC])
|
|
255
|
+ # @param limit int : The maximum number of returned results
|
|
256
|
+ # @param offset int : offset
|
253
|
257
|
# @return A list of lodel editorial components instance
|
254
|
258
|
# @todo think about LeObject and LeClass instanciation (partial instanciation, etc)
|
255
|
259
|
@classmethod
|
256
|
|
- def get(cls, query_filters, field_list = None):
|
|
260
|
+ def get(cls, query_filters, field_list = None, order = None, groups = None, limit = None, offset = 0):
|
257
|
261
|
if field_list is None or len(field_list) == 0:
|
258
|
262
|
#default field_list
|
259
|
263
|
field_list = cls.fieldlist()
|
|
@@ -263,8 +267,32 @@ class _LeCrud(object):
|
263
|
267
|
#preparing filters
|
264
|
268
|
filters, relational_filters = cls._prepare_filters(query_filters)
|
265
|
269
|
|
|
270
|
+ #preparing order
|
|
271
|
+ if order is None:
|
|
272
|
+ order = []
|
|
273
|
+ else:
|
|
274
|
+ order = self._prepare_order_fields(order)
|
|
275
|
+ if isinstance(order, Exception):
|
|
276
|
+ raise order #can be buffered and raised later, but _prepare_filters raise when fails
|
|
277
|
+
|
|
278
|
+ #preparing groups
|
|
279
|
+ if groups is None:
|
|
280
|
+ groups = []
|
|
281
|
+ else:
|
|
282
|
+ groups = cls._order_fields(groups)
|
|
283
|
+ if isinstance(groups, Exception):
|
|
284
|
+ raise groups # can also be buffered and raised later
|
|
285
|
+
|
|
286
|
+ #checking limit and offset values
|
|
287
|
+ if not (limit is None):
|
|
288
|
+ if limit <= 0:
|
|
289
|
+ raise ValueError("Invalid limit given : %d"%limit)
|
|
290
|
+ if not (offset is None):
|
|
291
|
+ if offset < 0:
|
|
292
|
+ raise ValueError("Invalid offset given : %d"%offset)
|
|
293
|
+
|
266
|
294
|
#Fetching editorial components from datasource
|
267
|
|
- results = cls._datasource.select(cls, field_list, filters, relational_filters)
|
|
295
|
+ results = cls._datasource.select(cls, field_list, filters, relational_filters, order, groups, limit, offset)
|
268
|
296
|
|
269
|
297
|
return results
|
270
|
298
|
|
|
@@ -382,6 +410,28 @@ class _LeCrud(object):
|
382
|
410
|
if field not in cls.fieldlist():
|
383
|
411
|
return ValueError("No such field '%s' in %s"%(field, cls.__name__))
|
384
|
412
|
return field
|
|
413
|
+
|
|
414
|
+ ## @brief Prepare the order parameter for the get method
|
|
415
|
+ # @note if an item in order_list is just a str it is considered as ASC by default
|
|
416
|
+ # @param order_list list : A list of field name or tuple (FIELDNAME, [ASC|DESC])
|
|
417
|
+ # @return a list of tuple (FIELDNAME, [ASC|DESC] )
|
|
418
|
+ @classmethod
|
|
419
|
+ def _prepare_order_fields(cls, order_field_list):
|
|
420
|
+ errors = dict()
|
|
421
|
+ result = []
|
|
422
|
+ for order_field in order_field_list:
|
|
423
|
+ if not isinstance(order_field, tuple):
|
|
424
|
+ order_field = (order_field, 'ASC')
|
|
425
|
+ if len(order_field) != 2 or order_field[1].upper() not in ['ASC', 'DESC']:
|
|
426
|
+ errors[order_field] = ValueError("Expected a string or a tuple with (FIELDNAME, ['ASC'|'DESC']) but got : %s"%order_field)
|
|
427
|
+ else:
|
|
428
|
+ ret = self._check_field(order_field[0])
|
|
429
|
+ if isinstance(ret, Exception):
|
|
430
|
+ errors[order_field] = ret
|
|
431
|
+ result.append(order_field)
|
|
432
|
+ if len(errors) > 0:
|
|
433
|
+ return LeApiErrors("Errors when preparing ordering fields", errors)
|
|
434
|
+ return result
|
385
|
435
|
|
386
|
436
|
## @brief Prepare filters for datasource
|
387
|
437
|
#
|