diff --git a/server/szurubooru/api/tag_api.py b/server/szurubooru/api/tag_api.py index 59bc152..cc1c47c 100644 --- a/server/szurubooru/api/tag_api.py +++ b/server/szurubooru/api/tag_api.py @@ -1,8 +1,17 @@ import datetime -from szurubooru import search +from szurubooru import db, search from szurubooru.api.base_api import BaseApi from szurubooru.func import auth, tags, snapshots +def _create_if_needed(tag_names, user): + if not tag_names: + return + auth.verify_privilege(user, 'tags:create') + _existing_tags, new_tags = tags.get_or_create_tags_by_names(tag_names) + db.session.flush() + for tag in new_tags: + snapshots.save_entity_creation(tag, user) + class TagListApi(BaseApi): def __init__(self): super().__init__() @@ -23,6 +32,9 @@ class TagListApi(BaseApi): implications = ctx.get_param_as_list( 'implications', required=False, default=[]) + _create_if_needed(suggestions, ctx.user) + _create_if_needed(implications, ctx.user) + tag = tags.create_tag(names, category, suggestions, implications) ctx.session.add(tag) ctx.session.flush() @@ -48,12 +60,14 @@ class TagDetailApi(BaseApi): tag, ctx.get_param_as_string('category')) if ctx.has_param('suggestions'): auth.verify_privilege(ctx.user, 'tags:edit:suggestions') - tags.update_tag_suggestions( - tag, ctx.get_param_as_list('suggestions')) + suggestions = ctx.get_param_as_list('suggestions') + _create_if_needed(suggestions, ctx.user) + tags.update_tag_suggestions(tag, suggestions) if ctx.has_param('implications'): auth.verify_privilege(ctx.user, 'tags:edit:implications') - tags.update_tag_implications( - tag, ctx.get_param_as_list('implications')) + implications = ctx.get_param_as_list('implications') + _create_if_needed(implications, ctx.user) + tags.update_tag_implications(tag, implications) tag.last_edit_time = datetime.datetime.now() ctx.session.flush() snapshots.save_entity_modification(tag, ctx.user) diff --git a/server/szurubooru/func/tags.py b/server/szurubooru/func/tags.py index 3fbf26c..3b3cd7b 100644 --- a/server/szurubooru/func/tags.py +++ b/server/szurubooru/func/tags.py @@ -214,13 +214,9 @@ def update_tag_names(tag, names): def update_tag_implications(tag, relations): if _check_name_intersection(_get_plain_names(tag), relations): raise InvalidTagRelationError('Tag cannot imply itself.') - related_tags, new_tags = get_or_create_tags_by_names(relations) - db.session.flush() - tag.implications = related_tags + new_tags + tag.implications = get_tags_by_names(relations) def update_tag_suggestions(tag, relations): if _check_name_intersection(_get_plain_names(tag), relations): raise InvalidTagRelationError('Tag cannot suggest itself.') - related_tags, new_tags = get_or_create_tags_by_names(relations) - db.session.flush() - tag.suggestions = related_tags + new_tags + tag.suggestions = get_tags_by_names(relations) diff --git a/server/szurubooru/tests/api/test_tag_creating.py b/server/szurubooru/tests/api/test_tag_creating.py index 78f8d87..e758f91 100644 --- a/server/szurubooru/tests/api/test_tag_creating.py +++ b/server/szurubooru/tests/api/test_tag_creating.py @@ -249,11 +249,12 @@ def test_reusing_suggestions_and_implications(test_ctx): } ]) def test_tag_trying_to_relate_to_itself(test_ctx, input): - with pytest.raises(tags.InvalidTagRelationError): + with pytest.raises(tags.TagAlreadyExistsError): test_ctx.api.post( test_ctx.context_factory( input=input, user=test_ctx.user_factory(rank=db.User.RANK_REGULAR))) + db.session.rollback() assert tags.try_get_tag_by_name('tag') is None def test_trying_to_create_tag_without_privileges(test_ctx): diff --git a/server/szurubooru/tests/api/test_tag_updating.py b/server/szurubooru/tests/api/test_tag_updating.py index cde09ff..d94a033 100644 --- a/server/szurubooru/tests/api/test_tag_updating.py +++ b/server/szurubooru/tests/api/test_tag_updating.py @@ -16,6 +16,7 @@ def test_ctx( 'tag_name_regex': '^[^!]*$', 'tag_category_name_regex': '^[^!]*$', 'privileges': { + 'tags:create': db.User.RANK_REGULAR, 'tags:edit:names': db.User.RANK_REGULAR, 'tags:edit:category': db.User.RANK_REGULAR, 'tags:edit:suggestions': db.User.RANK_REGULAR,