server/search: rename order: to sort:

This commit is contained in:
rr- 2016-04-22 19:37:58 +02:00
parent 14059de1d7
commit a30886cc70
13 changed files with 117 additions and 117 deletions

8
API.md
View file

@ -329,7 +329,7 @@ data.
| `suggestion-count` | with given number of suggestions |
| `implication-count` | with given number of implications |
**Order tokens**
**Sort style tokens**
| `<value>` | Description |
| ------------------- | ---------------------------- |
@ -637,7 +637,7 @@ data.
| `login-date` | alias of `last-login-date` |
| `login-time` | alias of `last-login-date` |
**Order tokens**
**Sort style tokens**
| `<value>` | Description |
| ----------------- | -------------------------- |
@ -907,7 +907,7 @@ data.
| `operation` | `changed`, `created` or `deleted` |
| `user` | name of the user that created given snapshot |
**Order tokens**
**Sort style tokens**
None. The snapshots are always sorted by creation time.
@ -1095,7 +1095,7 @@ be of following form:
| ----------------- | ----------------- | ------------------------------------------ |
| `<value>` | anonymous tokens | basic filters |
| `<key>:<value>` | named tokens | advanced filters |
| `order:<style>` | order tokens | sort results |
| `sort:<style>` | sort style tokens | sort the results |
| `special:<value>` | special tokens | filters usually tied to the logged in user |
Most of anonymous and named tokens support ranged and composite values that

View file

@ -21,9 +21,9 @@ can be of following form:</p>
<td>advanced filters</td>
</tr>
<tr>
<td><code>order:&lt;style&gt;</code></td>
<td>order tokens</td>
<td>sort results</td>
<td><code>sort:&lt;style&gt;</code></td>
<td>sort style tokens</td>
<td>sort the results</td>
</tr>
<tr>
<td><code>special:&lt;value&gt;</code></td>
@ -76,7 +76,7 @@ take following form:</p>
<p>All tokens can be negated by prepending them with <code>-</code>.</p>
<p>Order token values can be appended with <code>,asc</code> or
<p>Sort style token values can be appended with <code>,asc</code> or
<code>,desc</code> to control the sort direction, which can be also controlled
by negating the whole token.</p>

View file

@ -73,7 +73,7 @@
</tbody>
</table>
<p><strong>Order tokens</strong></p>
<p><strong>Sort style tokens</strong></p>
<table>
<tbody>

View file

@ -61,7 +61,7 @@
</tbody>
</table>
<p><strong>Order tokens</strong></p>
<p><strong>Sort style tokens</strong></p>
<table>
<tbody>

View file

@ -37,7 +37,7 @@
</tbody>
</table>
<p><strong>Order tokens</strong></p>
<p><strong>Sort style tokens</strong></p>
<table>
<tbody>

View file

@ -1,5 +1,6 @@
[basic]
method-rgx=[a-z_][a-z0-9_]{2,30}$|^test_
function-rgx=^_?[a-z_][a-z0-9_]{2,}$|^test_
method-rgx=^[a-z_][a-z0-9_]{2,}$|^test_
[variables]
dummy-variables-rgx=_|dummy

View file

