From 82d32ba1fbdadc0941dd94a634c8cfe4ad56eb4b Mon Sep 17 00:00:00 2001 From: rr- Date: Wed, 11 May 2016 22:44:05 +0200 Subject: [PATCH] server/tags: fix removing tags related to others --- server/szurubooru/api/tag_api.py | 2 +- server/szurubooru/func/tags.py | 11 ++++++++- .../szurubooru/tests/api/test_tag_merging.py | 24 +++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/server/szurubooru/api/tag_api.py b/server/szurubooru/api/tag_api.py index a410d05..35f5ee3 100644 --- a/server/szurubooru/api/tag_api.py +++ b/server/szurubooru/api/tag_api.py @@ -69,7 +69,7 @@ class TagDetailApi(BaseApi): 'Please untag relevant posts first.') auth.verify_privilege(ctx.user, 'tags:delete') snapshots.save_entity_deletion(tag, ctx.user) - ctx.session.delete(tag) + tags.delete(tag) ctx.session.commit() tags.export_to_json() return {} diff --git a/server/szurubooru/func/tags.py b/server/szurubooru/func/tags.py index ecb3c99..dc145f3 100644 --- a/server/szurubooru/func/tags.py +++ b/server/szurubooru/func/tags.py @@ -148,12 +148,21 @@ def get_tag_siblings(tag): .limit(50) return result +def delete(source_tag): + db.session.execute( + sqlalchemy.sql.expression.delete(db.TagSuggestion) \ + .where(db.TagSuggestion.child_id == source_tag.tag_id)) + db.session.execute( + sqlalchemy.sql.expression.delete(db.TagImplication) \ + .where(db.TagImplication.child_id == source_tag.tag_id)) + db.session.delete(source_tag) + def merge_tags(source_tag, target_tag): db.session.execute( sqlalchemy.sql.expression.update(db.PostTag) \ .where(db.PostTag.tag_id == source_tag.tag_id) \ .values(tag_id=target_tag.tag_id)) - db.session.delete(source_tag) + delete(source_tag) def create_tag(names, category_name, suggestions, implications): tag = db.Tag() diff --git a/server/szurubooru/tests/api/test_tag_merging.py b/server/szurubooru/tests/api/test_tag_merging.py index 2171cae..5f02353 100644 --- a/server/szurubooru/tests/api/test_tag_merging.py +++ b/server/szurubooru/tests/api/test_tag_merging.py @@ -72,6 +72,30 @@ def test_merging_with_usages(test_ctx, fake_datetime, post_factory): assert tags.try_get_tag_by_name('source') is None assert tags.get_tag_by_name('target').post_count == 1 +def test_merging_when_related(test_ctx, fake_datetime): + source_tag = test_ctx.tag_factory(names=['source'], category_name='meta') + target_tag = test_ctx.tag_factory(names=['target'], category_name='meta') + db.session.add_all([source_tag, target_tag]) + db.session.flush() + referring_tag = test_ctx.tag_factory(names=['parent']) + referring_tag.suggestions = [source_tag] + referring_tag.implications = [source_tag] + db.session.add(referring_tag) + db.session.commit() + assert tags.try_get_tag_by_name('parent').implications != [] + assert tags.try_get_tag_by_name('parent').suggestions != [] + with fake_datetime('1997-12-01'): + result = test_ctx.api.post( + test_ctx.context_factory( + input={ + 'remove': 'source', + 'mergeTo': 'target', + }, + user=test_ctx.user_factory(rank=db.User.RANK_REGULAR))) + assert tags.try_get_tag_by_name('source') is None + assert tags.try_get_tag_by_name('parent').implications == [] + assert tags.try_get_tag_by_name('parent').suggestions == [] + @pytest.mark.parametrize('input,expected_exception', [ ({'remove': None}, tags.TagNotFoundError), ({'remove': ''}, tags.TagNotFoundError),