本文整理汇总了Python中app.models.Comment.query方法的典型用法代码示例。如果您正苦于以下问题:Python Comment.query方法的具体用法?Python Comment.query怎么用?Python Comment.query使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app.models.Comment
的用法示例。
在下文中一共展示了Comment.query方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: post
# 需要导入模块: from app.models import Comment [as 别名]
# 或者: from app.models.Comment import query [as 别名]
def post(self, user):
# grab the form
form = PostDeleteForm(self.request.params)
# validate csrf
if not self.validate_csrf(form.csrf_token.data):
form.csrf_token.data = self.generate_csrf()
self.r(form, flashes=flash('Please submit the form again.'))
return
# get the post please
try:
post = Key(urlsafe=form.key.data).get()
except:
post = None
# as usual, this really shouldn't
# be None unless unnatural navigation
if post is None:
self.redirect('/')
return
# the post exists, is it owned by the user?
if post.author != user:
self.redirect('/')
return
# check if the user is the owner or not
if post.author != user:
self.redirect('/')
return
# everything checks out so let's remove the post
# first though, we need to remove the comments
# assoc with the post and the likes
# TODO query these entities to make sure they are removed
comments = Comment.query(ancestor=post.key).fetch()
ks = put_multi(comments)
delete_multi(ks)
likes = Like.query(Like.post == post.key).fetch()
ks = put_multi(likes)
delete_multi(ks)
# got everything else removed, let's
# remove the post
try:
post.key.delete()
self.redirect('/')
return
except Exception as e:
# let them try again
form.csrf_token.data = self.generate_csrf()
self.r(form, post, flashes=flash())
return
示例2: get
# 需要导入模块: from app.models import Comment [as 别名]
# 或者: from app.models.Comment import query [as 别名]
def get(self):
# get the user
user = self.validate_user()
# we have the user and need to access their key to see if they liked the post
# if they are not signed in then we don't show if they liked or not
if user is not None:
user = User.query(User.username == user).get()
if user is not None:
user = user.key
# get the post key
k = self.request.get('key', None)
if k is None:
self.redirect('/')
return
# create our form data for likes/comments
formData = {
'key': k,
'csrf_token': self.generate_csrf()
}
# create our comment form
form = CommentForm(data=formData)
# create our like form
like_form = LikeForm(data=formData)
# try to retrieve the post based on the key
try:
post = Key(urlsafe=k).get()
except:
post = None
# don't have the comments yet, or likes
comments = None
liked = None
if post is not None:
# grab the comments please
comments = Comment.query(ancestor=post.key).order(-Comment.created).fetch()
# grab likes
# we also need to see if this user has "liked" the post already
liked = Like.query(Like.owner == user, Like.post == post.key).get()
self.r(form, like_form, post, comments, liked)