server/search: add safety search for posts

This commit is contained in:
rr- 2016-06-02 09:11:48 +02:00
parent 74fba05302
commit 802c7feca0
4 changed files with 45 additions and 4 deletions

2
API.md
View file

@ -615,6 +615,8 @@ data.
| `fav-time` | alias of `fav-date` | | `fav-time` | alias of `fav-date` |
| `feature-date` | featured at given date | | `feature-date` | featured at given date |
| `feature-time` | alias of `feature-time` | | `feature-time` | alias of `feature-time` |
| `safety` | having given safety. `<value>` can be either `safe`, `sketchy` (or `questionable`) or `unsafe`. |
| `rating` | alias of `safety` |
**Sort style tokens** **Sort style tokens**

View file

@ -146,6 +146,14 @@
<td><code>feature-time</code></td> <td><code>feature-time</code></td>
<td>alias of <code>feature-time</code></td> <td>alias of <code>feature-time</code></td>
</tr> </tr>
<tr>
<td><code>safety</code></td>
<td>having given safety</td>
</tr>
<tr>
<td><code>rating</code></td>
<td>alias of <code>safety</code></td>
</tr>
</tbody> </tbody>
</table> </table>

View file

@ -5,7 +5,7 @@ from szurubooru.func import util
from szurubooru.search.base_search_config import BaseSearchConfig from szurubooru.search.base_search_config import BaseSearchConfig
def _type_transformer(value): def _type_transformer(value):
available_types = { available_values = {
'image': db.Post.TYPE_IMAGE, 'image': db.Post.TYPE_IMAGE,
'animation': db.Post.TYPE_ANIMATION, 'animation': db.Post.TYPE_ANIMATION,
'animated': db.Post.TYPE_ANIMATION, 'animated': db.Post.TYPE_ANIMATION,
@ -17,10 +17,23 @@ def _type_transformer(value):
'swf': db.Post.TYPE_FLASH, 'swf': db.Post.TYPE_FLASH,
} }
try: try:
return available_types[value.lower()] return available_values[value.lower()]
except KeyError: except KeyError:
raise errors.SearchError('Invalid type: %r. Available types: %r.' % ( raise errors.SearchError('Invalid value: %r. Available values: %r.' % (
value, available_types)) 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): class PostSearchConfig(BaseSearchConfig):
def create_filter_query(self): def create_filter_query(self):
@ -111,6 +124,8 @@ class PostSearchConfig(BaseSearchConfig):
self._create_date_filter(db.Post.last_favorite_time), self._create_date_filter(db.Post.last_favorite_time),
('feature-date', 'feature-time'): ('feature-date', 'feature-time'):
self._create_date_filter(db.Post.last_feature_time), self._create_date_filter(db.Post.last_feature_time),
('safety', 'rating'):
self._create_str_filter(db.Post.safety, _safety_transformer),
}) })
@property @property

View file

@ -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]) db.session.add_all([post1, post2, post3, post4])
verify_unpaged(input, expected_post_ids) 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): def test_filter_by_invalid_type(executor):
with pytest.raises(errors.SearchError): with pytest.raises(errors.SearchError):
actual_count, actual_posts = executor.execute( actual_count, actual_posts = executor.execute(