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
|
||||
|
||||
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()
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue