diff options
| author | Hunternif <hunternif@gmail.com> | 2019-04-23 02:08:05 +0700 |
|---|---|---|
| committer | Hunternif <hunternif@gmail.com> | 2019-04-23 02:08:05 +0700 |
| commit | 7ae11f483ff1e1fc3cf44775b07d666a7fe28a7f (patch) | |
| tree | 0d899410f8a0fb4e38ad65e9b01aad7afeb2f2a8 /server | |
| parent | f020db29d17e2c34d1776132532828553277639a (diff) | |
server: delete metrics
Diffstat (limited to 'server')
| -rw-r--r-- | server/config.yaml.dist | 1 | ||||
| -rw-r--r-- | server/szurubooru/api/metric_api.py | 19 | ||||
| -rw-r--r-- | server/szurubooru/func/metrics.py | 20 | ||||
| -rw-r--r-- | server/szurubooru/tests/func/test_metrics.py | 20 |
4 files changed, 59 insertions, 1 deletions
diff --git a/server/config.yaml.dist b/server/config.yaml.dist index 2e6ab0e..7440777 100644 --- a/server/config.yaml.dist +++ b/server/config.yaml.dist @@ -121,6 +121,7 @@ privileges: 'metrics:edit:bounds': power 'metrics:edit:posts': regular 'metrics:list': regular + 'metrics:delete': moderator 'comments:create': regular 'comments:delete:any': moderator diff --git a/server/szurubooru/api/metric_api.py b/server/szurubooru/api/metric_api.py index 12babbd..15f48d7 100644 --- a/server/szurubooru/api/metric_api.py +++ b/server/szurubooru/api/metric_api.py @@ -1,6 +1,8 @@ from typing import Optional, List, Dict from szurubooru import db, model, search, rest -from szurubooru.func import auth, metrics, snapshots, serialization, tags +from szurubooru.func import ( + auth, metrics, snapshots, serialization, tags, versions +) _search_executor_config = search.configs.PostMetricSearchConfig() @@ -21,6 +23,10 @@ def _serialize_post_metric( ) +def _get_metric(params: Dict[str, str]) -> model.Tag: + return metrics.get_metric_by_tag_name(params['tag_name']) + + @rest.routes.get('/metrics/?') def get_metrics( ctx: rest.Context, params: Dict[str, str] = {}) -> rest.Response: @@ -47,6 +53,17 @@ def create_metric( return _serialize_metric(ctx, metric) +@rest.routes.delete('/metric/(?P<tag_name>.+)') +def delete_metric(ctx: rest.Context, params: Dict[str, str]) -> rest.Response: + metric = _get_metric(params) + versions.verify_version(metric, ctx) + auth.verify_privilege(ctx.user, 'metrics:delete') + # snapshots.delete(metric, ctx.user) + metrics.delete_metric(metric) + ctx.session.commit() + return {} + + @rest.routes.get('/post-metrics/?') def get_post_metrics( ctx: rest.Context, params: Dict[str, str] = {}) -> rest.Response: diff --git a/server/szurubooru/func/metrics.py b/server/szurubooru/func/metrics.py index b7a6c01..5fa4e0c 100644 --- a/server/szurubooru/func/metrics.py +++ b/server/szurubooru/func/metrics.py @@ -84,6 +84,21 @@ def serialize_post_metric_range( return PostMetricRangeSerializer(post_metric_range).serialize(options) +def try_get_metric_by_tag_name(tag_name: str) -> Optional[model.Metric]: + return ( + db.session + .query(model.Metric) + .filter(sa.func.lower(model.Metric.tag_name) == tag_name.lower()) + .one_or_none()) + + +def get_metric_by_tag_name(tag_name: str) -> model.Metric: + metric = try_get_metric_by_tag_name(tag_name) + if not metric: + raise MetricDoesNotExistsError('Metric %r not found.' % tag_name) + return metric + + def get_all_metrics() -> List[model.Metric]: return db.session.query(model.Metric).all() @@ -246,3 +261,8 @@ def update_or_create_post_metric_ranges( post_metric_range = update_or_create_post_metric_range( post, tag.metric, low, high) post.metric_ranges.append(post_metric_range) + + +def delete_metric(metric: model.Metric) -> None: + assert metric + db.session.delete(metric) diff --git a/server/szurubooru/tests/func/test_metrics.py b/server/szurubooru/tests/func/test_metrics.py index b6f334a..e43757d 100644 --- a/server/szurubooru/tests/func/test_metrics.py +++ b/server/szurubooru/tests/func/test_metrics.py @@ -45,6 +45,15 @@ def test_serialize_post_metric_range(post_factory, tag_factory, metric_factory): } +def test_try_get_metric_by_tag_name(tag_factory, metric_factory): + tag = tag_factory(names=['mytag']) + metric = metric_factory(tag) + db.session.add_all([tag, metric]) + db.session.flush() + assert metrics.try_get_metric_by_tag_name('unknown') is None + assert metrics.try_get_metric_by_tag_name('mytag') is metric + + def test_try_get_post_metric( post_factory, metric_factory, post_metric_factory): metric1 = metric_factory() @@ -419,3 +428,14 @@ def test_update_or_create_post_metric_ranges_with_trim( assert post.metric_ranges[0].metric == metric2 assert post.metric_ranges[0].low == 3 assert post.metric_ranges[0].high == 4 + + +def test_delete_metric(metric_factory): + metric1 = metric_factory() + metric2 = metric_factory() + db.session.add_all([metric1, metric2]) + db.session.flush() + assert db.session.query(model.Metric).count() == 2 + metrics.delete_metric(metric2) + db.session.flush() + assert db.session.query(model.Metric).count() == 1 |