本文整理汇总了Python中models.Comment.delete方法的典型用法代码示例。如果您正苦于以下问题:Python Comment.delete方法的具体用法?Python Comment.delete怎么用?Python Comment.delete使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Comment
的用法示例。
在下文中一共展示了Comment.delete方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: delete_post
# 需要导入模块: from models import Comment [as 别名]
# 或者: from models.Comment import delete [as 别名]
def delete_post(cls, post_id, user_id):
error_msg = None
posts = Post.all().order('-created').fetch(limit=10)
updated_post_list = posts
post = Post.get_by_id(post_id)
if user_id == post.created_by:
# delete comments of the post
comments = Comment.all().filter('post_id =', post_id).fetch(limit=10)
for c in comments:
Comment.delete(c)
# delete likes of the post
likes = Like.all().filter('post_id =', post_id).fetch(limit=10)
for l in likes:
Like.delete(l)
# delete the post
updated_post_list = cls.update_list_on_delete(
object_list=posts,
object_id=post_id
)
Post.delete(post)
else:
error_msg = 'You can delete only your own posts'
return updated_post_list, error_msg
示例2: test_comment_crud_methods
# 需要导入模块: from models import Comment [as 别名]
# 或者: from models.Comment import delete [as 别名]
def test_comment_crud_methods(self):
text = 'Test message'
user = UserFactory.create(remote_id=TRAVIS_USER_ID)
post = PostFactory.create(remote_id=TR_POST_ID, wall_owner=user)
mock_comment = CommentFactory.create(text=text, post=post, wall_owner=user)
# Create
comment = Comment.objects.create(
**CommentFactory.get_mock_params_dict(
mock_comment, commit_remote=True)
)
self.assertTrue(comment.remote_id > 0)
self.assertEqual(comment.text, text)
fetched_comment = post.fetch_comments(sort='asc').first()
self.assertEqual(fetched_comment.text, text)
# Update
edited_message = 'Edited comment message'
comment = Comment.objects.get(id=comment.id)
comment.text = edited_message
comment.save(commit_remote=True)
self.assertEqual(comment.text, edited_message)
fetched_comment = post.fetch_comments(sort='asc').first()
self.assertEqual(fetched_comment.text, edited_message)
# Delete
comment.delete()
comment1 = Comment.objects.get(id=comment.id)
self.assertTrue(comment1.archived)
fetched_ids = [comment.remote_id for comment in post.fetch_comments()]
self.assertFalse(comment1.remote_id in fetched_ids)
# Restore
comment.restore()
comment1 = Comment.objects.get(id=comment.id)
self.assertFalse(comment1.archived)
fetched_ids = [comment.remote_id for comment in post.fetch_comments()]
self.assertTrue(comment1.remote_id in fetched_ids)
# Create with save()
comment = Comment()
comment.__dict__.update(
CommentFactory.get_mock_params_dict(mock_comment)
)
comment.text = text + text
comment.save(commit_remote=True)
self.assertTrue(comment.remote_id > 0)
self.assertEqual(comment.text, text + text)
fetched_comment = post.fetch_comments(sort='asc').first()
self.assertEqual(fetched_comment.text, text + text)
comment.delete()
示例3: view_ad
# 需要导入模块: from models import Comment [as 别名]
# 或者: from models.Comment import delete [as 别名]
def view_ad(url):
room = Room.query.filter_by(urlname=url).first()
if not room:
abort(404)
if room.dead or get_days_ago(room.created_at) > OLD_DAYS.days:
abort(404)
occupied = Occupied.query.filter_by(space=room.occupieds).order_by("created_at").first()
if occupied and get_days_ago(occupied.created_at) > OCCUPIED_DAYS.days:
abort(404)
# URL is okay. Show the ad.
comments = Comment.query.filter_by(commentspace=room.comments, parent=None).order_by("created_at").all()
commentform = CommentForm()
delcommentform = DeleteCommentForm()
if request.method == "POST":
if request.form.get("form.id") == "newcomment" and commentform.validate():
if commentform.edit_id.data:
comment = Comment.query.get(int(commentform.edit_id.data))
if comment:
if comment.user == g.user:
comment.message = commentform.message.data
flash("Your comment has been edited", category="info")
else:
flash("You can only edit your own comments", category="info")
else:
flash("No such comment", category="error")
else:
comment = Comment(user=g.user, commentspace=room.comments, message=commentform.message.data)
if commentform.parent_id.data:
parent = Comment.query.get(int(commentform.parent_id.data))
if parent and parent.commentspace == room.comments:
comment.parent = parent
room.comments.count += 1
db.session.add(comment)
flash("Your comment has been posted", category="success")
db.session.commit()
# Redirect despite this being the same page because HTTP 303 is required to not break
# the browser Back button
return redirect(url_for("view_ad", url=room.urlname) + "#c" + str(comment.id), code=303)
elif request.form.get("form.id") == "delcomment" and delcommentform.validate():
comment = Comment.query.get(int(delcommentform.comment_id.data))
if comment:
if comment.user == g.user:
comment.delete()
room.comments.count -= 1
db.session.commit()
flash("Your comment was deleted.", category="success")
else:
flash("You did not post that comment.", category="error")
else:
flash("No such comment.", category="error")
return redirect(url_for("view_ad", url=room.urlname), code=303)
return render_template(
"room.html", room=room, comments=comments, commentform=commentform, delcommentform=delcommentform
)
示例4: test_comment_crud_methods
# 需要导入模块: from models import Comment [as 别名]
# 或者: from models.Comment import delete [as 别名]
def test_comment_crud_methods(self):
group = GroupFactory(remote_id=GROUP_CRUD_ID)
post = PostFactory(remote_id=POST_CRUD_ID, text='', wall_owner=group)
user = UserFactory(remote_id=USER_AUTHOR_ID)
def assert_local_equal_to_remote(comment):
comment_remote = Comment.remote.fetch_post(post=comment.post).get(remote_id=comment.remote_id)
self.assertEqual(comment_remote.remote_id, comment.remote_id)
self.assertEqual(comment_remote.text, comment.text)
Comment.remote.fetch_post(post=post)
self.assertEqual(Comment.objects.count(), 0)
# create
comment = Comment(text='Test comment', post=post, wall_owner=group, author=user, date=datetime.now())
comment.save(commit_remote=True)
self.objects_to_delete += [comment]
self.assertEqual(Comment.objects.count(), 1)
self.assertNotEqual(len(comment.remote_id), 0)
assert_local_equal_to_remote(comment)
# create by manager
comment = Comment.objects.create(text='Test comment created by manager', post=post, wall_owner=group, author=user, date=datetime.now(), commit_remote=True)
self.objects_to_delete += [comment]
self.assertEqual(Comment.objects.count(), 2)
self.assertNotEqual(len(comment.remote_id), 0)
assert_local_equal_to_remote(comment)
# update
comment.text = 'Test comment updated'
comment.save(commit_remote=True)
self.assertEqual(Comment.objects.count(), 2)
assert_local_equal_to_remote(comment)
# delete
comment.delete(commit_remote=True)
self.assertEqual(Comment.objects.count(), 2)
self.assertTrue(comment.archived)
self.assertEqual(Comment.remote.fetch_post(post=comment.post).filter(remote_id=comment.remote_id).count(), 0)
# restore
comment.restore(commit_remote=True)
self.assertFalse(comment.archived)
self.assertEqual(Comment.objects.count(), 2)
assert_local_equal_to_remote(comment)
示例5: delete_comment
# 需要导入模块: from models import Comment [as 别名]
# 或者: from models.Comment import delete [as 别名]
def delete_comment(cls, post_id, comment_id, user_id):
error_msg = ''
comments = Comment.all().filter('post_id =', post_id).order('-created').fetch(limit=10)
comment = Comment.get_by_id(comment_id)
if user_id == comment.created_by:
updated_comments = cls.update_list_on_delete(
object_list=comments,
object_id=comment_id
)
Comment.delete(comment)
else:
error_msg = 'You can delete only your own comments'
updated_comments = comments
return updated_comments, error_msg
示例6: delete_comment
# 需要导入模块: from models import Comment [as 别名]
# 或者: from models.Comment import delete [as 别名]
def delete_comment(environ, id_comment):
Comment.delete(id_comment)
return HttpRedirect('/view/')
示例7: del_comment
# 需要导入模块: from models import Comment [as 别名]
# 或者: from models.Comment import delete [as 别名]
def del_comment(id):
comment = Comment.get(id=id)
comment.post.comm_count = comment.post.comm_count - 1
comment.post.save()
Comment.delete().where(id=id).execute()
redirect("/admin/comment")
示例8: delete_message
# 需要导入模块: from models import Comment [as 别名]
# 或者: from models.Comment import delete [as 别名]
def delete_message():
Comment.delete().where(
Comment.message_id == request.form['message_id']).execute()
Message.delete().where(Message.id == request.form['message_id']).execute()
return jsonify(True)
示例9: admin_delete_comment
# 需要导入模块: from models import Comment [as 别名]
# 或者: from models.Comment import delete [as 别名]
def admin_delete_comment(comment_id):
Comment.delete(comment_id)
comments = Comment.query.all()
return render_template('admin_comments.html', title='Admin', comments=comments, user=current_user)