当前位置: 首页>>代码示例>>Python>>正文


Python Response.error方法代码示例

本文整理汇总了Python中explorer.utils.misc.Response.error方法的典型用法代码示例。如果您正苦于以下问题:Python Response.error方法的具体用法?Python Response.error怎么用?Python Response.error使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在explorer.utils.misc.Response的用法示例。


在下文中一共展示了Response.error方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: login_handler

# 需要导入模块: from explorer.utils.misc import Response [as 别名]
# 或者: from explorer.utils.misc.Response import error [as 别名]
def login_handler(request):
    """ HTTP Request handler function for user login / logout requests """
    if request.POST:
        action = request.POST['action']
        if action == 'login':
            username = request.POST['username']
            password = request.POST['password']
            user = authenticate(username=username, password=password)
            if user is not None and user.is_active:
                # Correct password, and the user is marked "active"
                login(request, user)
            else:
                return HttpResponse(Response.error('login', 'Authentication Failed'))
        else:
            username = ''
            try:
                if request.session.session_key is not None and request.session.session_key != '':
                    session_dir = ServerSettings.session_path(request.session.session_key)
                    if os.path.exists(session_dir):
                        logger.debug('Cleaning ' + session_dir)
                        shutil.rmtree(session_dir)
                logout(request)
            except:
                logger.exception("Failed")
            else:
                logger.debug('Logout success!!')
        session = get_session_config(username)
        return HttpResponse(Response.success(action, 'ok', session))
    return HttpResponse(Response.error('unknown', 'Invalid request!!'))
开发者ID:CiscoDevNet,项目名称:yang-explorer,代码行数:31,代码来源:views.py

示例2: login_handler

# 需要导入模块: from explorer.utils.misc import Response [as 别名]
# 或者: from explorer.utils.misc.Response import error [as 别名]
def login_handler(request):
    """ HTTP Request handler function for user login / logout requests """
    session = ET.Element('session')
    if request.POST:
        action = request.POST['action']
        if action == 'login':
            username = request.POST['username']
            password = request.POST['password']
            user = authenticate(username=username, password=password)
            if user is not None and user.is_active:
                # Correct password, and the user is marked "active"
                login(request, user)
                session.text = username
            else:
                return HttpResponse(Response.error('login', 'Authentication Failed'))
        else:
            try:
                if request.session.session_key is not None and request.session.session_key != '':
                    session_dir = ServerSettings.session_path(request.session.session_key)
                    if os.path.exists(session_dir):
                        logging.debug('Cleaning ' + session_dir)
                        shutil.rmtree(session_dir)
                logout(request)
            except Exception as ex:
                print(ex.__doc__)
                print(ex.message)
        logging.debug('Login success')
        return HttpResponse(Response.success(action, 'ok', session))
    return HttpResponse(Response.error(action, 'Invalid Request'))
开发者ID:MFALHI,项目名称:yang-explorer,代码行数:31,代码来源:views.py

示例3: request_handler

