diff options
| author | Hunternif <hunternif@gmail.com> | 2019-05-02 03:57:31 +0700 |
|---|---|---|
| committer | Hunternif <hunternif@gmail.com> | 2019-05-02 03:57:31 +0700 |
| commit | 0b4bdc57894c885ba055aed74de8b9c94c8e2f60 (patch) | |
| tree | 4d217e71aa02fef005afe86d8845e90fe93a9f83 /server/szurubooru/api/post_api.py | |
| parent | 82facef4aa1f1ede518acf1435d6ed98d37f8df1 (diff) | |
server: implement median for posts, for any query
Diffstat (limited to 'server/szurubooru/api/post_api.py')
| -rw-r--r-- | server/szurubooru/api/post_api.py | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/server/szurubooru/api/post_api.py b/server/szurubooru/api/post_api.py index 95c0ebc..ffbf34d 100644 --- a/server/szurubooru/api/post_api.py +++ b/server/szurubooru/api/post_api.py @@ -1,3 +1,4 @@ +from math import ceil from typing import Optional, Dict, List from datetime import datetime from szurubooru import db, model, errors, rest, search @@ -277,3 +278,21 @@ def get_posts_by_image( for lookalike in lookalikes ], } + + +@rest.routes.get('/posts/median/?') +def get_posts_median( + ctx: rest.Context, _params: Dict[str, str] = {}) -> rest.Response: + auth.verify_privilege(ctx.user, 'posts:list') + _search_executor_config.user = ctx.user + query_text = ctx.get_param_as_string('query', default='') + total_count = _search_executor.count(query_text) + offset = ceil(total_count / 2) - 1 + _, results = _search_executor.execute(query_text, offset, 1) + return { + 'query': query_text, + 'offset': offset, + 'limit': 1, + 'total': len(results), + 'results': list([_serialize_post(ctx, post) for post in results]) + } |