server/info: expose a few config variables

This commit is contained in:
rr- 2016-06-18 10:55:44 +02:00
parent 4b39e79d5b
commit 3c3d0dbb8d
4 changed files with 61 additions and 4 deletions

15
API.md
View File

@ -1376,7 +1376,15 @@ data.
"featuredPost": <featured-post>,
"featuringTime": <time>,
"featuringUser": <user>,
"serverTime": <server-time>
"serverTime": <server-time>,
"config": {
"userNameRegex": <user-name-regex>,
"passwordRegex": <password-regex>,
"tagNameRegex": <tag-name-regex>,
"tagCategoryNameRegex": <tag-category-name-regex>,
"defaultUserRank": <default-rank>,
"privileges": <privileges>
}
}
```
@ -1384,7 +1392,10 @@ data.
Retrieves simple statistics. `<featured-post>` is null if there is no
featured post yet. `<server-time>` is pretty much the same as the `Date`
HTTP field, only formatted in a manner consistent with other dates.
HTTP field, only formatted in a manner consistent with other dates. Values
in `config` key are taken directly from the server config, with the
exception of privilege array keys being converted to lower camel case to
match the API convention.

View File

@ -2,7 +2,7 @@ import datetime
import os
from szurubooru import config
from szurubooru.api.base_api import BaseApi
from szurubooru.func import posts, users
from szurubooru.func import posts, users, util
class InfoApi(BaseApi):
def __init__(self):
@ -21,6 +21,15 @@ class InfoApi(BaseApi):
'featuringUser': users.serialize_user(post_feature.user, ctx.user) \
if post_feature else None,
'serverTime': datetime.datetime.now(),
'config': {
'userNameRegex': config.config['user_name_regex'],
'passwordRegex': config.config['password_regex'],
'tagNameRegex': config.config['tag_name_regex'],
'tagCategoryNameRegex': config.config['tag_category_name_regex'],
'defaultUserRank': config.config['default_rank'],
'privileges': util.snake_case_to_lower_camel_case_keys(
config.config['privileges']),
},
}
def _get_disk_usage(self):

View File

@ -6,6 +6,16 @@ import tempfile
from contextlib import contextmanager
from szurubooru import errors
def snake_case_to_lower_camel_case(text):
components = text.split('_')
return components[0] + ''.join(word[0].upper() + word[1:] for word in components[1:])
def snake_case_to_lower_camel_case_keys(source):
target = {}
for key, value in source.items():
target[snake_case_to_lower_camel_case(key)] = value
return target
def get_serialization_options(ctx):
return ctx.get_param_as_list('fields', required=False, default=None)

View File

@ -5,8 +5,32 @@ def test_info_api(
tmpdir, config_injector, context_factory, post_factory, fake_datetime):
directory = tmpdir.mkdir('data')
directory.join('test.txt').write('abc')
config_injector({'data_dir': str(directory)})
config_injector({
'data_dir': str(directory),
'user_name_regex': '1',
'password_regex': '2',
'tag_name_regex': '3',
'tag_category_name_regex': '4',
'default_rank': '5',
'privileges': {
'test_key1': 'test_value1',
'test_key2': 'test_value2',
},
})
db.session.add_all([post_factory(), post_factory()])
expected_config_key = {
'userNameRegex': '1',
'passwordRegex': '2',
'tagNameRegex': '3',
'tagCategoryNameRegex': '4',
'defaultUserRank': '5',
'privileges': {
'testKey1': 'test_value1',
'testKey2': 'test_value2',
},
}
info_api = api.InfoApi()
with fake_datetime('2016-01-01 13:00'):
assert info_api.get(context_factory()) == {
@ -16,6 +40,7 @@ def test_info_api(
'featuringTime': None,
'featuringUser': None,
'serverTime': datetime(2016, 1, 1, 13, 0),
'config': expected_config_key,
}
directory.join('test2.txt').write('abc')
with fake_datetime('2016-01-01 13:59'):
@ -26,6 +51,7 @@ def test_info_api(
'featuringTime': None,
'featuringUser': None,
'serverTime': datetime(2016, 1, 1, 13, 59),
'config': expected_config_key,
}
with fake_datetime('2016-01-01 14:01'):
assert info_api.get(context_factory()) == {
@ -35,4 +61,5 @@ def test_info_api(
'featuringTime': None,
'featuringUser': None,
'serverTime': datetime(2016, 1, 1, 14, 1),
'config': expected_config_key,
}