diff --git a/server/szurubooru/api/tag_category_api.py b/server/szurubooru/api/tag_category_api.py index 0b9eb660..fe8c34a7 100644 --- a/server/szurubooru/api/tag_category_api.py +++ b/server/szurubooru/api/tag_category_api.py @@ -1,4 +1,4 @@ -from szurubooru.util import auth, tags, tag_categories +from szurubooru.util import auth, tags, tag_categories, snapshots from szurubooru.api.base_api import BaseApi def _serialize_category(category): @@ -22,6 +22,8 @@ class TagCategoryListApi(BaseApi): color = ctx.get_param_as_string('color', required=True) category = tag_categories.create_category(name, color) ctx.session.add(category) + ctx.session.flush() + snapshots.create(category, ctx.user) ctx.session.commit() tags.export_to_json() return {'tagCategory': _serialize_category(category)} @@ -48,6 +50,7 @@ class TagCategoryDetailApi(BaseApi): auth.verify_privilege(ctx.user, 'tag_categories:edit:color') tag_categories.update_color( category, ctx.get_param_as_string('color')) + snapshots.modify(category, ctx.user) ctx.session.commit() tags.export_to_json() return {'tagCategory': _serialize_category(category)} @@ -65,6 +68,7 @@ class TagCategoryDetailApi(BaseApi): raise tag_categories.TagCategoryIsInUseError( 'Tag category has some usages and cannot be deleted. ' + 'Please remove this category from relevant tags first..') + snapshots.delete(category, ctx.user) ctx.session.delete(category) ctx.session.commit() tags.export_to_json() diff --git a/server/szurubooru/tests/util/test_snapshots.py b/server/szurubooru/tests/util/test_snapshots.py index e4062ae7..526d4d05 100644 --- a/server/szurubooru/tests/util/test_snapshots.py +++ b/server/szurubooru/tests/util/test_snapshots.py @@ -26,6 +26,13 @@ def test_serializing_tag(tag_factory): 'suggestions': ['sug1_main_name', 'sug2_main_name'], } +def test_serializing_tag_category(tag_category_factory): + category = tag_category_factory(name='name', color='color') + assert snapshots.get_tag_category_snapshot(category) == { + 'name': 'name', + 'color': 'color', + } + def test_merging_modification_to_creation(tag_factory, user_factory): tag = tag_factory(names=['dummy']) user = user_factory() diff --git a/server/szurubooru/util/snapshots.py b/server/szurubooru/util/snapshots.py index f1cc5148..4e87708f 100644 --- a/server/szurubooru/util/snapshots.py +++ b/server/szurubooru/util/snapshots.py @@ -13,15 +13,23 @@ def get_tag_snapshot(tag): ret['implications'] = sorted(rel.first_name for rel in tag.implications) return ret +def get_tag_category_snapshot(category): + return { + 'name': category.name, + 'color': category.color, + } + # pylint: disable=invalid-name serializers = { 'tag': get_tag_snapshot, + 'tag_category': get_tag_category_snapshot, } def save(operation, entity, auth_user): table_name = entity.__table__.name primary_key = inspect(entity).identity assert table_name in serializers + assert primary_key is not None assert len(primary_key) == 1 primary_key = primary_key[0] now = datetime.datetime.now()