server/posts: fix deleting post with comments
This commit is contained in:
parent
11d3853122
commit
ff189a3915
4 changed files with 10 additions and 4 deletions
|
@ -97,7 +97,7 @@ class PostDetailApi(BaseApi):
|
|||
auth.verify_privilege(ctx.user, 'posts:delete')
|
||||
post = posts.get_post_by_id(post_id)
|
||||
snapshots.save_entity_deletion(post, ctx.user)
|
||||
ctx.session.delete(post)
|
||||
posts.delete(post)
|
||||
ctx.session.commit()
|
||||
tags.export_to_json()
|
||||
return {}
|
||||
|
|
|
@ -125,7 +125,7 @@ class Post(Base):
|
|||
'PostFavorite', cascade='all, delete-orphan', lazy='joined')
|
||||
notes = relationship(
|
||||
'PostNote', cascade='all, delete-orphan', lazy='joined')
|
||||
comments = relationship('Comment')
|
||||
comments = relationship('Comment', cascade='all, delete-orphan')
|
||||
|
||||
# dynamic columns
|
||||
tag_count = column_property(
|
||||
|
|
|
@ -316,3 +316,6 @@ def feature_post(post, user):
|
|||
post_feature.post = post
|
||||
post_feature.user = user
|
||||
db.session.add(post_feature)
|
||||
|
||||
def delete(post):
|
||||
db.session.delete(post)
|
||||
|
|
|
@ -38,14 +38,15 @@ def test_saving_post(post_factory, user_factory, tag_factory):
|
|||
assert len(related_post1.relations) == 0
|
||||
assert len(related_post2.relations) == 0
|
||||
|
||||
def test_cascade_deletions(post_factory, user_factory, tag_factory):
|
||||
def test_cascade_deletions(post_factory, user_factory, tag_factory, comment_factory):
|
||||
user = user_factory()
|
||||
tag1 = tag_factory()
|
||||
tag2 = tag_factory()
|
||||
related_post1 = post_factory()
|
||||
related_post2 = post_factory()
|
||||
post = post_factory()
|
||||
db.session.add_all([user, tag1, tag2, post, related_post1, related_post2])
|
||||
comment = comment_factory(post=post, user=user)
|
||||
db.session.add_all([user, tag1, tag2, post, related_post1, related_post2, comment])
|
||||
db.session.flush()
|
||||
|
||||
score = db.PostScore()
|
||||
|
@ -91,6 +92,7 @@ def test_cascade_deletions(post_factory, user_factory, tag_factory):
|
|||
assert db.session.query(db.PostNote).count() == 1
|
||||
assert db.session.query(db.PostFeature).count() == 1
|
||||
assert db.session.query(db.PostFavorite).count() == 1
|
||||
assert db.session.query(db.Comment).count() == 1
|
||||
|
||||
db.session.delete(post)
|
||||
db.session.commit()
|
||||
|
@ -105,6 +107,7 @@ def test_cascade_deletions(post_factory, user_factory, tag_factory):
|
|||
assert db.session.query(db.PostNote).count() == 0
|
||||
assert db.session.query(db.PostFeature).count() == 0
|
||||
assert db.session.query(db.PostFavorite).count() == 0
|
||||
assert db.session.query(db.Comment).count() == 0
|
||||
|
||||
def test_tracking_tag_count(post_factory, tag_factory):
|
||||
post = post_factory()
|
||||
|
|
Loading…
Reference in a new issue