# 需要导入模块: from explorer.utils.misc import Response [as 别名]
# 或者: from explorer.utils.misc.Response import error [as 别名]
def request_handler(request):
    """ HTTP Request handler function to handle actions on collections """

    if not request.user.is_authenticated():
        return HttpResponse(Response.error(None, 'User must be logged in'))

    mode = request.GET.get('mode', '')
    reply_xml = None

    logger.info('request_handler: Received Request: (%s)' % mode)

    if mode == 'get-collection-list':
        reply_xml = Collection.list()

    elif mode == 'load-collection':
        metadata = request.GET.get('metadata', '')
        reply_xml = Collection.load(request.user.username, metadata)

        if reply_xml is None:
            return HttpResponse(Response.error(mode, 'Failed'))

        if isinstance(reply_xml, str):
            return HttpResponse(Response.success(mode, reply_xml))

    elif mode == 'add-collection':
        metadata = request.GET.get('metadata', '')
        payload = request.GET.get('payload', '')
        logger.debug('metadata: ' + metadata)
        logger.debug('payload: ' + payload)
        if not Collection.add(metadata, payload):
            return HttpResponse(Response.error(mode, 'Failed'))

        reply_xml = Collection.list()
        mode = 'get-collection-list'

    elif mode == 'delete-collection':
        metadata = request.GET.get('metadata', '')
        if not Collection.remove(metadata):
            return HttpResponse(Response.error(mode, 'Failed'))

        reply_xml = Collection.list()
        mode = 'get-collection-list'

    elif mode == 'rpc':
        req = request.GET.get('payload', '')
        reply_xml = Adapter.gen_rpc(request.user.username, req)
        if isinstance(reply_xml, str):
            return HttpResponse(Response.success(mode, reply_xml))
    elif mode == 'gen-script':
        req = request.GET.get('payload', '')
        reply_xml = Adapter.gen_script(request.user.username, req)
        if isinstance(reply_xml, str):
            return HttpResponse(Response.success(mode, reply_xml))

    elif mode in ['get-cap', 'run-rpc', 'run-edit-commit', 'run-commit']:
        payload = request.GET.get('payload', '')
        logger.debug('run: ' + payload)
        reply_xml = Adapter.run_request(request.user.username, payload)

    return HttpResponse(Response.success(mode, 'ok', reply_xml))
开发者ID:sachinsr099,项目名称:yang-explorer,代码行数:62,代码来源:views.py

示例4: upload_handler

# 需要导入模块: from explorer.utils.misc import Response [as 别名]
# 或者: from explorer.utils.misc.Response import error [as 别名]
def upload_handler(request):
    """ HTTP Request handler function to upload yang models """

    mode = request.GET.get('mode', '')
    logging.debug(request.method + ':Received upload request .. ' + mode)

    if not request.user.is_authenticated():
        logging.warning('User must be logged in !!')
        return HttpResponse(Response.error(mode, 'Unauthorized'))

    if not ServerSettings.user_aware():
        if not request.user.has_perm('explorer.delete_yangmodel') or \
                not request.user.has_perm('explorer.change_yangmodel'):
            logging.warning('Unauthorized upload request .. ')
            return HttpResponse(Response.error(mode, 'User does not have permission to upload !!'))

    if request.method == 'POST':
        # create a temporary storage for this session
        directory = ServerSettings.session_path(request.session.session_key)
        _file = Uploader.upload_file(request.FILES['Filedata'], directory)
        if _file is not None:
            module = ET.Element('module')
            module.text = _file
            rval = Response.success('upload', 'ok', xml=module)
            logging.debug(rval)
            return HttpResponse(rval)
        return HttpResponse(Response.error('upload', 'Failed to upload'))
    elif request.method == 'GET':
        if mode == 'sync':
            filename = request.GET.get('file', '')
            index = request.GET.get('index', '')
            logging.info('Received sync request for ' + filename + ', index ' + index)
            success, response = Uploader.sync_file(request.user.username,
                                                   request.session.session_key,
                                                   filename, index)
            if success:
                return HttpResponse(Response.success(mode, 'ok'))
            return HttpResponse(Response.error(mode, 'compilation failed', xml=response))

        elif mode == 'commit':
            success, modules = Uploader.commit_files(request.user.username, request.session.session_key)
            if success:
                return HttpResponse(Response.success('commit', 'ok', modules))

        elif mode == 'init':
            success, modules = Uploader.get_upload_files(request.user.username, request.session.session_key)
            if success:
                return HttpResponse(Response.success(mode, 'ok', modules))

        elif mode == 'clear':
            success, modules = Uploader.clear_upload_files(request.user.username, request.session.session_key)
            if success:
                return HttpResponse(Response.success(mode, 'ok', modules))
        return HttpResponse(Response.error(mode, 'failed'))

    return render_to_response('upload.html')