@ -6,10 +6,11 @@ from szurubooru.func import posts
class InfoApi(BaseApi):
def __init__(self):
super().__init__()
self._cache_time = None
self._cache_result = None
def get(self, ctx):
def get(self, _ctx):
return {
'postCount': posts.get_post_count(),
'diskUsage': self._get_disk_usage()

View file

@ -4,8 +4,8 @@ from szurubooru.func import util
from szurubooru.search import criteria
class BaseSearchConfig(object):
ORDER_DESC = 0
ORDER_ASC = 1
SORT_DESC = -1
SORT_ASC = 1
def create_query(self):
raise NotImplementedError()
@ -23,7 +23,7 @@ class BaseSearchConfig(object):
return {}
@property
def order_columns(self):
def sort_columns(self):
return {}
@staticmethod

View file

@ -64,8 +64,8 @@ class SearchExecutor(object):
return query
def _handle_key_value(self, query, key, value, negated):
if key == 'order':
return self._handle_order(query, value, negated)
if key == 'sort':
return self._handle_sort(query, value, negated)
elif key == 'special':
return self._handle_special(query, value, negated)
else:
@ -99,40 +99,46 @@ class SearchExecutor(object):
'Unknown special token: %r. Available special tokens: %r.' % (
value, list(self._search_config.special_filters.keys())))
def _handle_order(self, query, value, negated):
def _handle_sort(self, query, value, negated):
if value.count(',') == 0:
order_str = None
dir_str = None
elif value.count(',') == 1:
value, order_str = value.split(',')
value, dir_str = value.split(',')
else:
raise errors.SearchError(
'Too many commas in order search token.')
raise errors.SearchError('Too many commas in sort style token.')
if value not in self._search_config.order_columns:
try:
column, default_sort = self._search_config.sort_columns[value]
except KeyError:
raise errors.SearchError(
'Unknown search order: %r. Available search orders: %r.' % (
value, list(self._search_config.order_columns.keys())))
'Unknown sort style: %r. Available sort styles: %r.' % (
value, list(self._search_config.sort_columns.keys())))
column, default_order = self._search_config.order_columns[value]
if order_str == 'asc':
order = self._search_config.ORDER_ASC
elif order_str == 'desc':
order = self._search_config.ORDER_DESC
elif order_str is None:
order = default_order
else:
raise errors.SearchError(
'Unknown search direction: %r.' % order_str)
if negated:
if order == self._search_config.ORDER_ASC:
order = self._search_config.ORDER_DESC
elif order == self._search_config.ORDER_DESC:
order = self._search_config.ORDER_ASC
if order == self._search_config.ORDER_ASC:
column = column.asc()
elif order == self._search_config.ORDER_DESC:
column = column.desc()
return query.order_by(column)
sort_asc = self._search_config.SORT_ASC
sort_desc = self._search_config.SORT_DESC
try:
sort_map = {
'asc': sort_asc,
'desc': sort_desc,
'': default_sort,
None: default_sort,
}
sort = sort_map[dir_str]
except KeyError:
raise errors.SearchError('Unknown search direction: %r.' % dir_str)
if negated and sort:
sort = -sort
transform_map = {
sort_asc: lambda input: input.asc(),
sort_desc: lambda input: input.desc(),
None: lambda input: input,
}
print(sort)
transform = transform_map[sort]
return query.order_by(transform(column))
def _create_criterion(self, value, negated):
if '..' in value:

View file

@ -13,10 +13,6 @@ class TagSearchConfig(BaseSearchConfig):
def anonymous_filter(self):
return self._create_str_filter(db.Tag.first_name)
@property
def special_filters(self):
return {}
@property
def named_filters(self):
return {
@ -36,20 +32,20 @@ class TagSearchConfig(BaseSearchConfig):
}
@property
def order_columns(self):
def sort_columns(self):
return {
'random': (func.random(), None),
'name': (db.Tag.first_name, self.ORDER_ASC),
'category': (db.Tag.category, self.ORDER_ASC),
'creation-date': (db.Tag.creation_time, self.ORDER_DESC),
'creation-time': (db.Tag.creation_time, self.ORDER_DESC),
'last-edit-date': (db.Tag.last_edit_time, self.ORDER_DESC),
'last-edit-time': (db.Tag.last_edit_time, self.ORDER_DESC),
'edit-date': (db.Tag.last_edit_time, self.ORDER_DESC),
'edit-time': (db.Tag.last_edit_time, self.ORDER_DESC),
'usages': (db.Tag.post_count, self.ORDER_DESC),
'usage-count': (db.Tag.post_count, self.ORDER_DESC),
'post-count': (db.Tag.post_count, self.ORDER_DESC),
'suggestion-count': (db.Tag.suggestion_count, self.ORDER_DESC),
'implication-count': (db.Tag.implication_count, self.ORDER_DESC),
'name': (db.Tag.first_name, self.SORT_ASC),
'category': (db.Tag.category, self.SORT_ASC),
'creation-date': (db.Tag.creation_time, self.SORT_DESC),
'creation-time': (db.Tag.creation_time, self.SORT_DESC),
'last-edit-date': (db.Tag.last_edit_time, self.SORT_DESC),
'last-edit-time': (db.Tag.last_edit_time, self.SORT_DESC),
'edit-date': (db.Tag.last_edit_time, self.SORT_DESC),
'edit-time': (db.Tag.last_edit_time, self.SORT_DESC),
'usages': (db.Tag.post_count, self.SORT_DESC),
'usage-count': (db.Tag.post_count, self.SORT_DESC),
'post-count': (db.Tag.post_count, self.SORT_DESC),
'suggestion-count': (db.Tag.suggestion_count, self.SORT_DESC),
'implication-count': (db.Tag.implication_count, self.SORT_DESC),
}

