server/tags: allow deleting used tags
This commit is contained in:
parent
f63851e2cf
commit
f40e41ae8b
3 changed files with 8 additions and 12 deletions
1
API.md
1
API.md
|
@ -486,7 +486,6 @@ data.
|
||||||
- **Errors**
|
- **Errors**
|
||||||
|
|
||||||
- the tag does not exist
|
- the tag does not exist
|
||||||
- the tag is used by some posts
|
|
||||||
- privileges are too low
|
- privileges are too low
|
||||||
|
|
||||||
- **Description**
|
- **Description**
|
||||||
|
|
|
@ -90,10 +90,6 @@ class TagDetailApi(BaseApi):
|
||||||
|
|
||||||
def delete(self, ctx, tag_name):
|
def delete(self, ctx, tag_name):
|
||||||
tag = tags.get_tag_by_name(tag_name)
|
tag = tags.get_tag_by_name(tag_name)
|
||||||
if tag.post_count > 0:
|
|
||||||
raise tags.TagIsInUseError(
|
|
||||||
'Tag has some usages and cannot be deleted. ' +
|
|
||||||
'Please untag relevant posts first.')
|
|
||||||
auth.verify_privilege(ctx.user, 'tags:delete')
|
auth.verify_privilege(ctx.user, 'tags:delete')
|
||||||
snapshots.save_entity_deletion(tag, ctx.user)
|
snapshots.save_entity_deletion(tag, ctx.user)
|
||||||
tags.delete(tag)
|
tags.delete(tag)
|
||||||
|
|
|
@ -31,18 +31,19 @@ def test_deleting(test_ctx):
|
||||||
assert db.session.query(db.Tag).count() == 0
|
assert db.session.query(db.Tag).count() == 0
|
||||||
assert os.path.exists(os.path.join(config.config['data_dir'], 'tags.json'))
|
assert os.path.exists(os.path.join(config.config['data_dir'], 'tags.json'))
|
||||||
|
|
||||||
def test_trying_to_delete_used(test_ctx, post_factory):
|
def test_deleting_used(test_ctx, post_factory):
|
||||||
tag = test_ctx.tag_factory(names=['tag'])
|
tag = test_ctx.tag_factory(names=['tag'])
|
||||||
post = post_factory()
|
post = post_factory()
|
||||||
post.tags.append(tag)
|
post.tags.append(tag)
|
||||||
db.session.add_all([tag, post])
|
db.session.add_all([tag, post])
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
with pytest.raises(tags.TagIsInUseError):
|
test_ctx.api.delete(
|
||||||
test_ctx.api.delete(
|
test_ctx.context_factory(
|
||||||
test_ctx.context_factory(
|
user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)),
|
||||||
user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)),
|
'tag')
|
||||||
'tag')
|
db.session.refresh(post)
|
||||||
assert db.session.query(db.Tag).count() == 1
|
assert db.session.query(db.Tag).count() == 0
|
||||||
|
assert post.tags == []
|
||||||
|
|
||||||
def test_trying_to_delete_non_existing(test_ctx):
|
def test_trying_to_delete_non_existing(test_ctx):
|
||||||
with pytest.raises(tags.TagNotFoundError):
|
with pytest.raises(tags.TagNotFoundError):
|
||||||
|
|
Loading…
Reference in a new issue