diff --git a/API.md b/API.md index 9c87a13b..3e139da3 100644 --- a/API.md +++ b/API.md @@ -615,6 +615,8 @@ data. | `fav-time` | alias of `fav-date` | | `feature-date` | featured at given date | | `feature-time` | alias of `feature-time` | + | `safety` | having given safety. `` can be either `safe`, `sketchy` (or `questionable`) or `unsafe`. | + | `rating` | alias of `safety` | **Sort style tokens** diff --git a/client/html/help_search_posts.tpl b/client/html/help_search_posts.tpl index 78bfb5fb..52e59d13 100644 --- a/client/html/help_search_posts.tpl +++ b/client/html/help_search_posts.tpl @@ -146,6 +146,14 @@ feature-time alias of feature-time + + safety + having given safety + + + rating + alias of safety + diff --git a/server/szurubooru/search/post_search_config.py b/server/szurubooru/search/post_search_config.py index 74466cdb..820ae9b5 100644 --- a/server/szurubooru/search/post_search_config.py +++ b/server/szurubooru/search/post_search_config.py @@ -5,7 +5,7 @@ from szurubooru.func import util from szurubooru.search.base_search_config import BaseSearchConfig def _type_transformer(value): - available_types = { + available_values = { 'image': db.Post.TYPE_IMAGE, 'animation': db.Post.TYPE_ANIMATION, 'animated': db.Post.TYPE_ANIMATION, @@ -17,10 +17,23 @@ def _type_transformer(value): 'swf': db.Post.TYPE_FLASH, } try: - return available_types[value.lower()] + return available_values[value.lower()] except KeyError: - raise errors.SearchError('Invalid type: %r. Available types: %r.' % ( - value, available_types)) + raise errors.SearchError('Invalid value: %r. Available values: %r.' % ( + value, available_values)) + +def _safety_transformer(value): + available_values = { + 'safe': db.Post.SAFETY_SAFE, + 'sketchy': db.Post.SAFETY_SKETCHY, + 'questionable': db.Post.SAFETY_SKETCHY, + 'unsafe': db.Post.SAFETY_UNSAFE, + } + try: + return available_values[value.lower()] + except KeyError: + raise errors.SearchError('Invalid value: %r. Available values: %r.' % ( + value, available_values)) class PostSearchConfig(BaseSearchConfig): def create_filter_query(self): @@ -111,6 +124,8 @@ class PostSearchConfig(BaseSearchConfig): self._create_date_filter(db.Post.last_favorite_time), ('feature-date', 'feature-time'): self._create_date_filter(db.Post.last_feature_time), + ('safety', 'rating'): + self._create_str_filter(db.Post.safety, _safety_transformer), }) @property diff --git a/server/szurubooru/tests/search/test_post_search_config.py b/server/szurubooru/tests/search/test_post_search_config.py index b476908f..a3d1749f 100644 --- a/server/szurubooru/tests/search/test_post_search_config.py +++ b/server/szurubooru/tests/search/test_post_search_config.py @@ -292,6 +292,22 @@ def test_filter_by_type(verify_unpaged, post_factory, input, expected_post_ids): db.session.add_all([post1, post2, post3, post4]) verify_unpaged(input, expected_post_ids) +@pytest.mark.parametrize('input,expected_post_ids', [ + ('safety:safe', [1]), + ('safety:sketchy', [2]), + ('safety:questionable', [2]), + ('safety:unsafe', [3]), +]) +def test_filter_by_safety(verify_unpaged, post_factory, input, expected_post_ids): + post1 = post_factory(id=1) + post2 = post_factory(id=2) + post3 = post_factory(id=3) + post1.safety = db.Post.SAFETY_SAFE + post2.safety = db.Post.SAFETY_SKETCHY + post3.safety = db.Post.SAFETY_UNSAFE + db.session.add_all([post1, post2, post3]) + verify_unpaged(input, expected_post_ids) + def test_filter_by_invalid_type(executor): with pytest.raises(errors.SearchError): actual_count, actual_posts = executor.execute(