Ejemplo n.º 1
0
Archivo: adif.py Proyecto: LANJr4D/FRED
    def _render(self, action='', ctx=None):
        context = DictLookup()
        context.approot = '/'
        context.classname = self.classname
        context.classroot = "%s%s/" % (context.approot, context.classname)
        context.corba_server = cherrypy.session.get('corba_server_name')
        context.request = cherrypy.request
        context.history = cherrypy.session.get('history', False)

        user = cherrypy.session.get('user', None)
        if user:
            context.user = user
            # None for Login page that has no menu.
            context.menu = self._get_menu(action) or None
            context.body_id = self._get_selected_menu_body_id(action)

        if ctx:
            context.update(ctx)

        temp_class = self._template(action)(context)
        result = temp_class.render()

        return result
Ejemplo n.º 2
0
    def _render(self, action='', ctx=None):
        context = DictLookup()
        context.approot = '/'
        context.classname = self.classname
        context.classroot = "%s%s/" % (context.approot, context.classname)
        context.corba_server = cherrypy.session.get('corba_server_name')
        context.request = cherrypy.request
        context.history = cherrypy.session.get('history', False)

        user = cherrypy.session.get('user', None)
        if user:
            context.user = user
            # None for Login page that has no menu.
            context.menu = self._get_menu(action) or None
            context.body_id = self._get_selected_menu_body_id(action)

        if ctx:
            context.update(ctx)

        temp_class = self._template(action)(context)
        result = temp_class.render()

        return result
    def detail(self, *args, **kwd):
        # path can be 'detail/ID/' or 'detail/ID/resolve/'
        if (not 1 <= len(args) <= 2) or (len(args) > 1
                                         and args[1] != 'resolve'):
            return self._render('404_not_found')

        check_handle = args[0]
        if len(args) > 1:
            # cache lock is just helping users so they don't work on the same Check, but it's optional:
            if cache:
                cache_key = RESOLVE_LOCK_CACHE_KEY % check_handle
                stored = cache.add(
                    cache_key, cherrypy.session['user'].login,
                    config.verification_check_lock_default_duration)
                current_resolving_user = cache.get(cache_key)
                # resolve only if memcache is not running (return value 0) or lock was acquired (return value True) or
                # the current user is the user who has the lock:
                if (stored == 0 and type(stored) == type(0)) or stored is True \
                    or current_resolving_user == cherrypy.session['user'].login:
                    resolve = True
                else:
                    messages.warning(
                        'This check is currently being resolved by the user "%s"'
                        % current_resolving_user)
                    raise cherrypy.HTTPRedirect(f_urls['contactcheck'] +
                                                'detail/%s/' % check_handle)
            else:
                resolve = True
        else:  # read only mode
            resolve = False

        post_data = kwd if cherrypy.request.method == 'POST' else None

        if resolve and post_data:
            req_type = 'ContactCheckUpdateTestStatuses'
        else:
            req_type = 'ContactCheckDetail'

        log_req = create_log_request(
            req_type, properties=[['check_handle', check_handle]])
        out_props = []

        check = None
        try:
            check = c2u(cherrypy.session['Verification'].getContactCheckDetail(
                check_handle))
            if resolve:
                check_nperm_func('change.contactcheck_%s' %
                                 check.test_suite_handle,
                                 raise_err=True)
            else:
                check_nperm_func('read.contactcheck_%s' %
                                 check.test_suite_handle,
                                 raise_err=True)

            if resolve:
                if self._is_check_post_closed(check):
                    messages.warning(
                        _('This contact check was already resolved.'))
                    raise cherrypy.HTTPRedirect(f_urls['contactcheck'] +
                                                'detail/%s/' %
                                                check.check_handle)
                elif self._is_check_pre_run(check) and check.status_history[
                        -1].status != 'enqueue_req':
                    messages.warning(_('This contact check was not yet run.'))
                    raise cherrypy.HTTPRedirect(f_urls['contactcheck'] +
                                                'detail/%s/' %
                                                check.check_handle)

                initial = {
                    test_data.test_handle: test_data.status_history[-1].status
                    for test_data in check.test_list
                }
                form = self._generate_update_tests_form_class(check)(
                    post_data, initial=initial)
                if form.is_valid():
                    changed_statuses = {}
                    for test_data in check.test_list:
                        status_in_form = form.cleaned_data[
                            test_data.test_handle]
                        if status_in_form != test_data.status_history[
                                -1].status:
                            changed_statuses[
                                test_data.test_handle] = status_in_form
                    if changed_statuses:
                        cherrypy.session[
                            'Verification'].updateContactCheckTests(
                                u2c(check.check_handle),
                                u2c([
                                    Registry.AdminContactVerification.
                                    ContactTestUpdate(test_handle, status)
                                    for test_handle, status in
                                    changed_statuses.items()
                                ]), u2c(log_req.request_id))
                    log_req.result = 'Success'
                    out_props += [['changed_statuses', '']] + [[
                        key, val, True
                    ] for key, val in changed_statuses.items()]
                    self._update_check(check, post_data)
                else:
                    log_req.result = 'Fail'
            else:
                form = None

            log_req.result = 'Success'

            detail = VerificationCheckDetail(check=check,
                                             resolve=resolve,
                                             form=form)

            try:
                contact_detail = get_detail('contact', check.contact_id)
            except ccReg.Admin.ObjectNotFound:
                contact_detail = None

            context = DictLookup({
                'test_suit_name':
                ContactCheckEnums.SUITE_NAMES.get(check.test_suite_handle),
                'check':
                check,
                'contact_url':
                f_urls['contact'] + 'detail/?id=%s' % check.contact_id,
                'detail':
                detail,
                'contact_detail':
                contact_detail,
                'ajax_json_filter_url':
                f_urls['contactcheck'] + 'json_filter/%s/' % check.contact_id,
            })
            if cherrypy.session.get('history', False):
                context.update({
                    'table_tag':
                    self._get_checks_table_tag(),
                    'messages_list':
                    self._get_check_messages_list(check)
                })
            return self._render('detail', ctx=context)
        except Registry.AdminContactVerification.INVALID_CHECK_HANDLE:
            log_req.result = 'Fail'
            return self._render('404_not_found')
        finally:
            log_req.close(properties=out_props,
                          references=[('contact',
                                       check.contact_id)] if check else None)