Esempio n. 1
0
 def _backend_classtemplate_get(self, *args, **kwargs):
     verb = kwargs.get('verb', None)
     if verb is not None:
         try:
             params = web.input()
             backend_id = params.get('backend_id', None)
             template_id = params.get('template_id', None)
             if backend_id is not None:
                 backend = backends.backend_get({'_id': backend_id})
                 transport = get_xmlrpc_transport(backend['backend_url'],
                                                  backend['is_kerberos'])
                 s = ClassTemplates(transport)
                 template_list = s.get(id=template_id)
                 if template_list is not None:
                     result = self._prepare_output(result={
                         'backend_id': backend_id,
                         'datalist': template_list
                     })
                     return result
         except KerberosError as e:
             (first, last) = e.message
             (message, error_no) = last
             result = self._prepare_output(result={'backend_id': backend_id,
                                                   'error': True,
                                                   'error_type': 'Kerberos',
                                                   'error_msg': message,
                                                   'error_no': error_no})
             return result
     result = self._prepare_output(result={'backend_id': backend_id,
                                           'datalist': []})
     return result
Esempio n. 2
0
class BackendClassTemplatesController(AdminController):
    CONTROLLER_IDENT = {'title': 'DC2 Backends Classtemplates',
                        'url': '/admin/backends/classtemplates',
                        'show_in_menu': 'False'}

    def _init_backend(self, backend):
        self._transport = get_xmlrpc_transport(backend['backend_url'],
                                               backend['is_kerberos'])
        self._defaultclasses = DefaultClasses(self._transport)
        self._classtemplates = ClassTemplates(self._transport)

    @needs_auth
    @needs_admin
    @Logger(logger=logger)
    def _index(self, *args, **kwargs):
        params = web.input()
        verb = kwargs.get('verb', None)
        page = self._prepare_page(verb)
        backend_list = backends.backend_list()
        backend_id = params.get('backend_id', None)
        backend = backends.backend_get({'_id': backend_id})
        self._init_backend(backend)
        classtemplates = self._classtemplates.list()
        page.set_title('DC2 Admincenter - Backends - Class Templates - Index')
        page.add_page_data({
            'backendlist': backend_list,
            'backend_id': backend_id,
            'backend_classtemplates': classtemplates
        })
        page.set_action('index')
        result = self._prepare_output(verb['request_type'],
                                      verb['request_content_type'],
                                      output={'content': page.render()})
        return result

    @needs_auth
    @needs_admin
    @Logger(logger=logger)
    def _new(self, *args, **kwargs):
        params = web.input()
        verb = kwargs.get('verb', None)
        page = self._prepare_page(verb)
        backend_list = backends.backend_list()
        backend_id = params.get('backend_id', None)
        backend = backends.backend_get({'_id': backend_id})
        self._init_backend(backend)
        defaultclasses = self._defaultclasses.list()
        classtemplate = self._classtemplates.new()
        page.set_title('DC2 Admindcenter - Backends - Class Templates - Add')
        page.add_page_data({
            'backendlist': backend_list,
            'backend_id': backend_id,
            'defaultclasses': defaultclasses,
            'tmpl': classtemplate,
        })
        page.set_action('new')
        result = self._prepare_output(verb['request_type'],
                                      verb['request_content_type'],
                                      output={'content': page.render()})
        return result

    @needs_auth
    @needs_admin
    @Logger(logger=logger)
    def _edit(self, *args, **kwargs):
        params = web.input()
        verb = kwargs.get('verb', None)
        page = self._prepare_page(verb)
        backend_list = backends.backend_list()
        backend_id = params.get('backend_id', None)
        backend = backends.backend_get({'_id': backend_id})
        self._init_backend(backend)
        defaultclasses = self._defaultclasses.list()
        classtemplate = self._classtemplates.get(id=verb['request_data']['id'])
        if classtemplate['classes'] is not None:
            for classe in classtemplate['classes']:
                for k in xrange(len(defaultclasses)):
                    if defaultclasses[k]['classname'] == classe:
                        a = defaultclasses[k]
                        defaultclasses.remove(a)
                        break
        else:
            classtemplate['classes'] = []
        page.set_title('DC2 Admindcenter - Backends - Class Templates - Edit')
        page.add_page_data({
            'backendlist': backend_list,
            'backend_id': backend_id,
            'defaultclasses': defaultclasses,
            'tmpl': classtemplate,
        })
        page.set_action('edit')
        result = self._prepare_output(verb['request_type'],
                                      verb['request_content_type'],
                                      output={'content': page.render()})
        return result

    @needs_auth
    @needs_admin
    @Logger(logger=logger)
    def _create(self, *args, **kwargs):
        params = web.input()
        verb = kwargs.get('verb', None)
        backend_id = params.get('backend_id', None)
        backend = backends.backend_get({'_id': backend_id})
        self._init_backend(backend)
        result = json.loads(web.data())
        classtemplate = {}
        classtemplate['name'] = result['result']['classtemplate']['name']
        classtemplate['description'] = result[
            'result']['classtemplate']['description']
        classtemplate['classes'] = result['result']['classtemplate']['classes']
        self._classtemplates.add(classtemplate=classtemplate)
        output_format = verb.get('request_output_format', None)
        if output_format.lower() == 'json':
            result = self._prepare_output('json',
                                          verb['request_content_type'],
                                          verb['request_output_format'],
                                          {'redirect':
                                           {'url':
                                            '{0}?backend_id={1}'.format(
                                                self._controller_path,
                                                backend_id),
                                            'absolute': 'true'}})
        else:
            result = self._prepare_output(
                verb['request_type'],
                verb['request_content_type'],
                output={'redirect': {
                    'url': '{0}?backend_id={1}'.format(
                        self._controller_path,
                        backend_id),
                    'absolute': 'true'}})
        return result

    @needs_auth
    @needs_admin
    @Logger(logger=logger)
    def _update(self, *args, **kwargs):
        params = web.input()
        verb = kwargs.get('verb', None)
        backend_id = params.get('backend_id', None)
        backend = backends.backend_get({'_id': backend_id})
        self._init_backend(backend)
        tmpl_id = verb['request_data']['id']
        result = json.loads(web.data())
        classtemplate = {}
        classtemplate['_id'] = tmpl_id
        classtemplate['name'] = result['result']['classtemplate']['name']
        classtemplate['description'] = result[
            'result']['classtemplate']['description']
        classtemplate['classes'] = result['result']['classtemplate']['classes']
        self._classtemplates.update(classtemplate=classtemplate)
        output_format = verb.get('request_output_format', None)
        if output_format.lower() == 'json':
            result = self._prepare_output('json',
                                          verb['request_content_type'],
                                          verb['request_output_format'],
                                          {'redirect':
                                           {'url': '{0}?backend_id={1}'.format(
                                            self._controller_path,
                                            backend_id),
                                            'absolute': 'true'}})
        else:
            result = self._prepare_output(
                verb['request_type'],
                verb['request_content_type'],
                output={'redirect': {
                    'url': '{0}?backend_id={1}'.format(
                        self._controller_path,
                        backend_id),
                    'absolute': 'true'}})
        return result

    @needs_auth
    @needs_admin
    @Logger(logger=logger)
    def _delete(self, *args, **kwargs):
        params = web.input()
        verb = kwargs.get('verb', None)
        backend_id = params.get('backend_id', None)
        backend = backends.backend_get({'_id': backend_id})
        self._init_backend(backend)
        tmpl_id = verb['request_data']['id']
        result = self._classtemplates.delete(id=tmpl_id)
        output_format = verb.get('request_output_format', None)
        if output_format.lower() == 'json':
            result = self._prepare_output('json',
                                          verb['request_content_type'],
                                          verb['request_output_format'],
                                          {'redirect':
                                           {'url': '{0}?backend_id={1}'.format(
                                            self._controller_path,
                                            backend_id),
                                            'absolute': 'true'}})
        else:
            result = self._prepare_output(
                verb['request_type'],
                verb['request_content_type'],
                output={'redirect': {
                    'url': '{0}?backend_id={1}'.format(
                        self._controller_path,
                        backend_id),
                    'absolute': 'true'}})
        return result