开发者ID:center3d,项目名称:yang-explorer,代码行数:58,代码来源:views.py

示例5: upload_handler

# 需要导入模块: from explorer.utils.misc import Response [as 别名]
# 或者: from explorer.utils.misc.Response import error [as 别名]
def upload_handler(request):
    """ HTTP Request handler function to upload yang models """
    if not request.user.is_authenticated():
        logging.debug('Unauthorized upload request .. ')
        return HttpResponse(Response.error('upload', 'Unauthorized'))

    mode = request.GET.get('mode', '')
    logging.debug(request.method + ':Received upload request .. ' + mode)
   
    if request.method == 'POST':
        # create a temporary storage for this session
        directory = os.path.join('data', 'session', request.session.session_key)
        _file = upload_file(request.FILES['Filedata'], directory)
        if _file is not None:
            module = ET.Element('module')
            module.text = _file
            rval = Response.success('upload', 'ok', xml=module)
            logging.debug(rval)
            return HttpResponse(rval)
        return HttpResponse(Response.error('upload', 'Failed to upload'))
    elif request.method == 'GET':
        if mode == 'sync':
            filename = request.GET.get('file', '')
            index = request.GET.get('index', '')
            logging.debug('Received sync request for ' + filename + ', index ' + index)
            success, response = sync_file(request.user.username, request.session.session_key,
                                          filename, index)
            if success:
                return HttpResponse(Response.success(mode, 'ok'))
            return HttpResponse(Response.error(mode, 'compilation failed', xml=response))
        
        elif mode == 'commit':
            success, modules = commit_files(request.user.username, request.session.session_key)
            if success:
                return HttpResponse(Response.success('commit', 'ok', modules))
        
        elif mode == 'init':
            success, modules = get_upload_files(request.user.username, request.session.session_key)
            if success:
                return HttpResponse(Response.success(mode, 'ok', modules))

        elif mode == 'clear':
            success, modules = clear_upload_files(request.user.username, request.session.session_key)
            if success:
                return HttpResponse(Response.success(mode, 'ok', modules))
        return HttpResponse(Response.error(mode, 'failed'))       

    return render_to_response('upload.html')
开发者ID:janlindblad,项目名称:yang-explorer,代码行数:50,代码来源:views.py

示例6: download_yang

# 需要导入模块: from explorer.utils.misc import Response [as 别名]
# 或者: from explorer.utils.misc.Response import error [as 别名]
def download_yang(request, req):
    '''
    This API download yang schema from device
    '''
    logging.debug('Download Yang Schema')

    req = req.replace('<metadata>', '')
    req = req.replace('</metadata>', '')

    protocol, device, fmt, payload = Adapter.parse_request(req)
    if device.get('host', None) is None:
        return HttpResponse(Response.error('download', 'no host info'))

    session_dir = ServerSettings.schema_path(request.session.session_key)
    if not os.path.exists(session_dir):
        os.makedirs(session_dir)
    if not os.path.exists(session_dir):
        return HttpResponse(Response.error('download', 'No session directory'))

    for fname in os.listdir(session_dir):
        if fname.endswith('.yang'):
            fn = os.path.join(session_dir, fname)
            os.remove(fn)

    modules = ET.Element('modules')
    reqxml = ET.fromstring(req)
    schemas = reqxml.find('schemas')
    for sc in schemas:
        id = sc.text
        module = ET.Element('module')
        get_sc = ET.Element('get-schema')
        get_sc.set("xmlns", "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring")
        identifier = ET.Element("identifier")
        sfile = id.split('@')[0]
        identifier.text = sfile
        module.text = id+'.yang'
        get_sc.append(identifier)
        rpc.append(get_sc)
        modules.append(module)
        schema = Adapter.run_netconf(request.user.username, device, rpc)
        fname = os.path.join(session_dir, id+'.yang')
        with open(fname, 'w') as f:
            f.write(schema[0][0].text)
        rpc.remove(get_sc)

    return modules
