def api_create_comment(id, request, *, content): blog=yield from Blog.findAll(id) print(blog) print(content) comment=Comment(blog_id=id,user_id='0015302547958723f7da9d783434468901251428ea85d4c000',user_name='xuehh',user_image='http://www.gravatar.com/avatar/067bf298c6244dd085d9b4b7d9f3e0ba?d=mm&s=120',content=content.strip()) yield from comment.save() return comment
def api_comments(*, page='1'): page_index = get_page_index(page) num = yield from Comment.findNumber('count(id)') p = Page(num, page_index) if num == 0: return dict(page=p, comments=()) comments = yield from Comment.findAll(orderBy='created_at desc', limit=(p.offset, p.limit)) return dict(page=p, comments=comments)
def api_comments(*, page='1'): page_index = get_page_index(page) num = yield from Comment.find_number('count(id)') p = Page(num, page_index) if num == 0: return dict(page=p, comments=()) comments = yield from Comment.find_all(orderBy='created_at desc', limit=(p.offset, p.limit)) return dict(page=p, comments=comments)
def api_comments(*, page='1'): #获取页面索引,默认为1: page_index = get_page_index(page) #查询数据库中Comment表中评论总数: num = yield from Comment.findNumber('count(id)') p = Page(num, page_index) if num == 0: return dict(page=p, comments=()) comments = yield from Comment.findAll(orderBy='created_at desc', limit=(p.offset, p.limit)) return dict(page=p, comments=comments)
def api_create_comment(id, request, *, content): user = request.__user__ if user is None: raise APIPermissionError('Please signin first.') if not content or not content.strip(): raise APIValueError('content') blog = yield from Blog.find(id) if blog is None: raise APIResourceNotFoundError('Blog') comment = Comment(blog_id=blog.id, user_id=user.id, user_name=user.name, user_image=user.image, content=content.strip()) yield from comment.save() return comment
def apiCreateComment(id, request, *, content): user = request.__user__ if user is None: raise APIPermissionError('请先登录') if not content or not content.strip(): raise APIValueError('内容为空') blog = yield from Blog.find(id) if blog is None: raise APIResourceNotFoundError('文章不存在') comment = Comment(blogId=blog.id, userId=user.id, userName=user.name, userImage=user.image, content=content.strip()) yield from Comment.save(comment) return content
def getBlog(id): blog = yield from Blog.find(id) comments = yield from Comment.findAll('blog_id=?', [id], orderBy='create_time desc') for c in comments: c.htmlContent = text2Html(c.content) return {'__template__': 'blog.html', 'blog': blog, 'comments': comments}
def api_delete_comments(id, request): check_admin(request) c = yield from Comment.find(id) if c is None: raise APIResourceNotFoundError('Comment') yield from c.remove() return dict(id=id)
def apiCommentDelete(id, request): checkAdmin(request) c = yield from Comment.find(id) if c is None: raise APIResourceNotFoundError('评论不存在') yield from c.remove() return dict(id=id)
def get_blog(id): blog = yield from Blog.find(id) comments = yield from Comment.findAll('blog_id=?', [id], orderBy='created_at desc') for c in comments: c.html_content = text2html(c.content) blog.html_content = www.markdown2.markdown(blog.content) return {'__template__': 'blog.html', 'blog': blog, 'comments': comments}
def api_create_comment(id, request, *, content): #获取请求中的用户信息: user = request.__user__ #用户信息为None则抛出异常: if user is None: raise APIPermissionError('Please signin first.') #参数中内容信息为空,抛出异常: if not content or not content.strip(): raise APIValueError('content') #数据库Blog表中查询指定文章信息: blog = yield from Blog.find(id) #查询无结果则抛出异常: if blog is None: raise APIResourceNotFoundError('Blog') #创建comment实例: comment = Comment(blog_id=blog.id, user_id=user.id, user_name=user.name, user_image=user.image, content=content.strip()) #将Comment信息存储到数据库: yield from comment.save() return comment
def find_number(model, selectField, where=None, args=None): if model == 'blog': count = yield from Blog.findnumber(selectField=selectField, where=where, args=args) return count if model == 'user': count = yield from User.findnumber(selectField=selectField, where=where, args=args) return count if model == 'comment': count = yield from Comment.findnumber(selectField=selectField, where=where, args=args) return count
def find_model(model, id): if model == 'blog': blog = yield from Blog.find(id) return blog if model == 'user': user = yield from User.find(id) return user if model == 'comment': comment = yield from Comment.find(id) return comment
def find_models(model, where=None, args=None, **kw): if model == 'user': users = yield from User.findall(where=where, args=args, **kw) return users if model == 'blog': blogs = yield from Blog.findall(where=where, args=args, **kw) return blogs if model == 'comment': comments = yield from Comment.findall(where=where, args=args, **kw) return comments
def get_blog(id): blog = yield from Blog.find(id) comments = yield from Comment.findAll('blog_id=?', [id], orderBy='created_at desc') for c in comments: c.html_content = text2html(c.content) blog.html_content = markdown2.markdown(blog.content) return { '__template__': 'blog.html' , 'blog': blog, 'comments': comments }
def api_delete_comments(id, request): #校验当前用户权限: check_admin(request) #数据库Comment表中查询指定评论信息: c = yield from Comment.find(id) #查询无结果则抛出异常: if c is None: raise APIResourceNotFoundError('Comment') #将Comment信息从数据库删除: yield from c.remove() return dict(id=id)
def post(self): user_url = "" if len(request.form['url']) != 0: if not (request.form['url'].__contains__("http://") or request.form['url'].__contains__("https://")): user_url = "http://" + request.form['url'] else: user_url = request.form['url'] this_content = request.form["content"] if this_content[0] == "@": # 此评论为回复评论 temp_dict = {} for i in (" ", "\t", "\n"): # 分离评论回复内容和回复人 find_result_tmp = this_content.find(i) if find_result_tmp != -1: temp_dict[i] = find_result_tmp if len(temp_dict) != 0: # 按value 排序,得到第一个空白符的位置 x, first_space = sorted(temp_dict.items(), key=lambda temp_dict:temp_dict[1])[0] if first_space: # get the author he want to reply reply_author = this_content[1:first_space] else: reply_author = this_content[1:] print "reply: ", reply_author # send email to replied user TODO comment = Comment( blog_id=request.form['blog_id'], user_id=(str(uuid.uuid4()).replace("-","")*2)[:50], # 随机生成一个userid, 不提供用户注册功能了,没意义 user_name=request.form['author'], user_email=request.form['email'], user_url=user_url, content=this_content, ) comment.insert()
def get_blog(id): #通过id在数据库Blog表中查询对应内容: blog = yield from Blog.find(id) #通过id在数据库Comment表中查询对应内容: comments = yield from Comment.findAll('blog_id=?', [id], orderBy='created_at desc') for c in comments: #将content值从text格式转换成html格式: c.html_content = text2html(c.content) blog.html_content = markdown2.markdown(blog.content) return { '__template__': 'blog.html', 'blog': blog, 'comments': comments }
def question(id): question = Question.query.filter_by(id=id).first_or_404() tags = question.tags.all() if (datetime.datetime.now()-question.created_at).days == 0: created_time = datetime.datetime.strftime(question.created_at, '%H:%M') else: created_time = datetime.datetime.strftime(question.created_at, '%Y-%m-%d') form_question_comment = CommentForm(prefix="form_question_comment") if form_question_comment.validate_on_submit() and form_question_comment.submit.data: comment = Comment(content=form_question_comment.content.data) comment.comment_question = question author = User.query.filter_by(id=current_user.id).first() comment.comment_author = author db.session.add(comment) db.session.commit() flash('评论成功') return redirect(url_for('main.question', id=question.id)) question_comments = question.question_comments form_add_answer = AddAnswerForm(prefix="form_add_answer") if form_add_answer.validate_on_submit() and form_add_answer.submit.data: ans = Answer(content=form_add_answer.content.data) ans.answer_question = question author = User.query.filter_by(id=current_user.id).first() ans.answer_author = author db.session.add(ans) db.session.commit() flash('回答成功') return redirect(url_for('main.question', id=question.id)) answers = question.question_answers.all() for i in range(1,len(answers)): for j in range(0,len(answers)-i): if answers[j].users.count() < answers[j+1].users.count(): answers[j],answers[j+1] = answers[j+1],answers[j] return render_template('question.html', question=question, tags=tags, created_time=created_time, form_question_comment=form_question_comment, question_comments=question_comments, form_add_answer=form_add_answer, answers=answers)
async def api_create_comment(request, *, id, content): user = request.__user__ if user is None: raise APIPermissionError('Please login first') if not content or not content.strip(): raise APIValueError('comment', 'comment cannot be empty') quote = await Quote.find(id) if quote is None: raise APIResourceNotFoundError('Quote') comment = Comment(quote_id=quote.id, user_id=user.id, user_name=user.name, user_image=user.image, content=content.strip()) await comment.save() return comment
async def api_create_comment(id, request, *, content): user = request.__user__ if user is None: raise APIPermissionError("Please signin first.") if not content or not content.strip(): raise APIValueError("content") blog = await Blog.find(id) if blog is None: raise APIResourceNotFoundError("Blog") comment = Comment(blog_id=blog.id, user_id=user.id, user_name=user.name, user_image=user.image, content=content.strip()) await comment.save() return comment
async def api_create_comment(blog_id, request, *, content): check_admin(request) user = request.__user__ if user is None: raise APIPermissionError() if not content or not content.strip(): raise APIValueError('content') blog = Blog.find(blog_id) if blog is None: raise APIResourceNotFoundError('Blog') comment = Comment(blog_id=blog_id, user_id=request.__user__.id, user_name=request.__user__.name, user_image=request.__user__.image, content=content) await comment.save() return comment
async def api_create_comment(id, request, *, content): logging.info('york----------------------用户发表评论API') user = request.__user__ if user is None: raise APIPermissionError('Please signin first.') if not content or not content.strip(): raise APIValueError('content') blog = await Blog.find(id) if blog is None: raise APIResourceNotFoundError('Blog') comment = Comment(blog_id=blog.id, user_id=user.id, user_name=user.name, user_image=user.image, content=content.strip()) await comment.save() return comment
def get_blog(id,request): cookie_str = request.cookies.get(COOKIE_NAME) user = '' if cookie_str: if 'deleted' in cookie_str: user = '' else: user = yield from cookie2user(cookie_str) blog = yield from Blog.find(id) comments = yield from Comment.findAll('blog_id=?', [id], orderBy='created_at desc') for c in comments: c.html_content = text2html(c.content) blog.html_content = www.markdown2.markdown(blog.content) return { '__template__': 'blog.html', 'blog': blog, 'comments': comments, 'user':user }
def answer(qid, aid): answer = Answer.query.filter_by(id=aid).first_or_404() question = Question.query.filter_by(id=answer.answer_question.id).first_or_404() tags = question.tags.all() if (datetime.datetime.now()-question.created_at).days == 0: created_time = datetime.datetime.strftime(question.created_at, '%H:%M') else: created_time = datetime.datetime.strftime(question.created_at, '%Y-%m-%d') if (datetime.datetime.now()-answer.created_at).days == 0: answer_created_time = datetime.datetime.strftime(answer.created_at, '%H:%M') else: answer_created_time = datetime.datetime.strftime(answer.created_at, '%Y-%m-%d') form_question_comment = CommentForm(prefix="form_question_comment") if form_question_comment.validate_on_submit() and form_question_comment.submit.data: comment = Comment(content=form_question_comment.content.data) comment.comment_question = question author = User.query.filter_by(id=current_user.id).first() comment.comment_author = author db.session.add(comment) db.session.commit() flash('评论成功') return redirect(url_for('main.answer', qid=answer.answer_question.id, aid=answer.id)) question_comments = question.question_comments form_answer_comment = CommentForm(prefix="form_answer_comment") if form_answer_comment.validate_on_submit() and form_answer_comment.submit.data: comment = Comment(content=form_answer_comment.content.data) comment.comment_answer = answer author = User.query.filter_by(id=current_user.id).first() comment.comment_author = author db.session.add(comment) db.session.commit() flash('评论成功') return redirect(url_for('main.answer', qid=answer.answer_question.id, aid=answer.id)) answer_comments = answer.answer_comments return render_template('answer.html', question=question, answer=answer, tags=tags, created_time=created_time, answer_created_time=answer_created_time, form_question_comment=form_question_comment, question_comments=question_comments, form_answer_comment=form_answer_comment, answer_comments=answer_comments)
async def api_get_comments(*, blog_id): comments = Comment.findAll('blog_id=?', [blog_id], orderBy='created_at desc') return comments