server/tags: fix removing tags related to others

This commit is contained in:
rr- 2016-05-11 22:44:05 +02:00
parent 4cd5b8c1ac
commit 82d32ba1fb
3 changed files with 35 additions and 2 deletions

View File

@ -69,7 +69,7 @@ class TagDetailApi(BaseApi):
'Please untag relevant posts first.')
auth.verify_privilege(ctx.user, 'tags:delete')
snapshots.save_entity_deletion(tag, ctx.user)
ctx.session.delete(tag)
tags.delete(tag)
ctx.session.commit()
tags.export_to_json()
return {}

View File

@ -148,12 +148,21 @@ def get_tag_siblings(tag):
.limit(50)
return result
def delete(source_tag):
db.session.execute(
sqlalchemy.sql.expression.delete(db.TagSuggestion) \
.where(db.TagSuggestion.child_id == source_tag.tag_id))
db.session.execute(
sqlalchemy.sql.expression.delete(db.TagImplication) \
.where(db.TagImplication.child_id == source_tag.tag_id))
db.session.delete(source_tag)
def merge_tags(source_tag, target_tag):
db.session.execute(
sqlalchemy.sql.expression.update(db.PostTag) \
.where(db.PostTag.tag_id == source_tag.tag_id) \
.values(tag_id=target_tag.tag_id))
db.session.delete(source_tag)
delete(source_tag)
def create_tag(names, category_name, suggestions, implications):
tag = db.Tag()

View File

@ -72,6 +72,30 @@ def test_merging_with_usages(test_ctx, fake_datetime, post_factory):
assert tags.try_get_tag_by_name('source') is None
assert tags.get_tag_by_name('target').post_count == 1
def test_merging_when_related(test_ctx, fake_datetime):
source_tag = test_ctx.tag_factory(names=['source'], category_name='meta')
target_tag = test_ctx.tag_factory(names=['target'], category_name='meta')
db.session.add_all([source_tag, target_tag])
db.session.flush()
referring_tag = test_ctx.tag_factory(names=['parent'])
referring_tag.suggestions = [source_tag]
referring_tag.implications = [source_tag]
db.session.add(referring_tag)
db.session.commit()
assert tags.try_get_tag_by_name('parent').implications != []
assert tags.try_get_tag_by_name('parent').suggestions != []
with fake_datetime('1997-12-01'):
result = test_ctx.api.post(
test_ctx.context_factory(
input={
'remove': 'source',
'mergeTo': 'target',
},
user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
assert tags.try_get_tag_by_name('source') is None
assert tags.try_get_tag_by_name('parent').implications == []
assert tags.try_get_tag_by_name('parent').suggestions == []
@pytest.mark.parametrize('input,expected_exception', [
({'remove': None}, tags.TagNotFoundError),
({'remove': ''}, tags.TagNotFoundError),