开发者ID:center3d,项目名称:yang-explorer,代码行数:48,代码来源:schema.py

示例7: download_yang

# 需要导入模块: from explorer.utils.misc import Response [as 别名]
# 或者: from explorer.utils.misc.Response import error [as 别名]
def download_yang(request, req):
    """
    This API download yang schema from device
    """
    logger.debug("Download Yang Schema")

    req = req.replace("<metadata>", "")
    req = req.replace("</metadata>", "")

    protocol, device, fmt, _, payload = Adapter.parse_request(req)
    if device.get("host", None) is None:
        return HttpResponse(Response.error("download", "Netconf agent address missing!!"))

    # clear session directory if it exists
    Uploader.clear_upload_files(None, request.session.session_key)

    # create session directory if it does not exist
    session_dir = Uploader.create_session_storage(request.session.session_key)
    if session_dir is None:
        logger.error("download_yang: Invalid session")
        return HttpResponse(Response.error("download", "Invalid session_id"))

    # extact list of models from request
    req_xml = ET.fromstring(req)
    models = [sc.text.strip() for sc in req_xml.find("schemas")]

    # download all models recursively
    rpc = ET.fromstring(get_schema_rpc)
    download_helper(request.user.username, device, session_dir, rpc, models)

    # prepare list of downloaded models
    modules = ET.Element("modules")
    for _file in glob.glob(os.path.join(session_dir, "*.yang")):

        # see if we need to rename file with revision date
        parser = Parser(_file)
        new_fname = os.path.join(session_dir, parser.get_filename())
        if _file != new_fname:
            os.rename(_file, new_fname)

        module = ET.Element("module")
        module.text = os.path.basename(new_fname)
        modules.append(module)
    return modules
开发者ID:CiscoDevNet,项目名称:yang-explorer,代码行数:46,代码来源:schema.py

示例8: get_schema

# 需要导入模块: from explorer.utils.misc import Response [as 别名]
# 或者: from explorer.utils.misc.Response import error [as 别名]
def get_schema(request, req, all=True):
    """
    This API get yang schema from device
    """
    logger.debug("Get Yang Schema")

    req = req.replace("<metadata>", "")
    req = req.replace("</metadata>", "")

    protocol, device, fmt, _, payload = Adapter.parse_request(req)
    if device.get("host", None) is None:
        return HttpResponse(Response.error("get", "no host info"))

    rpc_xml = ET.fromstring(get_schema_list_rpc)
    xml = Adapter.run_netconf(request.user.username, device, rpc_xml)
    if xml is None:
        return HttpResponse(Response.error("get", "failed to get schema"))

    if xml.text is not None:
        if xml.text.find("error"):
            return HttpResponse(Response.error("get", xml.text))

    sclist = xml[0][0][0][0]
    schemas = ET.Element("schemas")
    for sc in sclist:
        schema = ET.Element("schema")
        id = sc[0].text
        ver = sc[1].text
        if all == False:
            if id.isupper() or "-MIB" in id or "SNMP" in id:
                continue
        schema.set("name", id)
        if ver is None:
            schema.set("version", "")
        else:
            schema.set("version", ver)
        unmatched = validate_schema(request.user.username, id, ver)
        if unmatched is not None:
            schema.set("unmatched", unmatched)

        schemas.append(schema)

    return HttpResponse(Response.success("get", "ok", xml=schemas))
开发者ID:CiscoDevNet,项目名称:yang-explorer,代码行数:45,代码来源:schema.py

示例9: get_schema

