server/tags: record tag category snapshots
This commit is contained in:
parent
2e57a0746f
commit
9a4b5a7dd3
3 changed files with 20 additions and 1 deletions
|
@ -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
|
from szurubooru.api.base_api import BaseApi
|
||||||
|
|
||||||
def _serialize_category(category):
|
def _serialize_category(category):
|
||||||
|
@ -22,6 +22,8 @@ class TagCategoryListApi(BaseApi):
|
||||||
color = ctx.get_param_as_string('color', required=True)
|
color = ctx.get_param_as_string('color', required=True)
|
||||||
category = tag_categories.create_category(name, color)
|
category = tag_categories.create_category(name, color)
|
||||||
ctx.session.add(category)
|
ctx.session.add(category)
|
||||||
|
ctx.session.flush()
|
||||||
|
snapshots.create(category, ctx.user)
|
||||||
ctx.session.commit()
|
ctx.session.commit()
|
||||||
tags.export_to_json()
|
tags.export_to_json()
|
||||||
return {'tagCategory': _serialize_category(category)}
|
return {'tagCategory': _serialize_category(category)}
|
||||||
|
@ -48,6 +50,7 @@ class TagCategoryDetailApi(BaseApi):
|
||||||
auth.verify_privilege(ctx.user, 'tag_categories:edit:color')
|
auth.verify_privilege(ctx.user, 'tag_categories:edit:color')
|
||||||
tag_categories.update_color(
|
tag_categories.update_color(
|
||||||
category, ctx.get_param_as_string('color'))
|
category, ctx.get_param_as_string('color'))
|
||||||
|
snapshots.modify(category, ctx.user)
|
||||||
ctx.session.commit()
|
ctx.session.commit()
|
||||||
tags.export_to_json()
|
tags.export_to_json()
|
||||||
return {'tagCategory': _serialize_category(category)}
|
return {'tagCategory': _serialize_category(category)}
|
||||||
|
@ -65,6 +68,7 @@ class TagCategoryDetailApi(BaseApi):
|
||||||
raise tag_categories.TagCategoryIsInUseError(
|
raise tag_categories.TagCategoryIsInUseError(
|
||||||
'Tag category has some usages and cannot be deleted. ' +
|
'Tag category has some usages and cannot be deleted. ' +
|
||||||
'Please remove this category from relevant tags first..')
|
'Please remove this category from relevant tags first..')
|
||||||
|
snapshots.delete(category, ctx.user)
|
||||||
ctx.session.delete(category)
|
ctx.session.delete(category)
|
||||||
ctx.session.commit()
|
ctx.session.commit()
|
||||||
tags.export_to_json()
|
tags.export_to_json()
|
||||||
|
|
|
@ -26,6 +26,13 @@ def test_serializing_tag(tag_factory):
|
||||||
'suggestions': ['sug1_main_name', 'sug2_main_name'],
|
'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):
|
def test_merging_modification_to_creation(tag_factory, user_factory):
|
||||||
tag = tag_factory(names=['dummy'])
|
tag = tag_factory(names=['dummy'])
|
||||||
user = user_factory()
|
user = user_factory()
|
||||||
|
|
|
@ -13,15 +13,23 @@ def get_tag_snapshot(tag):
|
||||||
ret['implications'] = sorted(rel.first_name for rel in tag.implications)
|
ret['implications'] = sorted(rel.first_name for rel in tag.implications)
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
def get_tag_category_snapshot(category):
|
||||||
|
return {
|
||||||
|
'name': category.name,
|
||||||
|
'color': category.color,
|
||||||
|
}
|
||||||
|
|
||||||
# pylint: disable=invalid-name
|
# pylint: disable=invalid-name
|
||||||
serializers = {
|
serializers = {
|
||||||
'tag': get_tag_snapshot,
|
'tag': get_tag_snapshot,
|
||||||
|
'tag_category': get_tag_category_snapshot,
|
||||||
}
|
}
|
||||||
|
|
||||||
def save(operation, entity, auth_user):
|
def save(operation, entity, auth_user):
|
||||||
table_name = entity.__table__.name
|
table_name = entity.__table__.name
|
||||||
primary_key = inspect(entity).identity
|
primary_key = inspect(entity).identity
|
||||||
assert table_name in serializers
|
assert table_name in serializers
|
||||||
|
assert primary_key is not None
|
||||||
assert len(primary_key) == 1
|
assert len(primary_key) == 1
|
||||||
primary_key = primary_key[0]
|
primary_key = primary_key[0]
|
||||||
now = datetime.datetime.now()
|
now = datetime.datetime.now()
|
||||||
|
|
Loading…
Reference in a new issue