server/info: return who featured the post and when

This commit is contained in:
rr- 2016-05-29 12:38:47 +02:00
parent 9b0c2012a7
commit abef6e5c35
4 changed files with 24 additions and 7 deletions

8
API.md
View File

@ -858,7 +858,9 @@ data.
Retrieves the post that is currently featured on the main page in web Retrieves the post that is currently featured on the main page in web
client. If no post is featured, `<post>` is null and `snapshots` array is client. If no post is featured, `<post>` is null and `snapshots` array is
empty. empty. Note that this method exists mostly for compatibility with setting
featured post - most of times, you'd want to use query global info which
contains more information.
## Featuring post ## Featuring post
- **Request** - **Request**
@ -1351,7 +1353,9 @@ data.
{ {
"postCount": <post-count>, "postCount": <post-count>,
"diskUsage": <disk-usage>, // in bytes "diskUsage": <disk-usage>, // in bytes
"featuredPost": <featured-post> "featuredPost": <featured-post>,
"featuringTime": <time>,
"featuringUser": <user>
} }
``` ```

View File

@ -2,7 +2,7 @@ import datetime
import os import os
from szurubooru import config from szurubooru import config
from szurubooru.api.base_api import BaseApi from szurubooru.api.base_api import BaseApi
from szurubooru.func import posts from szurubooru.func import posts, users
class InfoApi(BaseApi): class InfoApi(BaseApi):
def __init__(self): def __init__(self):
@ -11,11 +11,15 @@ class InfoApi(BaseApi):
self._cache_result = None self._cache_result = None
def get(self, ctx): def get(self, ctx):
featured_post = posts.try_get_featured_post() post_feature = posts.try_get_current_post_feature()
return { return {
'postCount': posts.get_post_count(), 'postCount': posts.get_post_count(),
'diskUsage': self._get_disk_usage(), 'diskUsage': self._get_disk_usage(),
'featuredPost': posts.serialize_post(featured_post, ctx.user), 'featuredPost': posts.serialize_post(post_feature.post, ctx.user) \
if post_feature else None,
'featuringTime': post_feature.time if post_feature else None,
'featuringUser': users.serialize_user(post_feature.user, ctx.user) \
if post_feature else None,
} }
def _get_disk_usage(self): def _get_disk_usage(self):

View File

@ -126,11 +126,14 @@ def get_post_by_id(post_id):
raise PostNotFoundError('Post %r not found.' % post_id) raise PostNotFoundError('Post %r not found.' % post_id)
return post return post
def try_get_featured_post(): def try_get_current_post_feature():
post_feature = db.session \ return db.session \
.query(db.PostFeature) \ .query(db.PostFeature) \
.order_by(db.PostFeature.time.desc()) \ .order_by(db.PostFeature.time.desc()) \
.first() .first()
def try_get_featured_post():
post_feature = try_get_current_post_feature()
return post_feature.post if post_feature else None return post_feature.post if post_feature else None
def create_post(content, tag_names, user): def create_post(content, tag_names, user):

View File

@ -12,6 +12,8 @@ def test_info_api(
'postCount': 2, 'postCount': 2,
'diskUsage': 3, 'diskUsage': 3,
'featuredPost': None, 'featuredPost': None,
'featuringTime': None,
'featuringUser': None,
} }
directory.join('test2.txt').write('abc') directory.join('test2.txt').write('abc')
with fake_datetime('13:59'): with fake_datetime('13:59'):
@ -19,10 +21,14 @@ def test_info_api(
'postCount': 2, 'postCount': 2,
'diskUsage': 3, # still 3 - it's cached 'diskUsage': 3, # still 3 - it's cached
'featuredPost': None, 'featuredPost': None,
'featuringTime': None,
'featuringUser': None,
} }
with fake_datetime('14:01'): with fake_datetime('14:01'):
assert info_api.get(context_factory()) == { assert info_api.get(context_factory()) == {
'postCount': 2, 'postCount': 2,
'diskUsage': 6, # cache expired 'diskUsage': 6, # cache expired
'featuredPost': None, 'featuredPost': None,
'featuringTime': None,
'featuringUser': None,
} }