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 | | `suggestion-count` | with given number of suggestions |
| `implication-count` | with given number of implications | | `implication-count` | with given number of implications |
**Order tokens** **Sort style tokens**
| `<value>` | Description | | `<value>` | Description |
| ------------------- | ---------------------------- | | ------------------- | ---------------------------- |
@ -637,7 +637,7 @@ data.
| `login-date` | alias of `last-login-date` | | `login-date` | alias of `last-login-date` |
| `login-time` | alias of `last-login-date` | | `login-time` | alias of `last-login-date` |
**Order tokens** **Sort style tokens**
| `<value>` | Description | | `<value>` | Description |
| ----------------- | -------------------------- | | ----------------- | -------------------------- |
@ -907,7 +907,7 @@ data.
| `operation` | `changed`, `created` or `deleted` | | `operation` | `changed`, `created` or `deleted` |
| `user` | name of the user that created given snapshot | | `user` | name of the user that created given snapshot |
**Order tokens** **Sort style tokens**
None. The snapshots are always sorted by creation time. None. The snapshots are always sorted by creation time.
@ -1095,7 +1095,7 @@ be of following form:
| ----------------- | ----------------- | ------------------------------------------ | | ----------------- | ----------------- | ------------------------------------------ |
| `<value>` | anonymous tokens | basic filters | | `<value>` | anonymous tokens | basic filters |
| `<key>:<value>` | named tokens | advanced 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 | | `special:<value>` | special tokens | filters usually tied to the logged in user |
Most of anonymous and named tokens support ranged and composite values that 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> <td>advanced filters</td>
</tr> </tr>
<tr> <tr>
<td><code>order:&lt;style&gt;</code></td> <td><code>sort:&lt;style&gt;</code></td>
<td>order tokens</td> <td>sort style tokens</td>
<td>sort results</td> <td>sort the results</td>
</tr> </tr>
<tr> <tr>
<td><code>special:&lt;value&gt;</code></td> <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>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 <code>,desc</code> to control the sort direction, which can be also controlled
by negating the whole token.</p> by negating the whole token.</p>

View File

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

View File

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

View File

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

View File

@ -1,5 +1,6 @@
[basic] [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] [variables]
dummy-variables-rgx=_|dummy dummy-variables-rgx=_|dummy

View File

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

View File

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

View File

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

View File

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

View File

@ -15,10 +15,6 @@ class UserSearchConfig(BaseSearchConfig):
def anonymous_filter(self): def anonymous_filter(self):
return self._create_str_filter(db.User.name) return self._create_str_filter(db.User.name)
@property
def special_filters(self):
return {}
@property @property
def named_filters(self): def named_filters(self):
return { return {
@ -32,14 +28,14 @@ class UserSearchConfig(BaseSearchConfig):
} }
@property @property
def order_columns(self): def sort_columns(self):
return { return {
'random': (None, func.random()), 'random': (func.random(), None),
'name': (db.User.name, self.ORDER_ASC), 'name': (db.User.name, self.SORT_ASC),
'creation-date': (db.User.creation_time, self.ORDER_DESC), 'creation-date': (db.User.creation_time, self.SORT_DESC),
'creation-time': (db.User.creation_time, self.ORDER_DESC), 'creation-time': (db.User.creation_time, self.SORT_DESC),
'last-login-date': (db.User.last_login_time, self.ORDER_DESC), 'last-login-date': (db.User.last_login_time, self.SORT_DESC),
'last-login-time': (db.User.last_login_time, self.ORDER_DESC), 'last-login-time': (db.User.last_login_time, self.SORT_DESC),
'login-date': (db.User.last_login_time, self.ORDER_DESC), 'login-date': (db.User.last_login_time, self.SORT_DESC),
'login-time': (db.User.last_login_time, self.ORDER_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', [ @pytest.mark.parametrize('input,expected_tag_names', [
('', ['t1', 't2']), ('', ['t1', 't2']),
('order:name', ['t1', 't2']), ('sort:name', ['t1', 't2']),
('-order:name', ['t2', 't1']), ('-sort:name', ['t2', 't1']),
('order:name,asc', ['t1', 't2']), ('sort:name,asc', ['t1', 't2']),
('order:name,desc', ['t2', 't1']), ('sort:name,desc', ['t2', 't1']),
('-order:name,asc', ['t2', 't1']), ('-sort:name,asc', ['t2', 't1']),
('-order:name,desc', ['t1', 't2']), ('-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=['t2']))
db.session.add(tag_factory(names=['t1'])) db.session.add(tag_factory(names=['t1']))
verify_unpaged(input, expected_tag_names) verify_unpaged(input, expected_tag_names)
@pytest.mark.parametrize('input,expected_user_names', [ @pytest.mark.parametrize('input,expected_user_names', [
('', ['t1', 't2', 't3']), ('', ['t1', 't2', 't3']),
('order:creation-date', ['t3', 't2', 't1']), ('sort:creation-date', ['t3', 't2', 't1']),
('order:creation-time', ['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): verify_unpaged, tag_factory, input, expected_user_names):
tag1 = tag_factory(names=['t1']) tag1 = tag_factory(names=['t1'])
tag2 = tag_factory(names=['t2']) tag2 = tag_factory(names=['t2'])
@ -119,9 +119,9 @@ def test_order_by_creation_time(
verify_unpaged(input, expected_user_names) verify_unpaged(input, expected_user_names)
@pytest.mark.parametrize('input,expected_tag_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): verify_unpaged, tag_factory, input, expected_tag_names):
sug1 = tag_factory(names=['sug1']) sug1 = tag_factory(names=['sug1'])
sug2 = tag_factory(names=['sug2']) sug2 = tag_factory(names=['sug2'])
@ -136,9 +136,9 @@ def test_order_by_suggestion_count(
verify_unpaged(input, expected_tag_names) verify_unpaged(input, expected_tag_names)
@pytest.mark.parametrize('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): verify_unpaged, tag_factory, input, expected_tag_names):
sug1 = tag_factory(names=['sug1']) sug1 = tag_factory(names=['sug1'])
sug2 = tag_factory(names=['sug2']) sug2 = tag_factory(names=['sug2'])

View File

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