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


Python ServerSettings.user_aware方法代码示例

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


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

示例1: upload_handler

# 需要导入模块: from explorer.utils.misc import ServerSettings [as 别名]
# 或者: from explorer.utils.misc.ServerSettings import user_aware [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

示例2: admin_action

# 需要导入模块: from explorer.utils.misc import ServerSettings [as 别名]
# 或者: from explorer.utils.misc.ServerSettings import user_aware [as 别名]
    def admin_action(username, payload, request):
        logger.info("ModuleAdmin.admin_action: enter (%s -> %s)" % (username, request))

        if payload is None:
            logger.error('ModuleAdmin.admin_action: invalid payload in request !!')
            return False, "Invalid payload !!"

        modified = False
        modules = ET.fromstring(payload)

        if request == 'graph':
            return dependencies_graph(username, modules)

        users = User.objects.filter(username=username)
        if not users:
            logger.error("ModuleAdmin.admin_action: invalid user " + username)
            return False, 'Unknown User %s !!' % username

        user = users[0]
        if not ServerSettings.user_aware():
            if (request == 'delete') and not user.has_perm('explorer.delete_yangmodel'):
                return False, 'User %s does not have permission to delete models!!' % username

        for module in modules:
            name = module.text.split('.yang')[0]

            logger.debug("ModuleAdmin.admin_action: %s ->  %s" % (request, name))

            # delete modules from user profile
            if request in ['delete', 'unsubscribe']:
                if UserProfile.objects.filter(user=user, module=name).exists():
                    profile = UserProfile.objects.filter(user=user, module=name)
                    profile.delete()
                    logger.debug('Module %s deleted for user %s' % (module.text, username))

            # delete yang and cxml files for delete request
            if request == 'delete':
                for _type in [('cxml', '.xml'), ('yang', '.yang')]:
                    _file = os.path.join('data', 'users', username, _type[0], name + _type[1])
                    if os.path.exists(_file):
                        os.remove(_file)
                        modified = True
                        logging.debug('Deleted %s (user: %s)' % (_file, username))

            if request == 'subscribe':
                if not is_browsable(username, name):
                    logger.debug('Module %s can not be subscribed ' % (module.text))
                    continue

                if not UserProfile.objects.filter(user=user, module=name).exists():
                    profile = UserProfile(user=user, module=name)
                    profile.save()
                    logger.debug('User %s subscribed to %s module ..' % (username, module.text))
                else:
                    logger.debug('User %s already subscribed to %s module ' % (username, module.text))

        # if any yang model modified, delete dependency file
        if modified:
            _file = os.path.join(ServerSettings.yang_path(username), 'dependencies.xml')
            if os.path.exists(_file):
                os.remove(_file)
                logger.debug('Deleted dependency file %s (user: %s)' % (_file, username))

        return True, None
开发者ID:CiscoDevNet,项目名称:yang-explorer,代码行数:66,代码来源:admin.py


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