server/tags: prohibit deleting last tag category
This commit is contained in:
parent
58838f8bd9
commit
884747bbbd
2 changed files with 13 additions and 1 deletions
|
@ -57,11 +57,14 @@ class TagCategoryDetailApi(BaseApi):
|
||||||
if not category:
|
if not category:
|
||||||
raise tag_categories.TagCategoryNotFoundError(
|
raise tag_categories.TagCategoryNotFoundError(
|
||||||
'Tag category %r not found.' % category_name)
|
'Tag category %r not found.' % category_name)
|
||||||
|
auth.verify_privilege(ctx.user, 'tag_categories:delete')
|
||||||
|
if len(tag_categories.get_all_category_names()) == 1:
|
||||||
|
raise tag_categories.TagCategoryIsInUseError(
|
||||||
|
'Cannot delete the default category.')
|
||||||
if category.tag_count > 0:
|
if category.tag_count > 0:
|
||||||
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..')
|
||||||
auth.verify_privilege(ctx.user, 'tag_categories:delete')
|
|
||||||
ctx.session.delete(category)
|
ctx.session.delete(category)
|
||||||
ctx.session.commit()
|
ctx.session.commit()
|
||||||
tags.export_to_json()
|
tags.export_to_json()
|
||||||
|
|
|
@ -54,6 +54,15 @@ def test_trying_to_delete_used(test_ctx, tag_factory):
|
||||||
'category')
|
'category')
|
||||||
assert db.session().query(db.TagCategory).count() == 1
|
assert db.session().query(db.TagCategory).count() == 1
|
||||||
|
|
||||||
|
def test_trying_to_delete_last(test_ctx, tag_factory):
|
||||||
|
db.session().add(test_ctx.tag_category_factory(name='root'))
|
||||||
|
db.session().commit()
|
||||||
|
with pytest.raises(tag_categories.TagCategoryIsInUseError):
|
||||||
|
result = test_ctx.api.delete(
|
||||||
|
test_ctx.context_factory(
|
||||||
|
user=test_ctx.user_factory(rank='regular_user')),
|
||||||
|
'root')
|
||||||
|
|
||||||
def test_trying_to_delete_non_existing(test_ctx):
|
def test_trying_to_delete_non_existing(test_ctx):
|
||||||
with pytest.raises(tag_categories.TagCategoryNotFoundError):
|
with pytest.raises(tag_categories.TagCategoryNotFoundError):
|
||||||
test_ctx.api.delete(
|
test_ctx.api.delete(
|
||||||
|
|
Loading…
Reference in a new issue