# 需要导入模块: from explorer.utils.misc import Response [as 别名]
# 或者: from explorer.utils.misc.Response import error [as 别名]
def get_schema(request, req, all=False):
    '''
    This API get yang schema from device
    '''
    logging.debug('Get Yang Schema')

    req = req.replace('<metadata>', '')
    req = req.replace('</metadata>', '')

    protocol, device, fmt, payload = Adapter.parse_request(req)
    if device.get('host', None) is None:
        return HttpResponse(Response.error('get', 'no host info'))

    rpc_xml= ET.fromstring(get_schema_rpc)
    xml = Adapter.run_netconf(request.user.username, device, rpc_xml) 
    if xml is None:
        return HttpResponse(Response.error('get', 'failed to get schema'))
 
    if xml.text is not None:
        if xml.text.find('error'):
            return HttpResponse(Response.error('get', xml.text))

    sclist = xml[0][0][0][0]
    schemas = ET.Element('schemas')
    for sc in sclist:
        schema = ET.Element('schema')
        id  = sc[0].text
        ver = sc[1].text
        if all == False:
            if id.isupper() or '-MIB' in id or 'SNMP' in id:
                continue
        schema.set('name', id)
        if ver is None:
            schema.set('version', '')
        else:
            schema.set('version', ver)
        unmatched = validate_schema(request.user.username, id, ver)
        if unmatched is not None:
            schema.set('unmatched', unmatched)

        schemas.append(schema) 

    return HttpResponse(Response.success('get', 'ok', xml=schemas))
开发者ID:center3d,项目名称:yang-explorer,代码行数:45,代码来源:schema.py

示例10: admin_handler

# 需要导入模块: from explorer.utils.misc import Response [as 别名]
# 或者: from explorer.utils.misc.Response import error [as 别名]
def admin_handler(request):
    """ HTTP Request handler function to handle actions on yang modules """

    if not request.user.is_authenticated():
        return HttpResponse(Response.error(None, 'User must be logged in'))

    if request.method != 'GET':
        return HttpResponse(Response.error(None, 'Invalid admin Request'))

    action = request.GET.get('action', '')
    logging.debug('Received admin request %s for user %s' % (action, request.user.username))

    if action in ['subscribe', 'unsubscribe', 'delete']:
        payload = request.GET.get('payload', None)
        if not ModuleAdmin.admin_action(request.user.username, payload, action):
            return HttpResponse(Response.error(action, 'Failed to %s' % action))

    modules = ModuleAdmin.get_modules(request.user.username)
    return HttpResponse(Response.success('modulelist', 'ok', xml=modules))
开发者ID:janlindblad,项目名称:yang-explorer,代码行数:21,代码来源:views.py

示例11: search_handler

# 需要导入模块: from explorer.utils.misc import Response [as 别名]
# 或者: from explorer.utils.misc.Response import error [as 别名]
def search_handler(request):
    """
    Args:
        request: Django HTTP request header

    Returns: HTTP response with search results
    """
    if not request.user.is_authenticated():
        return HttpResponse(Response.error(None, 'User must be logged in'))

    query = request.GET.get('query', '')
    mode = request.GET.get('mode', '')
    if not query:
        rc, result = False, 'Invalid or empty query'
    else:
        rc, result = Search.search(request.user.username, query)

    if not rc:
        return HttpResponse(Response.error(mode, result))

    return HttpResponse(Response.success(mode, 'ok', xml=result))
开发者ID:danhut,项目名称:yang-explorer,代码行数:23,代码来源:views.py

示例12: request_handler

# 需要导入模块: from explorer.utils.misc import Response [as 别名]
# 或者: from explorer.utils.misc.Response import error [as 别名]
def request_handler(request):
    """ HTTP Request handler function to handle actions on collections """

    mode = request.GET.get('mode', '')
    reply_xml = None

    logging.debug('request_handler: Received Request: (%s)' % mode)

    if mode == 'get-collection-list':
        reply_xml = Collection.list()

    elif mode == 'load-collection':
        metadata = request.GET.get('metadata', '')
        reply_xml = Collection.load(metadata)

        if reply_xml is None:
            return HttpResponse(Response.error(mode, 'Failed'))

    elif mode == 'add-collection':
        metadata = request.GET.get('metadata', '')
        payload = request.GET.get('payload', '')
        if not Collection.add(metadata, payload):
            return HttpResponse(Response.error(mode, 'Failed'))

        reply_xml = Collection.list()
        mode = 'get-collection-list'

    elif mode == 'delete-collection':
        metadata = request.GET.get('metadata', '')
        if not Collection.remove(metadata):
            return HttpResponse(Response.error(mode, 'Failed'))

        reply_xml = Collection.list()
        mode = 'get-collection-list'

    elif mode in ['get-cap', 'run-rpc']:
        payload = request.GET.get('payload', '')
        reply_xml = Adapter.run_request(payload)

    return HttpResponse(Response.success(mode, 'ok', reply_xml))
