api.py 文件源码

python
阅读 34 收藏 0 点赞 0 评论 0

项目:duckpond 作者: alexmilowski 项目源码 文件源码
def content_item_resource(id,resource):
   if request.method == 'GET':
      wrap = request.args.get('wrap')
      status_code,data,contentType = model.getContentResource(id,resource);
      if status_code==200:
         if contentType.startswith("text/html") and wrap is not None:
            blob = io.BytesIO()
            for chunk in data:
               blob.write(chunk)
            content = blob.getvalue().decode("utf-8").strip()
            if not content.startswith('<!DOCTYPE'):
               editorConfig = app.config.get('EDITOR_CONFIG')
               header = ''
               bodyStart = ''
               bodyEnd = ''
               if editorConfig is not None and wrap=='preview':
                  wheader = editorConfig.get('wrap-header')
                  pheader = editorConfig.get('preview-wrap-header')
                  if pheader is not None:
                     header = pheader
                  elif wheader is not None:
                     header = wheader
                  wbody = editorConfig.get('wrap-body')
                  pbody = editorConfig.get('preview-body-main')
                  if pbody is not None:
                     bodyStart = pbody[0]
                     bodyEnd = pbody[1]
                  elif wbody is not None:
                     bodyStart = wbody[0]
                     bodyEnd = wbody[1]
               elif editorConfig is not None and wrap=='formatted':
                  wheader = editorConfig.get('wrap-header')
                  if wheader is not None:
                     header = wheader
                  wbody = editorConfig.get('wrap-body')
                  if wbody is not None:
                     bodyStart = wbody[0]
                     bodyEnd = wbody[1]
               content = """
<!DOCTYPE html>
<html>
<head><title>""" + resource + '</title>' + header + """
</head>
<body>
""" + bodyStart + content + bodyEnd + '</body></html>'
            return Response(stream_with_context(content),content_type = contentType)
         else:
            return Response(stream_with_context(data),content_type = contentType)
      else:
         abort(status_code)
   if request.method == 'PUT':
      status_code,data,contentType = model.updateContentResource(id,resource,request.headers['Content-Type'],request.stream);
      if status_code==200 or status_code==201:
         return Response(stream_with_context(data),status=status_code,content_type = contentType)
      else:
         return Response(status=status)

   if request.method == 'DELETE':
      status = model.deleteContentResource(id,resource)
      return Response(status=status)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号