aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHunternif2019-04-30 03:38:03 +0700
committerHunternif2019-04-30 04:05:08 +0700
commit25ea3bd0a25b32c670491ecb2e1590a6086eb2fc (patch)
tree2843df5ceeefc3ade0cc1c2b1907a2fdf6bef15d
parent043d873117a5a9e44484ec42a88cee85d5371790 (diff)
server: metric presence named filter for posts
-rw-r--r--server/szurubooru/search/configs/post_search_config.py29
-rw-r--r--server/szurubooru/tests/search/configs/test_post_search_config.py16
2 files changed, 39 insertions, 6 deletions
diff --git a/server/szurubooru/search/configs/post_search_config.py b/server/szurubooru/search/configs/post_search_config.py
index 9f31497..8596bce 100644
--- a/server/szurubooru/search/configs/post_search_config.py
+++ b/server/szurubooru/search/configs/post_search_config.py
@@ -104,7 +104,7 @@ def _note_filter(
search_util.create_str_filter)(query, criterion, negated)
-def _create_metric_filter(name: str):
+def _create_metric_num_filter(name: str):
def wrapper(query: SaQuery,
criterion: Optional[criteria.BaseCriterion],
negated: bool) -> SaQuery:
@@ -125,6 +125,26 @@ def _create_metric_filter(name: str):
return wrapper
+def _metric_presence_filter(
+ query: SaQuery,
+ criterion: Optional[criteria.BaseCriterion],
+ negated: bool) -> SaQuery:
+ assert criterion
+ t = sa.orm.aliased(model.TagName)
+ tag_name_filter = search_util.apply_str_criterion_to_column(
+ t.name, criterion)
+ pm = sa.orm.aliased(model.PostMetric)
+ subquery = (
+ db.session.query(pm.post_id)
+ .join(t, t.tag_id == pm.tag_id)
+ .filter(tag_name_filter)
+ .subquery())
+ post_filter = model.Post.post_id.in_(subquery)
+ if negated:
+ post_filter = ~post_filter
+ return query.filter(post_filter)
+
+
def _create_metric_sort_column(metric_name: str):
t = sa.orm.aliased(model.TagName)
pm = sa.orm.aliased(model.PostMetric)
@@ -217,7 +237,7 @@ class PostSearchConfig(BaseSearchConfig):
@property
def named_filters(self) -> Dict[str, Filter]:
- filters = {'metric-' + name: _create_metric_filter(name)
+ filters = {'metric-' + name: _create_metric_num_filter(name)
for name in self.all_metric_names}
filters.update(util.unalias_dict([
(
@@ -237,6 +257,11 @@ class PostSearchConfig(BaseSearchConfig):
),
(
+ ['metric'],
+ _metric_presence_filter
+ ),
+
+ (
['score'],
search_util.create_num_filter(model.Post.score)
),
diff --git a/server/szurubooru/tests/search/configs/test_post_search_config.py b/server/szurubooru/tests/search/configs/test_post_search_config.py
index 3be7d98..dd9b98f 100644
--- a/server/szurubooru/tests/search/configs/test_post_search_config.py
+++ b/server/szurubooru/tests/search/configs/test_post_search_config.py
@@ -766,12 +766,15 @@ def test_tumbleweed(
('sort:id,asc c metric-a:3..', [3]),
('sort:id,asc metric-b:..2', [1, 2]),
('sort:id,asc metric-b:..1.9', [1]),
- ('sort:metric-a', [1, 2, 3]),
- ('sort:metric-a,desc', [3, 2, 1]),
+ ('sort:metric-a', [1, 2, 3, 5, 4]),
+ ('sort:metric-a,desc', [3, 2, 1, 5, 4]),
('metric-a:1..3 metric-b:1..3 sort:metric-b,desc', [2, 1]),
('metric-a:1..3 sort:metric-b,desc', [2, 1, 3]),
('metric-a:2..3 metric-b:1..3 sort:metric-b,desc', [2]),
('metric-a:2..3 sort:metric-b,desc', [2, 3]),
+ ('sort:id,asc metric:a', [1, 2, 3]),
+ ('sort:id,asc -metric:a', [4, 5]),
+ ('sort:id,asc metric:a -metric:b', [3]),
])
def test_metrics(
input,
@@ -780,6 +783,7 @@ def test_metrics(
tag_factory,
metric_factory,
post_metric_factory,
+ post_metric_range_factory,
verify_unpaged):
tag_a = tag_factory(names=['a'])
tag_b = tag_factory(names=['b'])
@@ -787,6 +791,8 @@ def test_metrics(
post1 = post_factory(id=1, tags=[tag_a, tag_b, tag_c])
post2 = post_factory(id=2, tags=[tag_a, tag_b, tag_c])
post3 = post_factory(id=3, tags=[tag_a, tag_b, tag_c])
+ post4 = post_factory(id=4, tags=[tag_a, tag_b, tag_c])
+ post5 = post_factory(id=5, tags=[tag_a, tag_b, tag_c])
metric_a = metric_factory(tag=tag_a)
metric_b = metric_factory(tag=tag_b)
metric_c = metric_factory(tag=tag_c)
@@ -797,10 +803,12 @@ def test_metrics(
b2 = post_metric_factory(post=post2, metric=metric_b, value=2)
a3 = post_metric_factory(post=post3, metric=metric_a, value=3)
c3 = post_metric_factory(post=post3, metric=metric_c, value=3)
+ r_a4 = post_metric_range_factory(post=post4, metric=metric_a,
+ low=1.5, high=2.5)
db.session.add_all([tag_a, tag_b, tag_c,
- post1, post2, post3,
+ post1, post2, post3, post4, post5,
metric_a, metric_b, metric_c,
- a1, b1, c1, a2, b2, a3, c3])
+ a1, b1, c1, a2, b2, a3, c3, r_a4])
db.session.flush()
verify_unpaged(input, expected_post_ids, True)

© 2015 - 2026 Jakob L. Kreuze