async def user_edit_blog_post(blog_id, request): cookies = request.cookies json = await request.json() blog_heading, blog_summary, blog_content = json['blog_heading'], json[ 'blog_summary'], json['blog_content'] ##----------------------------## b = await Blog.find(blog_id) print('blog:%s' % b) if not b: print('获取博客用户不存在') return '''博客不存在''' r = await checkUser(cookies) if not r['success']: print('获取Blog未登录') return '''请登录''' ##api please sign in u = r['user'] if u.id != b.user_id: print('u.id:%s,b.user-id:%s' % (u.id, b.user_id)) return '''没有权限''' ##------------------------------## created_at = b.created_at newBlog = await Blog.easyBlog(u, name=blog_heading, summary=blog_summary, content=blog_content, created_at=created_at) await b.chownComments(newBlog.id) await Blog.delete(blog_id) r = await Blog.find(blog_id) if not r: print('Yes delete blog:%s!' % blog_id) return jsonResponse(success=True, message='成功啦!')
async def do_comment_post(blog_id, request): cookies = request.cookies json = await request.json() content = json['content'] user_id = cookies['user_id'] logging.info('cookies:%s' % cookies) u = await User.find(user_id) if not u: logging.warn('User %s not found' % user_id) message = '<div class="alert alert-warning"><span class="glyphicon glyphicon-exclamation-sign"></span>你需要先登录<a href="/sign-in">前往登录</a>?</div>' return apiError(message=message) if u.key != cookies['key']: logging.warn('User %d 持有的key 与本地数据不一致' % user_id) message = '<div class="alert alert-warning"><span class="glyphicon glyphicon-exclamation-sign"></span>你尚未登录,<a href="/sign-in">前往登录</a>?</div>' return apiError(message=message) comment = Comment(user_id=user_id, user_name=u.name, user_image=u.image, blog_id=blog_id, content=content) logging.info('comments-object formed:%s' % comment) await comment.save() comment.format() tem = env.get_template(files['comment_show']) co = tem.render(comment=comment) return jsonResponse(data={'comment': co})
async def api_delete_blog(blog_id, cookies): check = await checkBlog(blog_id, cookies) json = check['json'] u = check['user'] if not check['success']: return apiError(**json) await Blog.delete(blog_id) json['message'] = '成功删除博客' return jsonResponse(**json)
async def do_api_get_blog(blog_id, cookies): b = await Blog.find(blog_id) r = await checkUser(cookies) if not r['success']: return r['response'] u = r['user'] uid = cookies['user_id'] if uid != u.id: logging.info('serverid: %s, client_id:%s' % (u.id, blog_id)) return apiError(message='你没有权限查看该博客') comments = await b.getCommentsFormat() b.comments = comments logging.info('blog:%s' % b) return jsonResponse(data=b)
async def do_comment_post(blog_id, cookies): user_id = cookies['user_id'] logging.info('cookies:%s' % cookies) u = await User.find(user_id) if not u: logging.warn('User %s not found' % user_id) message = '<div class="alert alert-warning"><span class="glyphicon glyphicon-exclamation-sign"></span>你需要先登录<a href="/sign-in">前往登录</a>?</div>' return apiError(message=message) if u.key != cookies['key']: logging.warn('User %d 持有的key 与本地数据不一致' % user_id) message = '<div class="alert alert-warning"><span class="glyphicon glyphicon-exclamation-sign"></span>你尚未登录,<a href="/sign-in">前往登录</a>?</div>' return apiError(message=message) b = await Blog.find(blog_id) if not b: return '''博客不存在''' return jsonResponse(data={'content': b.content})
async def do_mudule_get(module_name): module_dir = 'js' file_name = module_dir + '/' + module_name + '.js' js = getEasyTemplate(file_name) return jsonResponse(data=js)
async def do_cloud_app_get(app_name): app_dir = 'app/' + app_name file_name = app_dir + '/' + app_name + '.html' html = getEasyTemplate(file_name) return jsonResponse(data=html)
async def do_api_file_get(filename): log(filename) #filename=decodePath(filename) f = Path(filename) f.addContent() return jsonResponse(f.toJson())
async def do_file_write(filename, content): log('filename', filename) writeFile(filename, content) return jsonResponse(message='successfully write into file ' + filename)
async def do_file_read(filename): #filename=json['filename'] content = loadText(filename) return jsonResponse(data=content)