diff --git a/server/szurubooru/search/configs/post_search_config.py b/server/szurubooru/search/configs/post_search_config.py index 3a578478..743f5b64 100644 --- a/server/szurubooru/search/configs/post_search_config.py +++ b/server/szurubooru/search/configs/post_search_config.py @@ -62,6 +62,22 @@ def _create_score_filter(score): return wrapper +def _create_user_filter(): + def wrapper(query, criterion, negated): + if isinstance(criterion, criteria.PlainCriterion) \ + and not criterion.value: + expr = db.Post.user_id == None # sic + if negated: + expr = ~expr + return query.filter(expr) + return search_util.create_subquery_filter( + db.Post.user_id, + db.User.user_id, + db.User.name, + search_util.create_str_filter)(query, criterion, negated) + return wrapper + + class PostSearchConfig(BaseSearchConfig): def on_search_query_parsed(self, search_query): new_special_tokens = [] @@ -138,11 +154,7 @@ class PostSearchConfig(BaseSearchConfig): lambda subquery: subquery.join(db.Tag).join(db.TagName)), 'score': search_util.create_num_filter(db.Post.score), ('uploader', 'upload', 'submit'): - search_util.create_subquery_filter( - db.Post.user_id, - db.User.user_id, - db.User.name, - search_util.create_str_filter), + _create_user_filter(), 'comment': search_util.create_subquery_filter( db.Post.post_id, db.Comment.post_id, 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 0c87f993..c4bed6d0 100644 --- a/server/szurubooru/tests/search/configs/test_post_search_config.py +++ b/server/szurubooru/tests/search/configs/test_post_search_config.py @@ -131,12 +131,15 @@ def test_filter_by_score( @pytest.mark.parametrize('input,expected_post_ids', [ + ('uploader:', [4]), ('uploader:u1', [1]), ('uploader:u3', [3]), ('uploader:u1,u3', [1, 3]), + ('upload:', [4]), ('upload:u1', [1]), ('upload:u3', [3]), ('upload:u1,u3', [1, 3]), + ('submit:', [4]), ('submit:u1', [1]), ('submit:u3', [3]), ('submit:u1,u3', [1, 3]), @@ -146,10 +149,11 @@ def test_filter_by_uploader( post1 = post_factory(id=1) post2 = post_factory(id=2) post3 = post_factory(id=3) + post4 = post_factory(id=4) post1.user = user_factory(name='u1') post2.user = user_factory(name='u2') post3.user = user_factory(name='u3') - db.session.add_all([post1, post2, post3]) + db.session.add_all([post1, post2, post3, post4]) verify_unpaged(input, expected_post_ids)