View file

@ -15,10 +15,6 @@ class UserSearchConfig(BaseSearchConfig):
def anonymous_filter(self):
return self._create_str_filter(db.User.name)
@property
def special_filters(self):
return {}
@property
def named_filters(self):
return {
@ -32,14 +28,14 @@ class UserSearchConfig(BaseSearchConfig):
}
@property
def order_columns(self):
def sort_columns(self):
return {
'random': (None, func.random()),
'name': (db.User.name, self.ORDER_ASC),
'creation-date': (db.User.creation_time, self.ORDER_DESC),
'creation-time': (db.User.creation_time, self.ORDER_DESC),
'last-login-date': (db.User.last_login_time, self.ORDER_DESC),
'last-login-time': (db.User.last_login_time, self.ORDER_DESC),
'login-date': (db.User.last_login_time, self.ORDER_DESC),
'login-time': (db.User.last_login_time, self.ORDER_DESC),
'random': (func.random(), None),
'name': (db.User.name, self.SORT_ASC),
'creation-date': (db.User.creation_time, self.SORT_DESC),
'creation-time': (db.User.creation_time, self.SORT_DESC),
'last-login-date': (db.User.last_login_time, self.SORT_DESC),
'last-login-time': (db.User.last_login_time, self.SORT_DESC),
'login-date': (db.User.last_login_time, self.SORT_DESC),
'login-time': (db.User.last_login_time, self.SORT_DESC),
}

View file

