aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIlya Tetin2019-04-19 16:49:24 +0700
committerIlya Tetin2019-04-19 16:49:24 +0700
commit09d55f35394b84a983c7cf06af1bcb6802c2d5f8 (patch)
treec0b2825c3e1e3675d00ed3101c4fa99b6f98f8a2
parent77de3c1ca3a935567b4afeaae9da9835366ff540 (diff)
server: serialize post metrics and metric ranges in post
-rw-r--r--server/szurubooru/func/metrics.py29
-rw-r--r--server/szurubooru/func/posts.py22
-rw-r--r--server/szurubooru/tests/conftest.py37
-rw-r--r--server/szurubooru/tests/func/test_metrics.py14
-rw-r--r--server/szurubooru/tests/func/test_posts.py45
5 files changed, 125 insertions, 22 deletions
diff --git a/server/szurubooru/func/metrics.py b/server/szurubooru/func/metrics.py
index d2ef8e6..ab5a0c4 100644
--- a/server/szurubooru/func/metrics.py
+++ b/server/szurubooru/func/metrics.py
@@ -57,6 +57,27 @@ class PostMetricSerializer(serialization.BaseSerializer):
return self.post_metric.value
+class PostMetricRangeSerializer(serialization.BaseSerializer):
+ def __init__(self, post_metric_range: model.PostMetricRange):
+ self.post_metric_range = post_metric_range
+
+ def _serializers(self) -> Dict[str, Callable[[], Any]]:
+ return {
+ 'tag_name': self.serialize_tag_name,
+ 'low': self.serialize_low,
+ 'high': self.serialize_high,
+ }
+
+ def serialize_tag_name(self) -> Any:
+ return self.post_metric_range.metric.tag_name
+
+ def serialize_low(self) -> Any:
+ return self.post_metric_range.low
+
+ def serialize_high(self) -> Any:
+ return self.post_metric_range.high
+
+
def serialize_metric(
metric: model.Metric,
options: List[str] = []) -> Optional[rest.Response]:
@@ -73,6 +94,14 @@ def serialize_post_metric(
return PostMetricSerializer(post_metric).serialize(options)
+def serialize_post_metric_range(
+ post_metric_range: model.PostMetricRange,
+ options: List[str] = []) -> Optional[rest.Response]:
+ if not post_metric_range:
+ return None
+ return PostMetricRangeSerializer(post_metric_range).serialize(options)
+
+
def get_all_metrics() -> List[model.Metric]:
return db.session.query(model.Metric).all()
diff --git a/server/szurubooru/func/posts.py b/server/szurubooru/func/posts.py
index 9a5307b..edd911e 100644
--- a/server/szurubooru/func/posts.py
+++ b/server/szurubooru/func/posts.py
@@ -4,7 +4,7 @@ from datetime import datetime
import sqlalchemy as sa
from szurubooru import config, db, model, errors, rest
from szurubooru.func import (
- users, scores, comments, tags, util,
+ users, scores, comments, tags, metrics, util,
mime, images, files, image_hash, serialization, snapshots)
@@ -177,6 +177,8 @@ class PostSerializer(serialization.BaseSerializer):
'hasCustomThumbnail': self.serialize_has_custom_thumbnail,
'notes': self.serialize_notes,
'comments': self.serialize_comments,
+ 'metrics': self.serialize_metrics,
+ 'metricRanges': self.serialize_metric_ranges,
}
def serialize_id(self) -> Any:
@@ -230,6 +232,10 @@ class PostSerializer(serialization.BaseSerializer):
'names': [name.name for name in tag.names],
'category': tag.category.name,
'usages': tag.post_count,
+ 'metric': {
+ 'min': tag.metric.min,
+ 'max': tag.metric.max
+ } if tag.metric else None,
}
for tag in tags.sort_tags(self.post.tags)]
@@ -300,6 +306,20 @@ class PostSerializer(serialization.BaseSerializer):
self.post.comments,
key=lambda comment: comment.creation_time)]
+ def serialize_metrics(self) -> Any:
+ return [
+ metrics.serialize_post_metric(metric)
+ for metric in sorted(
+ self.post.metrics,
+ key=lambda metric: metric.metric.tag_name)]
+
+ def serialize_metric_ranges(self) -> Any:
+ return [
+ metrics.serialize_post_metric_range(metric_range)
+ for metric_range in sorted(
+ self.post.metric_ranges,
+ key=lambda metric_range: metric_range.metric.tag_name)]
+
def serialize_post(
post: Optional[model.Post],
diff --git a/server/szurubooru/tests/conftest.py b/server/szurubooru/tests/conftest.py
index b8af456..084e6e5 100644
--- a/server/szurubooru/tests/conftest.py
+++ b/server/szurubooru/tests/conftest.py
@@ -168,7 +168,7 @@ def tag_category_factory():
@pytest.fixture
def tag_factory():
- def factory(names=None, category=None):
+ def factory(names=None, category=None, metric=None):
if not category:
category = model.TagCategory(get_unique_name())
db.session.add(category)
@@ -178,6 +178,8 @@ def tag_factory():
tag.names.append(model.TagName(name, i))
tag.category = category
tag.creation_time = datetime(1996, 1, 1)
+ if metric:
+ tag.metric = metric
return tag
return factory
@@ -254,35 +256,44 @@ def post_favorite_factory(user_factory, post_factory):
@pytest.fixture
def metric_factory(tag_factory):
def factory(tag=None, min=0, max=10):
- if tag is None:
+ if not tag:
tag = tag_factory()
return model.Metric(tag=tag, min=min, max=max)
return factory
@pytest.fixture
-def post_metric_factory(post_factory, metric_factory):
- def factory(post=None, metric=None, value=None):
- if post is None:
+def post_metric_factory(post_factory, tag_factory, metric_factory):
+ def factory(post=None, metric=None, value=None, tag=None, tag_name=None):
+ if not post:
post = post_factory()
- if metric is None:
+ if tag_name:
+ tag = tag_factory(names=[tag_name])
+ if tag:
+ metric = metric_factory(tag=tag)
+ elif not metric:
metric = metric_factory()
- if value is None:
+ if not value:
value = (metric.min + metric.max)/2
return model.PostMetric(post=post, metric=metric, value=value)
return factory
@pytest.fixture
-def post_metric_range_factory(post_factory, metric_factory):
- def factory(post=None, metric=None, low=None, high=None):
- if post is None:
+def post_metric_range_factory(post_factory, tag_factory, metric_factory):
+ def factory(post=None, metric=None, low=None, high=None, tag=None,
+ tag_name=None):
+ if not post:
post = post_factory()
- if metric is None:
+ if tag_name:
+ tag = tag_factory(names=[tag_name])
+ if tag:
+ metric = metric_factory(tag=tag)
+ elif not metric:
metric = metric_factory()
- if low is None:
+ if not low:
low = metric.min
- if high is None:
+ if not high:
high = metric.max
return model.PostMetricRange(
post=post, metric=metric, low=low, high=high)
diff --git a/server/szurubooru/tests/func/test_metrics.py b/server/szurubooru/tests/func/test_metrics.py
index 997d726..0db76c4 100644
--- a/server/szurubooru/tests/func/test_metrics.py
+++ b/server/szurubooru/tests/func/test_metrics.py
@@ -26,6 +26,20 @@ def test_serialize_post_metric(tag_factory, metric_factory):
}
+def test_serialize_post_metric_range(tag_factory, metric_factory):
+ tag = tag_factory(names=['mytag'])
+ metric = metric_factory(tag)
+ db.session.add_all([tag, metric])
+ db.session.flush()
+ post_metric_range = model.PostMetricRange(metric=metric, low=-1.2, high=3.4)
+ result = metrics.serialize_post_metric_range(post_metric_range)
+ assert result == {
+ 'tag_name': 'mytag',
+ 'low': -1.2,
+ 'high': 3.4
+ }
+
+
def test_try_get_post_metric(
post_factory, metric_factory, post_metric_factory):
post = post_factory()
diff --git a/server/szurubooru/tests/func/test_posts.py b/server/szurubooru/tests/func/test_posts.py
index d0c27ba..d8e47d6 100644
--- a/server/szurubooru/tests/func/test_posts.py
+++ b/server/szurubooru/tests/func/test_posts.py
@@ -79,6 +79,9 @@ def test_serialize_post(
comment_factory,
tag_factory,
tag_category_factory,
+ metric_factory,
+ post_metric_factory,
+ post_metric_range_factory,
config_injector):
config_injector({'data_url': 'http://example.com/', 'secret': 'test'})
with patch('szurubooru.func.comments.serialize_comment'), \
@@ -95,13 +98,20 @@ def test_serialize_post(
post.post_id = 1
post.creation_time = datetime(1997, 1, 1)
post.last_edit_time = datetime(1998, 1, 1)
- post.tags = [
- tag_factory(
- names=['tag1', 'tag2'],
- category=tag_category_factory('test-cat1')),
- tag_factory(
- names=['tag3'],
- category=tag_category_factory('test-cat2'))
+ tag1 = tag_factory(
+ names=['tag1', 'tag2'],
+ category=tag_category_factory('test-cat1'))
+ tag1.metric = metric_factory(tag=tag1, min=-2.5, max=2.5)
+ tag3 = tag_factory(
+ names=['tag3'],
+ category=tag_category_factory('test-cat2'))
+ post.tags = [tag1, tag3]
+ post.metrics = [
+ post_metric_factory(post=post, metric=tag1.metric, value=-1.2)
+ ]
+ post.metric_ranges = [
+ post_metric_range_factory(post=post, metric=tag1.metric,
+ low=2, high=3)
]
post.safety = model.Post.SAFETY_SAFE
post.source = '4gag'
@@ -173,12 +183,18 @@ def test_serialize_post(
'tags': [
{
'names': ['tag1', 'tag2'],
- 'category': 'test-cat1', 'usages': 1,
+ 'category': 'test-cat1',
+ 'usages': 1,
+ 'metric': {
+ 'min': -2.5,
+ 'max': 2.5
+ }
},
{
'names': ['tag3'],
'category': 'test-cat2',
'usages': 1,
+ 'metric': None
},
],
'relations': [],
@@ -198,6 +214,19 @@ def test_serialize_post(
'hasCustomThumbnail': True,
'mimeType': 'image/jpeg',
'comments': ['commenter1', 'commenter2'],
+ 'metrics': [
+ {
+ 'tag_name': 'tag1',
+ 'value': -1.2
+ }
+ ],
+ 'metricRanges': [
+ {
+ 'tag_name': 'tag1',
+ 'low': 2,
+ 'high': 3
+ }
+ ]
}

© 2015 - 2026 Jakob L. Kreuze