server/search: fix searching by partial range

This commit is contained in:
rr- 2016-06-02 20:48:28 +02:00
parent 802c7feca0
commit 1034362b84
2 changed files with 13 additions and 4 deletions

View file

@ -1,6 +1,6 @@
import sqlalchemy import sqlalchemy
import szurubooru.errors import szurubooru.errors
from szurubooru import db from szurubooru import db, errors
from szurubooru.func import util from szurubooru.func import util
from szurubooru.search import criteria from szurubooru.search import criteria
@ -43,8 +43,15 @@ class BaseSearchConfig(object):
elif isinstance(criterion, criteria.ArraySearchCriterion): elif isinstance(criterion, criteria.ArraySearchCriterion):
expr = column.in_(int(value) for value in criterion.values) expr = column.in_(int(value) for value in criterion.values)
elif isinstance(criterion, criteria.RangedSearchCriterion): elif isinstance(criterion, criteria.RangedSearchCriterion):
assert criterion.min_value != '' \
or criterion.max_value != ''
if criterion.min_value != '' and criterion.max_value != '':
expr = column.between( expr = column.between(
int(criterion.min_value), int(criterion.max_value)) int(criterion.min_value), int(criterion.max_value))
elif criterion.min_value != '':
expr = column >= int(criterion.min_value)
elif criterion.max_value != '':
expr = column <= int(criterion.max_value)
else: else:
assert False assert False
if criterion.negated: if criterion.negated:
@ -68,7 +75,7 @@ class BaseSearchConfig(object):
for value in criterion.values: for value in criterion.values:
expr = expr | column.ilike(transformer(value)) expr = expr | column.ilike(transformer(value))
elif isinstance(criterion, criteria.RangedSearchCriterion): elif isinstance(criterion, criteria.RangedSearchCriterion):
raise szurubooru.errors.SearchError( raise errors.SearchError(
'Composite token %r is invalid in this context.' % (criterion,)) 'Composite token %r is invalid in this context.' % (criterion,))
else: else:
assert False assert False

View file

@ -133,6 +133,8 @@ def test_filter_by_edit_time(
@pytest.mark.parametrize('input,expected_tag_names', [ @pytest.mark.parametrize('input,expected_tag_names', [
('post-count:2', ['t1']), ('post-count:2', ['t1']),
('post-count:1', ['t2']), ('post-count:1', ['t2']),
('post-count:1..', ['t1', 't2']),
('post-count:..1', ['t2']),
('usage-count:2', ['t1']), ('usage-count:2', ['t1']),
('usage-count:1', ['t2']), ('usage-count:1', ['t2']),
('usages:2', ['t1']), ('usages:2', ['t1']),