@ -90,24 +90,24 @@ def test_anonymous(verify_unpaged, tag_factory, input, expected_tag_names):
@pytest.mark.parametrize('input,expected_tag_names', [
('', ['t1', 't2']),
('order:name', ['t1', 't2']),
('-order:name', ['t2', 't1']),
('order:name,asc', ['t1', 't2']),
('order:name,desc', ['t2', 't1']),
('-order:name,asc', ['t2', 't1']),
('-order:name,desc', ['t1', 't2']),
('sort:name', ['t1', 't2']),
('-sort:name', ['t2', 't1']),
('sort:name,asc', ['t1', 't2']),
('sort:name,desc', ['t2', 't1']),
('-sort:name,asc', ['t2', 't1']),
('-sort:name,desc', ['t1', 't2']),
])
def test_order_by_name(verify_unpaged, tag_factory, input, expected_tag_names):
def test_sort_by_name(verify_unpaged, tag_factory, input, expected_tag_names):
db.session.add(tag_factory(names=['t2']))
db.session.add(tag_factory(names=['t1']))
verify_unpaged(input, expected_tag_names)
@pytest.mark.parametrize('input,expected_user_names', [
('', ['t1', 't2', 't3']),
('order:creation-date', ['t3', 't2', 't1']),
('order:creation-time', ['t3', 't2', 't1']),
('sort:creation-date', ['t3', 't2', 't1']),
('sort:creation-time', ['t3', 't2', 't1']),
])
def test_order_by_creation_time(
def test_sort_by_creation_time(
verify_unpaged, tag_factory, input, expected_user_names):
tag1 = tag_factory(names=['t1'])
tag2 = tag_factory(names=['t2'])
@ -119,9 +119,9 @@ def test_order_by_creation_time(
verify_unpaged(input, expected_user_names)
@pytest.mark.parametrize('input,expected_tag_names', [
('order:suggestion-count', ['t1', 't2', 'sug1', 'sug2', 'sug3']),
('sort:suggestion-count', ['t1', 't2', 'sug1', 'sug2', 'sug3']),
])
def test_order_by_suggestion_count(
def test_sort_by_suggestion_count(
verify_unpaged, tag_factory, input, expected_tag_names):
sug1 = tag_factory(names=['sug1'])
sug2 = tag_factory(names=['sug2'])
@ -136,9 +136,9 @@ def test_order_by_suggestion_count(
verify_unpaged(input, expected_tag_names)
@pytest.mark.parametrize('input,expected_tag_names', [
('order:implication-count', ['t1', 't2', 'sug1', 'sug2', 'sug3']),
('sort:implication-count', ['t1', 't2', 'sug1', 'sug2', 'sug3']),
])
def test_order_by_implication_count(
def test_sort_by_implication_count(
verify_unpaged, tag_factory, input, expected_tag_names):
sug1 = tag_factory(names=['sug1'])
sug2 = tag_factory(names=['sug2'])

View file

@ -126,14 +126,14 @@ def test_paging(
@pytest.mark.parametrize('input,expected_user_names', [
('', ['u1', 'u2']),
('order:name', ['u1', 'u2']),
('-order:name', ['u2', 'u1']),
('order:name,asc', ['u1', 'u2']),
('order:name,desc', ['u2', 'u1']),
('-order:name,asc', ['u2', 'u1']),
('-order:name,desc', ['u1', 'u2']),
('sort:name', ['u1', 'u2']),
('-sort:name', ['u2', 'u1']),
('sort:name,asc', ['u1', 'u2']),
('sort:name,desc', ['u2', 'u1']),
('-sort:name,asc', ['u2', 'u1']),
('-sort:name,desc', ['u1', 'u2']),
])
def test_order_by_name(
def test_sort_by_name(
verify_unpaged, input, expected_user_names, user_factory):
db.session.add(user_factory(name='u2'))
db.session.add(user_factory(name='u1'))
@ -141,15 +141,15 @@ def test_order_by_name(
@pytest.mark.parametrize('input,expected_user_names', [
('', ['u1', 'u2', 'u3']),
('order:creation-date', ['u3', 'u2', 'u1']),
('order:creation-time', ['u3', 'u2', 'u1']),
('-order:creation-date', ['u1', 'u2', 'u3']),
('order:creation-date,asc', ['u1', 'u2', 'u3']),
('order:creation-date,desc', ['u3', 'u2', 'u1']),
('-order:creation-date,asc', ['u3', 'u2', 'u1']),
('-order:creation-date,desc', ['u1', 'u2', 'u3']),
('sort:creation-date', ['u3', 'u2', 'u1']),
('sort:creation-time', ['u3', 'u2', 'u1']),
('-sort:creation-date', ['u1', 'u2', 'u3']),
('sort:creation-date,asc', ['u1', 'u2', 'u3']),
('sort:creation-date,desc', ['u3', 'u2', 'u1']),
('-sort:creation-date,asc', ['u3', 'u2', 'u1']),
('-sort:creation-date,desc', ['u1', 'u2', 'u3']),
])
def test_order_by_creation_time(
def test_sort_by_creation_time(
verify_unpaged, input, expected_user_names, user_factory):
user1 = user_factory(name='u1')
user2 = user_factory(name='u2')
@ -162,12 +162,12 @@ def test_order_by_creation_time(
@pytest.mark.parametrize('input,expected_user_names', [
('', ['u1', 'u2', 'u3']),
('order:last-login-date', ['u3', 'u2', 'u1']),
('order:last-login-time', ['u3', 'u2', 'u1']),
('order:login-date', ['u3', 'u2', 'u1']),
('order:login-time', ['u3', 'u2', 'u1']),
('sort:last-login-date', ['u3', 'u2', 'u1']),
('sort:last-login-time', ['u3', 'u2', 'u1']),
('sort:login-date', ['u3', 'u2', 'u1']),
('sort:login-time', ['u3', 'u2', 'u1']),
])
def test_order_by_last_login_time(
def test_sort_by_last_login_time(
verify_unpaged, input, expected_user_names, user_factory):
user1 = user_factory(name='u1')
user2 = user_factory(name='u2')
@ -178,13 +178,13 @@ def test_order_by_last_login_time(
db.session.add_all([user3, user1, user2])
verify_unpaged(input, expected_user_names)
def test_random_order(executor, user_factory):
def test_random_sort(executor, user_factory):
user1 = user_factory(name='u1')
user2 = user_factory(name='u2')
user3 = user_factory(name='u3')
db.session.add_all([user3, user1, user2])
actual_count, actual_users = executor.execute(
'order:random', page=1, page_size=100)
'sort:random', page=1, page_size=100)
actual_user_names = [u.name for u in actual_users]
assert actual_count == 3
assert len(actual_user_names) == 3
@ -204,10 +204,10 @@ def test_random_order(executor, user_factory):
('creation-date:..bad', errors.ValidationError),
('creation-date:bad..bad', errors.ValidationError),
('name:a..b', errors.SearchError),
('order:', errors.SearchError),
('order:nam', errors.SearchError),
('order:name,as', errors.SearchError),
('order:name,asc,desc', errors.SearchError),
('sort:', errors.SearchError),
('sort:nam', errors.SearchError),
('sort:name,as', errors.SearchError),
('sort:name,asc,desc', errors.SearchError),
('bad:x', errors.SearchError),
('special:unsupported', errors.SearchError),
])