开发者ID:janlindblad,项目名称:yang-explorer,代码行数:42,代码来源:views.py

示例13: login_handler

# 需要导入模块: from explorer.utils.misc import Response [as 别名]
# 或者: from explorer.utils.misc.Response import error [as 别名]
def login_handler(request):
    """ HTTP Request handler function for user login / logout requests """
    session = ET.Element('session')
    if request.POST:
        action = request.POST['action']
        if action == 'login':
            username = request.POST['username']
            password = request.POST['password']
            user = authenticate(username=username, password=password)
            if user is not None and user.is_active:
                # Correct password, and the user is marked "active"
                login(request, user)
                session.text = username
            else:
                return HttpResponse(Response.error('login', 'Authentication Failed'))
        else:
            try:
                logout(request)
            except Exception as ex:
                print(ex.__doc__)
                print(ex.message)
        return HttpResponse(Response.success(action, 'ok', session))
    return HttpResponse(Response.error(action, 'Invalid Request'))
开发者ID:pgohite,项目名称:yang-explorer,代码行数:25,代码来源:views.py

示例14: admin_handler

# 需要导入模块: from explorer.utils.misc import Response [as 别名]
# 或者: from explorer.utils.misc.Response import error [as 别名]
def admin_handler(request):
    """ HTTP Request handler function to handle actions on yang modules """

    if not request.user.is_authenticated():
        return HttpResponse(Response.error(None, 'User must be logged in'))

    if request.method != 'GET':
        return HttpResponse(Response.error(None, 'Invalid admin Request'))

    action = request.GET.get('action', '')
    logger.info('Received admin request %s for user %s' % (action, request.user.username))

    if action in ['subscribe', 'unsubscribe', 'delete', 'graph']:
        payload = request.GET.get('payload', None)
        print(str(payload))
        (rc, msg) = ModuleAdmin.admin_action(request.user.username, payload, action)
        if not rc:
            return HttpResponse(Response.error(action, msg))

    if action == 'graph':
        return HttpResponse(Response.success(action, msg))

    modules = ModuleAdmin.get_modules(request.user.username)
    return HttpResponse(Response.success(action, 'ok', xml=modules))
开发者ID:sachinsr099,项目名称:yang-explorer,代码行数:26,代码来源:views.py

示例15: schema_handler

# 需要导入模块: from explorer.utils.misc import Response [as 别名]
# 或者: from explorer.utils.misc.Response import error [as 别名]
def schema_handler(request):
    '''
    Handle schema request from UI.
    '''
    logging.debug("schema_handler: enter")
    req = request.GET.get('payload', '')
    action = request.GET.get('action', '')
    logging.debug('Recieved schema Request (%s)' % action)

    if not request.user.is_authenticated():
        logging.error('User must be logged in !!')
        return HttpResponse(Response.error(action, 'Unauthorized'))
    if action == 'get-schema':
        return get_schema(request, req)
    elif action == 'get-all-schema':
        return get_schema(request, req, all=True)
    elif action == 'download-schema':
        return download_schema(request, req)
    elif action == 'add-schema':
        return add_schema(request, req)
开发者ID:center3d,项目名称:yang-explorer,代码行数:22,代码来源:views.py


注:本文中的explorer.utils.misc.Response.error方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。