def update_comment(comment_id, content=''): #if user is None: #raise Err('value:notfound', 'user', 'user not found.') content = content.strip() if not content: raise Err('value:invalid', 'content', 'content cannot be empty.') comment = Query(Comments).get(comment_id) if comment is None: raise Err('value:notfound', 'comment', 'comment not found.') comment.set('content', content) comment.save() return comment
def add(): name = request.form['name'].strip() summary = request.form['summary'].strip() content = request.form['content'].strip() if not name: raise Err('value:invalid', 'name', 'name cannot be empty.') if not summary: raise Err('value:invalid', 'summary', 'summary cannot be empty.') if not content: raise Err('value:invalid', 'content', 'content cannot be empty.') blog = Blog(name=name, summary=summary, content=content, user=user) blog.save() return redirect(url_for('blogs.list'))
def comment(blog_id, content=''): user = check_login() if user is None: raise Err('value:notfound', 'user', 'user not found.') content = content.strip() if not content: raise Err('value:invalid', 'content', 'content cannot be empty.') try: blog = Query(Blog).get(blog_id) comment = Comment(blog=blog, user=user, content=content) comment.save() except LeanCloudError, e: raise e
def update_blog(blog_id, name='', summary='', content=''): name = name.strip() summary = summary.strip() content = content.strip() if not name: raise Err('value:invalid', 'name', 'name cannot be empty.') if not summary: raise Err('value:invalid', 'summary', 'summary cannot be empty.') if not content: raise Err('value:invalid', 'content', 'content cannot be empty.') user = check_login() #if user is None: #raise Err('value:notfound', 'user', 'user not found.') blog = None try: blog = Query(Blog).get(blog_id) except LeanCloudError, e: pass
def sign_up(username, password, email): email = email.strip().lower() if not email or not _RE_EMAIL.match(email): raise Err('value:invalid', 'email', 'email cannot be empty.') user = User() user.set("username", username) user.set("password", password) user.set("email", email) try: user.sign_up() except LeanCloudError, e: raise e
def get_comments(blog_id): blog = None comments = None user = None try: blog = Query(Blog).equal_to("objectId", blog_id).first() if blog is None: raise Err('value:notfound', 'blog', 'blog not found.') try: comments = Query(Comments).equal_to( "blog", blog).descending('createdAt').find() except LeanCloudError, e: pass except LeanCloudError, e: if e.code == 101: # 服务端对应的 Class 还没创建 blog = {} else: raise e
def delete_comment(comment_id): comment = Query(Comments).get(comment_id) if comment is None: raise Err('value:notfound', 'comment', 'comment not found.') comment.destroy() return comment
def delete_blog(blog_id): blog = Query(Blog).get(blog_id) if blog is None: raise Err('value:notfound', 'blog', 'blog not found.') blog.destroy() return blog
def get_blog(blog_id): blog = Query(Blog).get(blog_id) if blog: return blog raise Err('value:notfound', 'blog', 'blog not found.')
def get_user(user_id): user = Query(User).get(user_id) if user: return user raise Err('value:notfound', 'user', 'user not found.')