Implement random sort order in API

master
Nikola Forró 7 years ago
parent ab8a04ec88
commit 63b68f4b66

@ -62,7 +62,7 @@ Parameters:
* `filter` (optional) - Filter string
* `sort_by` (optional) - Sort field, `id`, `date`, `game` or `text`
* `sort_order` (optional) - Sort order, `asc` or `desc`
* `sort_order` (optional) - Sort order, `asc`, `desc` or `random`
* `page_number` (optional) - Page number to retrieve
* `page_size` (optional) - Number of quotes per page

@ -111,8 +111,9 @@ class QuotesResource(flask_restful.Resource):
if args['filter']:
q = q.filter(Quote.text.ilike('%{}%'.format(args['filter'])))
count = q.count()
# TODO: random order
if args['sort_by']:
if args['sort_order'] == 'random':
q = q.order_by(sqlalchemy.func.random())
elif args['sort_by']:
col = getattr(Quote, args['sort_by'], None)
if col:
if args['sort_order']:

Loading…
Cancel
Save