diff --git a/api/README.md b/api/README.md index 0173b09..2a0d320 100644 --- a/api/README.md +++ b/api/README.md @@ -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 diff --git a/api/app.py b/api/app.py index 7528be4..afe4465 100644 --- a/api/app.py +++ b/api/app.py @@ -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']: