コード例 #1
0
ファイル: errors.py プロジェクト: teodoryantcheff/OSPy
 def __init__(self):
     status = status = HTTP_STATUS_CODES[406]
     headers = {
         'Cache-Control': 'no-cache',
         'Content-Type': 'application/json'
     }
     HTTPError.__init__(self, status, headers, self.message)
コード例 #2
0
 def test_snarfuri_405(self, mock_get, mock_head):
     mock_head.side_effect = HTTPError(response=MagicMock(status_code='405'))
     mock_get.side_effect = HTTPError(response=MagicMock(status_code='405'))
     self.input.group.return_value = 'http://405notallowed.com'
     snarfuri(self.phenny, self.input)
     mock_head.assert_called_once_with('http://405notallowed.com')
     self.assertEqual(mock_get.call_args[0][0], 'http://405notallowed.com')
     self.assertFalse(self.phenny.msg.called)
コード例 #3
0
  def __init__(self, url, status='303 See Other'):
    """
    Returns a `status` redirect to the new URL. 
    `url` is joined with the base URL so that things like 
    `redirect("about") will work properly.
    """
    newloc = urlparse.urljoin(web.ctx.path, url)

    newloc = ctx.homedomain + newloc

    headers = {
      'Content-Type': 'text/html',
      'Location': newloc
    }
    HTTPError.__init__(self, status, headers, "")
コード例 #4
0
ファイル: utils.py プロジェクト: nataliaratnikova/rucio
def generate_http_error(status_code, exc_cls, exc_msg):
    """
    utitily function to generate a complete HTTP error response.
    :param status_code: The HTTP status code to generate a response for.
    :param exc_cls: The name of the exception class to send with the response.
    :param exc_msg: The error message.
    :returns: a web.py HTTP response object.
    """
    status = codes[status_code]
    data = {'ExceptionClass': exc_cls, 'ExceptionMessage': exc_msg}
    # Truncate too long exc_msg
    if len(str(exc_msg)) > 15000:
        exc_msg = str(exc_msg)[:15000]
    headers = {
        'Content-Type': 'application/octet-stream',
        'ExceptionClass': exc_cls,
        'ExceptionMessage': clean_headers(exc_msg)
    }
    try:
        return HTTPError(status, headers=headers, data=render_json(**data))
    except:
        print {
            'Content-Type': 'application/octet-stream',
            'ExceptionClass': exc_cls,
            'ExceptionMessage': str(exc_msg).strip()
        }
        raise
コード例 #5
0
 def test_head_404(self, mock_head):
     self.input.group.return_value = 'https://vtluug.org/trigger_404'
     mock_head.side_effect = HTTPError(response=MagicMock(
         status_code='404'))
     head.head(self.phenny, self.input)
     mock_head.assert_called_once_with('https://vtluug.org/trigger_404')
     self.assertIn('404', self.phenny.say.call_args[0][0])
コード例 #6
0
def find_note_for_view0(token, id, name):
    if token != "":
        return NOTE_DAO.get_by_token(token)
    if id != "":
        return NOTE_DAO.get_by_id(id)
    if name != "":
        return NOTE_DAO.get_by_name(xauth.current_name(), name)

    raise HTTPError(504)
コード例 #7
0
    def GET(self):
        id   = xutils.get_argument("id", "")
        name = xutils.get_argument("name", "")
        page = xutils.get_argument("page", 1, type=int)
        if id == "" and name == "":
            raise HTTPError(504)
        if id != "":
            id = int(id)
            file = dao.get_by_id(id)
        elif name is not None:
            file = dao.get_by_name(name)
        if file is None:
            raise web.notfound()
        
        if not file.is_public and xauth.get_current_user() is None:
            return xauth.redirect_to_login()

        db = xtables.get_file_table()
        pathlist = dao.get_pathlist(db, file)
        user_name = xauth.get_current_name()
        can_edit = (file.creator == user_name) or (user_name == "admin")

        role = xauth.get_current_role()
        if role != "admin" and file.groups != '*' and file.groups != role:
            raise web.seeother("/unauthorized")

        files = []
        amount = 0
        if file.type == "group":
            amount = db.count(where="parent_id=%s AND is_deleted=0" % file.id)
            files = db.select(where=dict(parent_id=file.id, is_deleted=0), 
                order="priority DESC, sctime DESC", 
                limit=10, 
                offset=(page-1)*10)
        elif file.type == "post":
            file.content = file.content.replace(u'\xad', '\n')
            file.content = file.content.replace("\n", "<br/>")
            dao.visit_by_id(id)
        else:
            dao.visit_by_id(id)
        return xtemplate.render("file/view.html",
            file=file, 
            content = file.get_content(), 
            date2str=date2str,
            can_edit = can_edit,
            pathlist = pathlist,
            page_max = math.ceil(amount/10),
            page = page,
            page_url = "/file/view?id=%s&page=" % id,
            files = files)
コード例 #8
0
ファイル: utils.py プロジェクト: yashwardhan-gautam/rucio
def generate_http_error(status_code, exc_cls, exc_msg):
    """
    utitily function to generate a complete HTTP error response.
    :param status_code: The HTTP status code to generate a response for.
    :param exc_cls: The name of the exception class to send with the response.
    :param exc_msg: The error message.
    :returns: a web.py HTTP response object.
    """
    data, headers = _error_response(exc_cls, exc_msg)
    try:
        return HTTPError(status=codes[status_code],
                         headers=headers,
                         data=render_json(**data))
    except Exception:
        print(data)
        raise
コード例 #9
0
 def default_request(self):
     id   = xutils.get_argument("id", "")
     name = xutils.get_argument("name", "")
     if id == "" and name == "":
         raise HTTPError(504)
     if id != "":
         id = int(id)
         dao.visit_by_id(id)
         file = dao.get_by_id(id)
     elif name is not None:
         file = dao.get_by_name(name)
     if file is None:
         raise web.notfound()
     download_csv = file.related != None and "CODE-CSV" in file.related
     db = xtables.get_file_table()
     self.render("file/markdown_edit.html", file=file, 
         pathlist = dao.get_pathlist(db, file),
         content = file.get_content(), 
         date2str=date2str,
         download_csv = download_csv, 
         children = [])
コード例 #10
0
ファイル: httpstatus.py プロジェクト: five3/WTP
 def __init__(self, status, message=None):
     headers = {'Content-Type': 'text/html'}
     HTTPError.__init__(self, status, headers, message or self.message)
コード例 #11
0
ファイル: internalerror.py プロジェクト: UCL-INGI/INGInious
 def __init__(self, message):
     status = '500 Internal Server Error'
     headers = {'Content-Type': 'text/html'}
     HTTPError.__init__(self, status, headers, message)
コード例 #12
0
    def GET(self, op):
        id = xutils.get_argument("id", "")
        name = xutils.get_argument("name", "")
        page = xutils.get_argument("page", 1, type=int)
        pagesize = xutils.get_argument("pagesize", xconfig.PAGE_SIZE, type=int)
        db = xtables.get_file_table()
        user_name = xauth.get_current_name()
        show_add_file = False

        if id == "" and name == "":
            raise HTTPError(504)
        if id != "":
            id = int(id)
            file = dao.get_by_id(id, db=db)
        elif name is not None:
            file = dao.get_by_name(name, db=db)
        if file is None:
            raise web.notfound()

        if not file.is_public and user_name != "admin" and user_name != file.creator:
            raise web.seeother("/unauthorized")
        show_search_div = False
        pathlist = dao.get_pathlist(db, file)
        can_edit = (file.creator == user_name) or (user_name == "admin")
        role = xauth.get_current_role()

        # 定义一些变量
        files = []
        amount = 0
        template_name = "note/view.html"
        xconfig.note_history.put(
            dict(user=user_name, file_id=id, name=file.name))

        if file.type == "group":
            amount = db.count(
                where="parent_id=$id AND is_deleted=0 AND creator=$creator",
                vars=dict(id=file.id, creator=user_name))
            files = db.select(where=dict(parent_id=file.id,
                                         is_deleted=0,
                                         creator=user_name),
                              order="priority DESC, name",
                              limit=pagesize,
                              offset=(page - 1) * pagesize)
            content = file.content
            show_search_div = True
            show_add_file = True
        elif file.type == "md" or file.type == "text":
            content = file.content
            if op == "edit":
                template_name = "note/markdown_edit.html"
        else:
            content = file.content
            content = content.replace(u'\xad', '\n')
            content = content.replace(u'\n', '<br/>')
            file.data = file.data.replace(u"\xad", "\n")
            file.data = file.data.replace(u'\n', '<br/>')
            if file.data == None or file.data == "":
                file.data = content

        xmanager.fire("note.view", file)
        return xtemplate.render(template_name,
                                file=file,
                                op=op,
                                show_add_file=show_add_file,
                                can_edit=can_edit,
                                pathlist=pathlist,
                                page_max=math.ceil(amount / pagesize),
                                page=page,
                                page_url="/file/view?id=%s&page=" % id,
                                files=files)
コード例 #13
0
 def __init__(self, message):
     status = '500 Internal Server Error'
     headers = {'Content-Type': 'text/html'}
     HTTPError.__init__(self, status, headers, message)
コード例 #14
0
ファイル: http_response.py プロジェクト: BakaRei/pyprint
 def __init__(self):
     status = "401 Bad Request"
     headers = {'Content-type': 'application/json'}
     HTTPError.__init__(self, status, headers, self.message)
コード例 #15
0
ファイル: http_response.py プロジェクト: BakaRei/pyprint
 def __init__(self):
     status = "403 Forbidden"
     headers = {'Content-Type': 'application/json'}
     HTTPError.__init__(self, status, headers, self.message)
コード例 #16
0
ファイル: responses.py プロジェクト: maparco/numenta-apps
 def __init__(self, message=None):
   status = "405 Method Not Allowed"
   headers = {
     "Content-Type": "application/json; charset=UTF-8"
   }
   HTTPError.__init__(self, status, headers, jsonEncode(message or self.message))
コード例 #17
0
ファイル: view.py プロジェクト: Cicin411/xnote
    def GET(self, op):
        id            = xutils.get_argument("id", "")
        name          = xutils.get_argument("name", "")
        page          = xutils.get_argument("page", 1, type=int)
        pagesize      = xutils.get_argument("pagesize", xconfig.PAGE_SIZE, type=int)
        show_menu     = xutils.get_argument("show_menu", "true") != "false"
        orderby       = xutils.get_argument("orderby", "mtiem_desc")
        user_name     = xauth.get_current_name()
        show_add_file = False
        title         = None
        show_pagination = True
        show_search_div = False

        if id == "0":
            raise web.found("/")
        # 回收站的笔记也能看到
        if id == "" and name == "":
            raise HTTPError(504)
        if id != "":
            file = xutils.call("note.get_by_id", id)
        elif name is not None:
            file = xutils.call("note.get_by_name", name, db=db)
        if file is None:
            raise web.notfound()
        
        if file.type != "group" and not file.is_public and user_name != "admin" and user_name != file.creator:
            raise web.seeother("/unauthorized")
        pathlist        = xutils.call("note.list_path", file)
        can_edit        = (file.creator == user_name) or (user_name == "admin")
        role            = xauth.get_current_role()

        # 定义一些变量
        show_groups    = False
        show_mdate     = False
        files          = []
        recent_created = []
        groups         = []
        amount         = 0
        show_recommend = False
        template_name  = "note/view.html"
        next_note      = None
        prev_note      = None
        filelist       = None
        xconfig.note_history.put(dict(user=user_name, 
            link = "/note/view?id=%s" % id, 
            name = file.name))
        recommended_notes = []

        title  = file.name
        if file.type == "group":
            files  = xutils.call("note.list_by_parent", user_name, file.id, (page-1)*pagesize, pagesize, orderby)
            amount = xutils.call("note.count", user_name, file.id)
            content         = file.content
            show_search_div = True
            show_add_file   = True
            show_mdate      = True
        elif file.type == "md" or file.type == "text":
            content = file.content
            show_recommend = True
            show_pagination = False
            if op == "edit":
                show_recommend = False
                template_name = "note/editor/markdown_edit.html"
        else:
            content = file.content
            content = content.replace(u'\xad', '\n')
            content = content.replace(u'\n', '<br/>')
            file.data = file.data.replace(u"\xad", "\n")
            file.data = file.data.replace(u'\n', '<br/>')
            if file.data == None or file.data == "":
                file.data = content
            show_recommend = True
            show_pagination = False

        fpath = os.path.join(xconfig.UPLOAD_DIR, file.creator, str(file.parent_id), str(id))

        # 处理相册
        if file.type == "gallery":
            if os.path.exists(fpath):
                filelist = fsutil.list_files(fpath, webpath = True)
            else:
                filelist = []
            file.path = fpath

        if show_recommend and user_name is not None:
            show_groups = False
            # 推荐系统
            ctx = Storage(id=file.id, name = file.name, creator = file.creator, 
                content = file.content,
                parent_id = file.parent_id,
                result = [])
            xmanager.fire("note.recommend", ctx)
            recommended_notes = ctx.result

            next_note = xutils.call("note.find_next_note", file)
            prev_note = xutils.call("note.find_prev_note", file)
        
        xmanager.fire("note.view", file)
        show_aside = True
        if op == "edit":
            show_aside = False

        return xtemplate.render(template_name,
            show_aside    = show_aside,
            html_title    = title,
            file          = file, 
            path          = fpath,
            filelist      = filelist,
            note_id       = id,
            op            = op,
            show_mdate    = show_mdate,
            show_add_file = show_add_file,
            show_menu     = show_menu,
            show_pagination = show_pagination,
            can_edit = can_edit,
            pathlist = pathlist,
            page_max = math.ceil(amount/pagesize),
            page     = page,
            page_url = "/note/view?id=%s&orderby=%s&page=" % (id, orderby),
            files    = files, 
            recent_created    = recent_created,
            show_groups       = show_groups,
            groups            = groups,
            prev_note         = prev_note,
            next_note         = next_note,
            recommended_notes = recommended_notes)
 def __init__(self, message=None):
   status = "400 Bad Request"
   headers = {
     "Content-Type": "application/json; charset=UTF-8"
   }
   HTTPError.__init__(self, status, headers, jsonEncode(message or self.message))
コード例 #19
0
ファイル: view.py プロジェクト: zenistzw/xnote
    def GET(self, op):
        id = xutils.get_argument("id", "")
        name = xutils.get_argument("name", "")
        page = xutils.get_argument("page", 1, type=int)
        pagesize = xutils.get_argument("pagesize", xconfig.PAGE_SIZE, type=int)
        show_menu = xutils.get_argument("show_menu", "true") != "false"
        db = xtables.get_file_table()
        user_name = xauth.get_current_name()
        show_add_file = False
        title = None
        show_pagination = True
        show_search_div = False

        if id == "" and name == "":
            raise HTTPError(504)
        if id != "":
            id = int(id)
            file = dao.get_by_id(id, db=db)
        elif name is not None:
            file = dao.get_by_name(name, db=db)
        if file is None:
            raise web.notfound()
        if file.is_deleted == 1:
            raise web.seeother("/")

        if file.type != "group" and not file.is_public and user_name != "admin" and user_name != file.creator:
            raise web.seeother("/unauthorized")
        pathlist = dao.get_pathlist(db, file)
        can_edit = (file.creator == user_name) or (user_name == "admin")
        role = xauth.get_current_role()

        # 定义一些变量
        show_groups = False
        show_mdate = False
        files = []
        recent_created = []
        groups = []
        amount = 0
        show_recommend = False
        template_name = "note/view.html"
        next_note = None
        prev_note = None
        xconfig.note_history.put(
            dict(user=user_name, link="/note/view?id=%s" % id, name=file.name))
        recommended_notes = []

        title = file.name
        if file.type == "group":
            where_sql = "parent_id=$parent_id AND is_deleted=0 AND (creator=$creator OR is_public=1)"
            if xauth.is_admin():
                where_sql = "parent_id=$parent_id AND is_deleted=0"
            amount = db.count(where=where_sql,
                              vars=dict(parent_id=file.id, creator=user_name))
            files = db.select(where=where_sql,
                              vars=dict(parent_id=file.id,
                                        is_deleted=0,
                                        creator=user_name),
                              order="name",
                              limit=pagesize,
                              offset=(page - 1) * pagesize)
            content = file.content
            show_search_div = True
            show_add_file = True
            show_mdate = True
            # recent_created  = xutils.call("note.list_recent_created", file.id, 10)
        elif file.type == "md" or file.type == "text":
            content = file.content
            if op == "edit":
                template_name = "note/markdown_edit.html"
            show_recommend = True
            show_pagination = False
        else:
            content = file.content
            content = content.replace(u'\xad', '\n')
            content = content.replace(u'\n', '<br/>')
            file.data = file.data.replace(u"\xad", "\n")
            file.data = file.data.replace(u'\n', '<br/>')
            if file.data == None or file.data == "":
                file.data = content
            show_recommend = True
            show_pagination = False

        if show_recommend:
            show_groups = False
            # 推荐系统
            ctx = Storage(id=file.id,
                          name=file.name,
                          creator=file.creator,
                          content=file.content,
                          parent_id=file.parent_id,
                          result=[])
            xmanager.fire("note.recommend", ctx)
            recommended_notes = ctx.result

            next_note = xutils.call("note.find_next_note", file)
            prev_note = xutils.call("note.find_prev_note", file)

        xmanager.fire("note.view", file)
        show_aside = True
        if op == "edit":
            show_aside = False
        return xtemplate.render(template_name,
                                show_aside=show_aside,
                                html_title=title,
                                file=file,
                                note_id=id,
                                op=op,
                                show_mdate=show_mdate,
                                show_add_file=show_add_file,
                                show_menu=show_menu,
                                show_pagination=show_pagination,
                                can_edit=can_edit,
                                pathlist=pathlist,
                                page_max=math.ceil(amount / pagesize),
                                page=page,
                                page_url="/note/view?id=%s&page=" % id,
                                files=files,
                                recent_created=recent_created,
                                show_groups=show_groups,
                                groups=groups,
                                prev_note=prev_note,
                                next_note=next_note,
                                recommended_notes=recommended_notes)
コード例 #20
0
 def __init__(self, log_message=None):
     HTTPError.__init__(self, 400, log_message)
コード例 #21
0
ファイル: view.py プロジェクト: 552301/xnote
    def GET(self, op, id = None):
        if id is None:
            id = xutils.get_argument("id", "")
        name          = xutils.get_argument("name", "")
        page          = xutils.get_argument("page", 1, type=int)
        pagesize      = xutils.get_argument("pagesize", xconfig.PAGE_SIZE, type=int)
        show_menu     = xutils.get_argument("show_menu", "true") != "false"
        show_search   = xutils.get_argument("show_search", "true") != "false"
        orderby       = xutils.get_argument("orderby", None)
        is_iframe     = xutils.get_argument("is_iframe", "false")
        user_name     = xauth.current_name()
        show_add_file = False
        title         = None
        show_pagination = True
        show_search_div = False

        kw = Storage()
        kw.show_left   = False
        kw.show_groups = False
        kw.show_aside  = True
        kw.groups = []
        kw.recommended_notes = []

        if id == "0":
            raise web.found("/")
        # 回收站的笔记也能看到
        if id == "" and name == "":
            raise HTTPError(504)
        if id != "":
            file = NOTE_DAO.get_by_id(id)
        elif name is not None:
            file = NOTE_DAO.get_by_name(name)
        if file is None:
            raise web.notfound()
        
        if file.type != "group" and not file.is_public and user_name != "admin" and user_name != file.creator:
            raise web.seeother("/unauthorized")
        pathlist        = xutils.call("note.list_path", file)
        can_edit        = (file.creator == user_name) or (user_name == "admin")
        role            = xauth.get_current_role()

        # 定义一些变量
        show_mdate     = False
        files          = []
        recent_created = []
        amount         = 0
        show_recommend = False
        template_name  = "note/view.html"
        next_note      = None
        prev_note      = None

        title  = file.name
        if file.type == "group":
            if orderby != None and file.orderby != orderby:
                NOTE_DAO.update(where = dict(id = file.id, creator = file.creator), orderby = orderby)
            else:
                orderby = file.orderby

            files  = NOTE_DAO.list_by_parent(user_name, file.id, (page-1)*pagesize, pagesize, orderby)
            amount = NOTE_DAO.count(user_name, file.id)
            content         = file.content
            show_search_div = True
            show_add_file   = True
            show_mdate      = True
            kw.show_aside   = False
        elif file.type == "md" or file.type == "text":
            content = file.content
            show_recommend = True
            show_pagination = False
            if op == "edit":
                show_recommend = False
                template_name = "note/editor/markdown_edit.html"
        elif file.type == "list":
            kw.show_aside = False
            show_pagination = False
        else:
            # post/html 等其他类型
            handle_note_content(file)
            show_recommend = True
            show_pagination = False

        # 处理笔记背后的文件系统
        handle_note_files(kw, file)

        if show_recommend and user_name is not None:
            # 推荐系统
            handle_note_recommend(kw, file, user_name)
            
        
        xmanager.fire("note.view", file)
        if op == "edit":
            kw.show_aside = False

        if is_iframe == "true":
            show_menu = False
            show_search = False

        kw.show_menu   = show_menu
        kw.show_search = show_search

        # 如果是页面,需要查出上级目录列表
        handle_left_dir(kw, user_name, file, op)

        return xtemplate.render(template_name,
            html_title    = title,
            file          = file, 
            note_id       = id,
            op            = op,
            show_mdate    = show_mdate,
            show_add_file = show_add_file,
            show_pagination = show_pagination,
            can_edit = can_edit,
            pathlist = pathlist,
            page_max = math.ceil(amount/pagesize),
            page     = page,
            page_url = "/note/view?id=%s&orderby=%s&page=" % (id, orderby),
            files    = files, 
            recent_created    = recent_created,
            is_iframe         = is_iframe, **kw)
コード例 #22
0
ファイル: responses.py プロジェクト: darian19/what
 def __init__(self, message=None):
   status = "403 Forbidden"
   headers = {
     "Content-Type": "application/json; charset=UTF-8"
   }
   HTTPError.__init__(self, status, headers, encodeJson(message or self.message))
コード例 #23
0
ファイル: http_response.py プロジェクト: BakaRei/pyprint
 def __init__(self):
     status = "401 Unauthorized"
     headers = {'Content-type': 'application/json'}
     HTTPError.__init__(self, status, headers, self.message)
 def __init__(self, message=None):
     status = "405 Method Not Allowed"
     headers = {"Content-Type": "application/json; charset=UTF-8"}
     HTTPError.__init__(self, status, headers,
                        encodeJson(message or self.message))
コード例 #25
0
ファイル: http_response.py プロジェクト: BakaRei/pyprint
 def __init__(self):
     status = "201 Created"
     headers = {'Content-Type': 'application/json'}
     HTTPError.__init__(self, status, headers, self.message)
コード例 #26
0
ファイル: validators.py プロジェクト: wmark/anzu
 def __init__(self, log_message=None):
     HTTPError.__init__(self, 400, log_message)