def api_create_comment(blog_id): ''' 创建评论 ''' i = ctx.request.input(content='') content = i.content.strip() if not content: raise APIValueError('content', 'content cannot be empty') user = ctx.request.user comment = Comment(blog_id=blog_id, user_id=user.id, user_name=user.name, user_image=user.image, content=content) comment.insert() 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
def api_create_comment(id, request, *, content): user = request.__user__ if user is None: raise APIPermissionError('Please sign in first.') if not content or not content.strip(): raise APIValueError('content', 'Content cannot be empty.') blog = yield from Blog.find(id) if blog is None: raise APIResourceNotFoundError('Blog', 'No such a blog.') comment = Comment(user_id=user.id, user_name=user.name, user_image=user.image, blog_id=blog.id, content=content.strip()) yield from content.save() return comment
async def api_create_comment(id, request, *, content): # 传入日志id,request,评论内容 user = request.__user__ # 获取经过auth_factory加工的request的__user__属性 if user is None: # user为空说明没有登录或没有成功登录 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 create_comment(id, request, *, content): user = request.__user__ if user is None: raise APIPermissionError('content') 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 api_create_blog_comment(blog_id): ''' 创建评论 ''' blog = Blog.get(blog_id) if blog is None: raise APIResourceNotFoundError('Blog') content = ctx.request.input(content='').content.strip() if not content: raise APIValueError('content') c = Comment(blog_id=blog_id, user_id=1, user_name='匿名', user_image='', content=content) c.insert() return dict(comment=c)
async def create_blog_comment(id, request, *, content): user = request.__user__ if not user: raise APIPermissionError('permission sigin first') if not content or not content.strip(): raise APIValueError('comment', 'comment is empty') blog = await Blog.find(id) if not blog: raise APIResourceNotFoundError('not exist blog id') comment = Comment(blog_id=blog.id, user_id=user.id, user_name=user.name, user_image=user.image, content=content.strip()) logging.info('handler comment: %s' % str(comment)) await comment.save() return comment
def api_create_comment(id): ##通过Cookie查询登录用户 user = cookie2user() if user is None: raise APIPermissionError('Please signin first.') content = request.json['content'] if not content or not content.strip(): raise APIValueError('content') blog = Blog.get(id=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()) return comment.to_dict()
async def api_comment_blog(id, request, *, content): # check_admin(request) user = request.__user__ if user is None: raise APIPermissionError('Please signin first.') blog = await Blog.find(id) if blog is None: raise APIResourceNotFoundError('blog is not found.') if not content or not content.strip(): raise APIValueError('content', 'content cannot be empty.') 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_comments(id, request, *, content): user = request.__user__ if user is None: raise APIPermissioinError('please signin first.') if not content or not content.strip(): raise APIValueError('content', 'content connot be empty') blog = await Blog.find(id) if blog is None: #确保每一个存入的评论都有对应的blog 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.strip()) await comment.save() return comment
def api_create_blog_comment(blog_id): user = ctx.request.user if user is None: raise APIPermissionError('Need signin.') blog = Blog.get(blog_id) if blog is None: raise APIResourceNotFoundError('Blog') content = ctx.request.input(content='').content.strip() if not content: raise APIValueError('content') c = Comment(blog_id=blog_id, user_id=user.id, user_name=user.name, user_image=user.image, content=content) c.insert() return dict(comment=c)
async def get_blogs(*, page='1', size='8', order_by='created_at', desc=True, user=False, category=None, request): ''' 1. if user is set to False, then it is browser mode, enable selection by category 2. otherwise, it is management mode, return blogs whose author is the current user. ''' try: page, size = get_page_index(page, size) except APIError as e: raise APIValueError('Blog', 'GET /api/blogs ' + e.data) if not user: if category: where = 'category=?' args = [category] else: where = None args = None num = await Blog.findNumber('count(id)', where, args) p = Page(num, page, size) des = ' desc' if desc is True else ' asc' blogs = await Blog.findAll(where, args, orderBy=order_by + des, limit=(p.offset, p.limit)) else: if not request.__user__: raise APIPermissionError('blog', 'Log in to manage blogs') num = await Blog.findNumber('count(id)', where="user_id=?", args=[request.__user__.id]) p = Page(num, page, size) des = ' desc' if desc is True else ' asc' blogs = await Blog.findAll('user_id=?', [request.__user__.id], orderBy=order_by + des, limit=(p.offset, p.limit)) for blog in blogs: blog.content = '' return dict(total=p.page_count, page=page, blogs=blogs)
def get_category_blogs(request, *, id, page='1'): category = yield from Category.find(id) if not category: raise APIValueError('category id', 'can not find category, by id:{}'.format(id)) page_index = get_page_index(page) num = yield from Blog.find_number('count(id)', 'category_id=?', [id]) page = Page(num, page_index) if num == 0: blogs = [] else: blogs = yield from Blog.find_all(where='category_id=?', args=[id], order_by='created_at desc', limit=(page.offset, page.limit)) for blog in blogs: blog.html_summary = markdown2.markdown( blog.summary, extras=[ 'code-friendly', 'fenced-code-blocks', 'highlightjs-lang', 'tables', 'break-on-newline' ]).replace("<table>", "<table class=\"ui celled table\">") comments_count = yield from Comment.find_number( select_field='count(id)', where='blog_id=?', args=[blog.id]) blog.comments_count = comments_count #get blog tags tags = [] if blog.tags: for tag_id in blog.tags.split(","): tag = yield from Tags.find(tag_id) if tag: tags.append({ "key": tag.id, "value": tag.name, "color": COLORS[tag.id % len(COLORS)] }) blog.tags = tags return { '__template__': 'category.html', 'page': page, 'blogs': blogs, 'category': category }
async def api_update_pwd_users(id, *, request, passwd, passwd2): logging.info('%s %s' % (passwd, passwd2)) if request.__user__.id != id: raise APIResourceNotFoundError('User') user = await User.find(id) if user is None: raise APIResourceNotFoundError('User') sha1 = hashlib.sha1() sha1.update(user.id.encode('utf-8')) sha1.update(b':') sha1.update(passwd.encode('utf-8')) logging.info('%s %s' % (user.passwd, sha1.hexdigest())) if user.passwd != sha1.hexdigest(): raise APIValueError('passwd', 'Invalid password...') sha1_password = '******' % (id, passwd2) user.passwd = hashlib.sha1(sha1_password.encode('utf-8')).hexdigest() await user.update() return dict(id=id)
def api_get_histories(user_id): if not user_id: return APIValueError('users') histories = History.find_by('where user_id=? order by created_at desc', user_id) movies = [] if histories: for history in histories: dt = datetime.fromtimestamp(history.created_at) movie_id = history.movie_id movie = Movie.get(movie_id) if movie: movie = _get_movie_details(movie) movie.history_day = dt.day movie.history_month = dt.month movies.append(movie) return dict(movies=movies) raise APIResourceNotFoundError('histories')
def api_create_comment(id,request,*,content): user=request.__user__ #必须为登录状态 if user is None: raise APIPermissionError('content') #评论不能为空 if not content or not content.strip(): raise APIValueError('content') #查询一下博客id是否有对应的博客 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 get_blog(id): blog = yield from Blog.find(id) logger.info('blog id is :{}'.format(id)) if not blog: raise APIValueError('id', 'can not find blog id is :{}'.format(id)) if not blog.enabled: raise APIResourceNotFoundError( 'id', 'Sorry, This articale can\'t find now, Please try it again later...' ) blog.view_count += 1 yield from blog.update() #comments = yield from Comment.find_all('blog_id=?', [id], order_by='created_at desc') comments = yield from CommentAnonymous.find_all('blog_id=?', [id], order_by='created_at asc') tags = [] if blog.tags: for tag_id in blog.tags.split(","): tag = yield from Tags.find(tag_id) if tag: tags.append({ "key": tag.id, "value": tag.name, "color": COLORS[tag.id % len(COLORS)] }) blog.keywords = ",".join([x["value"] for x in tags]) blog.tags = tags for c in comments: c.html_content = markdown2.markdown( c.content, extras=[ 'code-friendly', 'fenced-code-blocks', 'highlightjs-lang', 'tables', 'break-on-newline' ]).replace("<table>", "<table class=\"ui celled table\">") blog.html_content = markdown2.markdown( blog.content, extras=[ 'code-friendly', 'fenced-code-blocks', 'highlightjs-lang', 'tables', 'break-on-newline' ]).replace("<table>", "<table class=\"ui celled table\">") return {'__template__': 'blog.html', 'blog': blog, 'comments': comments}
async def api_create_comment(id, request, *, content): # 对某个博客发表评论 user = request.__user__ # 必须在登陆状态下评论 if user is None: raise APIPermissionError('content') # 评论不能为空 if not content or not content.strip(): raise APIValueError('content') # 查询以下博客id是否有对应的博客 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 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", "content cannot be empty") # 检查博客的存在性 blog = yield from Blog.find(id) if blog is None: raise APIResourceNotFoundError("Blog", "No such a blog.") # 创建评论对象 comment = Comment(user_id=user.id, user_name=user.name, user_image=user.image, blog_id=blog.id, content=content.strip()) yield from comment.save() # 储存评论入数据库 return comment # 返回评论
def get_blog(id): blog = yield from Blog.find(id) logger.info('blog id is :{}'.format(id)) if not blog: raise APIValueError('id', 'can not find blog id is :{}'.format(id)) if not blog.enabled: raise APIResourceNotFoundError('id', 'Sorry, This articale can\'t find now, Please try it again later...') blog.view_count += 1 yield from blog.update() #comments = yield from Comment.find_all('blog_id=?', [id], order_by='created_at desc') comments = yield from CommentAnonymous.find_all('blog_id=?', [id], order_by='created_at asc') for c in comments: c.html_content = markdown2.markdown(c.content, extras=['code-friendly', 'fenced-code-blocks', 'highlightjs-lang', 'tables', 'break-on-newline']).replace("<table>", "<table class=\"ui celled table\">") blog.html_content = markdown2.markdown(blog.content, extras=['code-friendly', 'fenced-code-blocks', 'highlightjs-lang', 'tables', 'break-on-newline']).replace("<table>", "<table class=\"ui celled table\">") return { '__template__': 'blog.html', 'blog': blog, 'comments': comments }
def api_create_blog_comments(request, *, id, content): '''create blog comments''' if not request.__user__: raise APIPermissionError('please login first.') if not content or not content.strip(): raise APIValueError('content', 'content can not be empty.') blog = yield from Blog.find(id) if blog is None: raise APIResourceNotFoundError('blog', 'can not find blog, id :{}'.format(id)) comment = Comment(blog_id=id, user_id=request.__user__.id, user_name=request.__user__.name, user_image=request.__user__.image, content=content.strip()) yield from comment.save() return comment
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 # 返回评论
async def api_create_jwc_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') jwc = await Jwc.find(id) if jwc is None: raise APIResourceNotFoundError('jwc') uid = next_id() await find_at(uid, content) comment = Comment(id=uid, xinxi_id=jwc.id, user_id=user.id, user_name=user.name, user_image=user.image, content=content.strip(), ku='jwc') await comment.save() return dict(code=0, comment=comment)
async def api_create_reply(request, *, blog_id, comment_id, content, reply_user_id): user = request.__user__ if user is None: raise APIPermissionError('请先登录哦!(Please signin first.)') if not content or not content.strip(): raise APIValueError('content') reply_user = await User.find(reply_user_id) if reply_user is None: raise APIPermissionError('您回复的账号不存在!') send_email.send_comment_email(reply_user.email, content, '/blog/%s' % blog_id) reply = Reply(comment_id=comment_id, user_id=user.id, reply_user_id=reply_user_id, user_name=user.name, user_image=user.image, content=content) await reply.save() return reply
async def api_create_lecture_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') lecture = await Lecture.find(id) if lecture is None: raise APIResourceNotFoundError('lecture') uid = next_id() await find_at(uid, content) comment = Comment(id=uid, xinxi_id=lecture.id, ku='lecture', user_id=user.id, user_name=user.name, user_image=user.image, content=content.strip()) await comment.save() return dict(code=0, comment=comment)
def get_testResults(paper_id): d1 = datetime.now() if not paper_id: raise APIValueError('paper', 'not fond') paper = list(Paper.select().where(Paper.id == paper_id).dicts())[0] data = json.loads(paper['state']) print(paper['state']) res = data['results'] paper['state'] = '' re_state = TestState(paper=paper) re_state.finish_items = list( Item.select().where(Item.id << data['finish_items']).dicts()) re_state.unfinish_items = list( Item.select().where(Item.id << data['unfinish_items']).dicts()) re_state.true_items = list( Item.select().where(Item.id << data['true_items']).dicts()) re_state.false_items = list( Item.select().where(Item.id << data['false_items']).dicts()) ''' items = list(Item.select().where(Item.paper==paper['id']).dicts()) for item in items: for re in res: if item['id'] == re['item']: item['user_answer'] = re['user_answer'] if item['id'] in data['finish_items']: re_state.finish_items.append(item) if item['id'] in data['unfinish_items']: re_state.unfinish_items.append(item) if item['id'] in data['true_items']: re_state.true_items.append(item) if item['id'] in data['false_items']: re_state.false_items.append(item) ''' re_state.score = data['score'] re_state.status = data['status'] re_state.finish_time = data['finish_time'] re_state.utime = data['utime'] response = {'state': re_state.dicts()} d2 = datetime.now() return jsonify(response)
async def api_create_comment(id, request, *, content): check_admin(request) user = request.__user__ if not user: raise APIPermissionError('please sign in') if not content or not content.strip(): raise APIValueError('content', 'content cannot be empty ') blog = await Blog.find(id) if not blog: raise APIResourceNotFoundError('Blog not found') comments = Comment(blog_id=blog.id, user_id=user.id, user_name=user.name, user_image=user.image, content=content.strip(), created_at=time.time()) await comments.save() return comments
def comment_handler(blog_id): val_email = request.cookies.get('email') email = check_secure_val(val_email) try: user = session.query(User).filter_by(email=email).first() except: session.rollback() if request.method == 'POST': data = request.get_json() content = data['content'] if not content or not content.strip(): raise APIValueError('content', 'content cannot be empty.') comment = Comment(content=content.strip(), user_id=user.id, post_id=blog_id, user_name=user.name, user_image=user.image) session.add(comment) try: session.commit() except: session.rollback() # session.close() r = make_response(json.dumps(user.name, ensure_ascii=False).encode('utf-8')) r.headers['Content-type'] = 'application/json; charset=utf-8' return r
def api_create_comment(id, request, *, content): user = request.__user__ # 必须为登录状态下才能发表评论 if user is None: raise APIPermissionError('content') # 评论不能为空 if not content or not content.strip(): raise APIValueError('content') # 查询bolg id是否存在 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 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