From 63b68f4b66ec2db6d65ddcec9825df6434a514ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Forr=C3=B3?= Date: Fri, 11 May 2018 10:53:39 +0200 Subject: [PATCH] Implement random sort order in API --- api/README.md | 2 +- api/app.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) 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']: