本文整理汇总了Python中models.Post.get_by_id方法的典型用法代码示例。如果您正苦于以下问题:Python Post.get_by_id方法的具体用法?Python Post.get_by_id怎么用?Python Post.get_by_id使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Post
的用法示例。
在下文中一共展示了Post.get_by_id方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fetch
# 需要导入模块: from models import Post [as 别名]
# 或者: from models.Post import get_by_id [as 别名]
def fetch(request):
if request.method != 'GET':
raise Http404
params = {}
for k in ('since_id', 'until_id', 'count'):
if k in request.GET:
params[k] = request.GET[k]
since_post = None
until_post = None
if 'since_id' in params:
since_post = Post.get_by_id(int(params['since_id']))
elif 'until_id' in params:
until_post = Post.get_by_id(int(params['until_id']))
count = 8
if 'count' in params and re.match('\d+', params['count']):
count = int(params['count'])
if not (count >= 1 and count <= 8):
count = 8
posts = Post.fetch(**{
'since_post' : since_post,
'until_post' : until_post,
'count' : count
})
return util.respond(True, [post.to_dict() for post in posts])
示例2: update_post
# 需要导入模块: from models import Post [as 别名]
# 或者: from models.Post import get_by_id [as 别名]
def update_post(cls, post_id, user_id, subject, content):
error_msg = ''
post = Post.get_by_id(post_id)
if user_id == post.created_by and subject and content:
post.content = content
post.subject = subject
post.put()
post = Post.get_by_id(post_id)
else:
error_msg = 'The post could not be saved, please check your content'
return post, error_msg
示例3: update_post
# 需要导入模块: from models import Post [as 别名]
# 或者: from models.Post import get_by_id [as 别名]
def update_post(filename, post_base_dir='./post/'):
filename = post_base_dir + filename
with open(filename) as f:
txt = f.readlines()
category = map(strip, txt[0].strip().split(','))
title = txt[1].strip()
tags = map(strip, txt[2].strip().split(','))
content = ''.join(txt[3:])
postid = get_postid(filename)
print 'postid...', postid
if not postid:
key = Post(category=category,
title=title,
tags=tags,
content=content).put()
print 'key...', key.id()
set_postid(filename, key.id())
print 'New post saved'
else:
post = Post.get_by_id(int(postid))
suggest = post.suggest
print suggest
post.category = category
post.title = title
post.tags = tags
post.content = content
post.suggest = suggest
post.put()
print 'Existed post saved'
示例4: post
# 需要导入模块: from models import Post [as 别名]
# 或者: from models.Post import get_by_id [as 别名]
def post(self):
cookie_user = self.get_user_from_cookie()
if cookie_user:
postID = self.request.get('postID')
post_obj = Post.get_by_id(int(postID))
like_obj = Likes.query(Likes.post == post_obj.key).get()
if post_obj.user == cookie_user.key:
self.write(json.dumps(({'like_count': -1})))
else:
if like_obj:
liked_by = like_obj.user_id
for u_id in liked_by:
if u_id == cookie_user.key.id():
self.write(json.dumps(({'like_count': -2})))
return
like_obj.like_count += 1
liked_by.append(cookie_user.key.id())
like_obj.put()
self.write(json.dumps(({'like_count': like_obj.like_count})))
else:
like_obj = Likes(post=post_obj.key, like_count=1)
like_obj.user_id.append(cookie_user.key.id())
like_obj.put()
time.sleep(0.2)
self.write(json.dumps(({'like_count' : like_obj.like_count})))
else:
return None
示例5: get
# 需要导入模块: from models import Post [as 别名]
# 或者: from models.Post import get_by_id [as 别名]
def get(self, post_id):
post = Post.get_by_id(int(post_id))
#resource = Post.post.blobKey
resource = post.blobKey
#resource = str(urllib.unquote(resource))
blob_info = blobstore.BlobInfo.get(resource)
self.send_blob(blob_info)
示例6: edit_post
# 需要导入模块: from models import Post [as 别名]
# 或者: from models.Post import get_by_id [as 别名]
def edit_post(request, post_id):
post = Post.get_by_id(int(post_id))
if not post:
raise Http404
if request.method == 'GET':
form = PostForm({'title': post.title,
'content': post.content,
# 'tags': post.tags,
'category': post.category.key(),
'is_published': post.is_published})
tagsString = post.getTagsString()
if request.method == 'POST':
form = PostForm(request.POST)
if form.is_valid():
modified_post = form.save(commit=False)
if modified_post.category.key() != post.category.key():
post.category.post_count -= 1
post.category.put()
modified_post.category.post_count += 1
modified_post.category.put()
post.title = modified_post.title
post.category = modified_post.category
post.content = modified_post.content
post.is_published = modified_post.is_published
post.putTags(request.POST['tagsString'])
post.put()
return HttpResponseRedirect('/post/%s/'%post.key().id())
return render_to_response('operate_post.html', {'form': form, 'id':post.key().id, 'tagsString': tagsString}, context_instance=RequestContext(request))
示例7: update
# 需要导入模块: from models import Post [as 别名]
# 或者: from models.Post import get_by_id [as 别名]
def update(request, post_id):
user = request.user
if request.method != 'POST':
raise Http404
post = Post.get_by_id(int(post_id))
if not post or not post.belongs_to(user):
return util.respond(False, 'Post doesn\'t exist')
post_data = {}
for k in ('title', 'body'):
if not k in request.POST:
return util.respond(False, 'Missing argument \'%s\''%k)
post_data[k] = request.POST[k]
title_len = len(post_data['title'])
if title_len > 80 or title_len < 1:
return util.respond(False, 'Post title must be between 1 and 80 letters')
body_len = len(post_data['body'])
if body_len > 1024 or body_len < 1:
return util.respond(False, 'Post body must be between 1 and 1024 letters')
post.title = util.xssescape(post_data['title'])
post.body = util.xssescape(post_data['body'])
post.put()
return util.respond(True, post.to_dict())
示例8: post
# 需要导入模块: from models import Post [as 别名]
# 或者: from models.Post import get_by_id [as 别名]
def post(self, digits):
post_id = digits
post = None
if digits.isdigit():
post = Post.get_by_id(int(post_id))
message = ''
# if user_id is not None, the user is logged in
if self.user:
if post:
uid = str(self.user.get_id())
if post.already_liked(uid):
message = 'You already liked this.'
if post.is_creator(uid):
message = 'You can\'t like your own post.'
if not post.already_liked(uid) and not post.is_creator(uid):
post.like(uid)
post.put()
else:
self.response.set_status(422)
message = 'The requested post or user was not found.'
else:
message = 'You must <a href="/blog/login/">sign in</a> or <a href="/blog/signup/">create an account</a> to comment.'
self.render('snippet/likes.html', post=post, message=message)
示例9: put
# 需要导入模块: from models import Post [as 别名]
# 或者: from models.Post import get_by_id [as 别名]
def put(self, id):
post = Post.get_by_id(id)
if post is None:
flash(gettext('The post was not found'), 'error')
return redirect(url_for('PostsView:index'))
if not current_user.is_admin() and not post.is_mine():
abort(401)
if request.method in ['POST', 'PUT']:
form = EditPostForm()
if form.validate_on_submit():
try:
form.populate_obj(post)
post.save()
flash(gettext('Post was succesfully saved'))
if request.method == 'POST':
if form.remain.data:
return redirect(url_for('PostsView:get', id=post.id))
else:
return redirect(url_for('PostsView:index'))
except:
flash(gettext('Error while updating the post'), 'error')
else:
flash(gettext('Invalid submission, please check the message below'), 'error')
if request.method == 'PUT':
return jsonify(redirect_to=url_for('PostsView:index'))
else:
form = NewPostForm(post)
return render_template('admin/posts/edit.html',
title=gettext('Edit Post: %(title)s', title=post.title),
form=form,
post=post)
示例10: post
# 需要导入模块: from models import Post [as 别名]
# 或者: from models.Post import get_by_id [as 别名]
def post(self, post_id):
# user_id = check_for_valid_cookie(self)
# cookie_user = User.get_by_id(int(user_id))
cookie_user = self.get_user_from_cookie()
if cookie_user:
post = Post.get_by_id(int(post_id))
if post:
postTitle = self.request.get('post-title')
postContent = self.request.get('content')
if postTitle and postContent:
post.title = postTitle
post.content = postContent
post.put()
time.sleep(0.2)
self.redirect('/blog/%s'%post.key.id())
else:
empty_title_content = True
self.render('editpost.html', user=cookie_user,
post=post, empty_title_content=empty_title_content)
else:
self.error(404)
return
else:
cookie_error = "You need to log in before you edit the post!"
self.render('login.html', cookie_error=cookie_error)
示例11: get
# 需要导入模块: from models import Post [as 别名]
# 或者: from models.Post import get_by_id [as 别名]
def get(self):
post_id = int(self.request.get('id'))
post = Post.get_by_id(post_id)
post.comment += 1
post.put()
comment = Comment(post=post.key, content=self.request.get("content"))
comment.put()
示例12: delete_post
# 需要导入模块: from models import Post [as 别名]
# 或者: from models.Post import get_by_id [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
示例13: view_post
# 需要导入模块: from models import Post [as 别名]
# 或者: from models.Post import get_by_id [as 别名]
def view_post(request, post_id):
post = Post.get_by_id(int(post_id))
if not post:
raise Http404
if not is_admin() and not post.is_published:
raise Http404
if request.method == "POST":
comment = Comment()
comment.content = request.POST["comment"]
comment.author = users.get_current_user()
comment.post = post
if request.POST["parent_comment"] != "":
parent_comment = Comment.get_by_id(int(request.POST["parent_comment"]))
comment.parent_comment = parent_comment
comment.put()
post.comment_count = post.comment_count + 1
post.put()
mail.send_mail(
sender="[email protected]",
to=post.author.email(),
subject=(u"牛逼 - 你的文章%s有了新评论" % post.title).encode("utf8"),
body=(
u"""%s在你的文章%s上留了评论:
%s
点击这个链接回复: http://www.niubi.de/post/%s/"""
% (comment.author.nickname(), post.title, comment.content, post.key().id())
).encode("utf8"),
)
comments = Comment.all().filter("post", post)
sent_users = []
for c in comments:
if not contains_user(sent_users, c.author):
mail.send_mail(
sender="[email protected]",
to=c.author.email(),
subject=(u"牛逼 - 你参与评论的文章%s有了新评论" % post.title).encode("utf8"),
body=(
u"""%s在文章%s上留了评论:
%s
点击这个链接回复: http://www.niubi.de/post/%s/"""
% (comment.author.nickname(), post.title, comment.content, post.key().id())
).encode("utf8"),
)
sent_users.append(c.author)
return HttpResponseRedirect("/post/%s" % post.key().id())
post.read_count = post.read_count + 1
post.put()
post.getComments()
return render_to_response(
"view_post.html",
{"post": post, "is_post_author": is_post_author(post_id)},
context_instance=RequestContext(request),
)
示例14: get
# 需要导入模块: from models import Post [as 别名]
# 或者: from models.Post import get_by_id [as 别名]
def get(self, post):
p = Post.get_by_id(int(post))
values = {
'p': p,
'upload_url': blobstore.create_upload_url('/bacheca/admin/upload'),
'code': memcache.get('last_img')
}
self.generate('bacheca/edit.html', values)
示例15: print_post
# 需要导入模块: from models import Post [as 别名]
# 或者: from models.Post import get_by_id [as 别名]
def print_post(request, post_id):
post = Post.get_by_id(int(post_id))
post.getComments()
if not post:
raise Http404
if not is_admin() and not post.is_published:
raise Http404
return render_to_response("print_post.html", {"post": post}, context_instance=RequestContext(request))