Example #1
0
    def section(self, serverid=1, sectionid='1'):
        "Settings section"
        server = self._get_server(serverid)
        if not server:
            abort(404)

        if not int(sectionid) in CONFIG_SECTIONS:
            abort(404)

        c.serverid = serverid
        c.sectionid = sectionid
        c.scanner = server
        c.sections = CONFIG_SECTIONS
        c.form = settings_forms[sectionid](request.POST, csrf_context=session)
        if not request.POST:
            for field in c.form:
                if (sectionid == '1' and '_' in field.name
                    and not field.name == 'csrf_token'):
                    internal = global_settings_dict[field.name]
                else:
                    internal = field.name
                conf = self._get_setting(serverid, internal)
                if conf:
                    attr = getattr(c.form, field.name)
                    attr.data = conf.value

        updated = None
        if request.POST and c.form.validate():
            for field in c.form:
                if field.data and not field.name == 'csrf_token':
                    if sectionid == '1':
                        if '_' in field.name:
                            external = global_settings_dict[field.name]
                            internal = external
                        else:
                            external = CONFIG_RE.sub('', field.label.text)
                            internal = field.name
                    else:
                        external = CONFIG_RE.sub('', field.label.text)
                        internal = field.name
                    conf = self._get_setting(serverid, internal)
                    if conf is None:
                        if field.data != field.default:
                            conf = ConfigSettings(
                                        internal=internal,
                                        external=external,
                                        section=sectionid
                                    )
                            conf.value = field.data
                            conf.server = server
                            updated = True
                            Session.add(conf)
                            Session.commit()
                            subs = dict(svr=server.hostname,
                                        s=external,
                                        a=conf.value)
                            info = HOSTSETTING_MSG % subs
                            audit_log(c.user.username,
                                    3, info, request.host,
                                    request.remote_addr, datetime.now())
                    else:
                        if conf.value != field.data:
                            conf.value = field.data
                            updated = True
                            Session.add(conf)
                            Session.commit()
                            subs = dict(svr=conf.server.hostname,
                                        s=external,
                                        a=conf.value)
                            info = HOSTSETTING_MSG % subs
                            audit_log(c.user.username,
                                    2, info, request.host,
                                    request.remote_addr, datetime.now())
            if updated:
                flash(_('%(settings)s updated') % dict(
                settings=CONFIG_SECTIONS[int(sectionid)]))
                update_serial.delay()
            else:
                flash_info(_('No configuration changes made'))
            #redirect(url('settings-scanner', serverid=serverid))
        return render('/settings/section.html')
Example #2
0
    def section(self, serverid=1, sectionid='1'):
        "Settings section"
        server = self._get_server(serverid)
        if not server:
            abort(404)

        if not int(sectionid) in CONFIG_SECTIONS:
            abort(404)

        c.serverid = serverid
        c.sectionid = sectionid
        c.scanner = server
        c.sections = CONFIG_SECTIONS
        c.form = settings_forms[sectionid](request.POST, csrf_context=session)
        if not request.POST:
            for field in c.form:
                if (sectionid == '1' and '_' in field.name
                        and not field.name == 'csrf_token'):
                    internal = global_settings_dict[field.name]
                else:
                    internal = field.name
                conf = self._get_setting(serverid, internal)
                if conf:
                    attr = getattr(c.form, field.name)
                    attr.data = conf.value

        updated = None
        if request.POST and c.form.validate():
            for field in c.form:
                if field.data and not field.name == 'csrf_token':
                    if sectionid == '1':
                        if '_' in field.name:
                            external = global_settings_dict[field.name]
                            internal = external
                        else:
                            external = CONFIG_RE.sub(u'',
                                                     unicode(field.label.text))
                            internal = field.name
                    else:
                        external = CONFIG_RE.sub(u'',
                                                 unicode(field.label.text))
                        internal = field.name
                    conf = self._get_setting(serverid, internal)
                    if conf is None:
                        if field.data != field.default:
                            conf = ConfigSettings(internal=internal,
                                                  external=external,
                                                  section=sectionid)
                            conf.value = field.data
                            conf.server = server
                            updated = True
                            Session.add(conf)
                            Session.commit()
                            subs = dict(svr=server.hostname,
                                        s=external,
                                        a=conf.value)
                            info = HOSTSETTING_MSG % subs
                            audit_log(c.user.username, 3, info, request.host,
                                      request.remote_addr, now())
                    else:
                        if conf.value != field.data:
                            conf.value = field.data
                            updated = True
                            Session.add(conf)
                            Session.commit()
                            subs = dict(svr=conf.server.hostname,
                                        s=external,
                                        a=conf.value)
                            info = HOSTSETTING_MSG % subs
                            audit_log(c.user.username, 2, info, request.host,
                                      request.remote_addr, now())
            if updated:
                flash(
                    _('%(settings)s updated') %
                    dict(settings=CONFIG_SECTIONS[int(sectionid)]))
                update_serial.delay()
            else:
                flash_info(_('No configuration changes made'))
            #redirect(url('settings-scanner', serverid=serverid))
        return render('/settings/section.html')
Example #3
0
    def section(self, serverid=1, sectionid='1'):
        "Settings section"
        server = self._get_server(serverid)
        if not server:
            abort(404)

        if not int(sectionid) in CONFIG_SECTIONS:
            abort(404)

        c.serverid = serverid
        c.sectionid = sectionid
        c.scanner = server
        c.sections = CONFIG_SECTIONS
        c.form = settings_forms[sectionid](request.POST, csrf_context=session)
        if not request.method == 'POST':
            for field in c.form:
                if (sectionid == '1' and '_' in field.name
                    and not field.name == 'csrf_token'):
                    internal = global_settings_dict[field.name]
                else:
                    internal = field.name
                conf = self._get_setting(serverid, internal)
                if conf:
                    attr = getattr(c.form, field.name)
                    attr.data = conf.value

        updated = None
        if request.method == 'POST' and c.form.validate():
            for field in c.form:
                if field.type == 'SelectMultipleField' or \
                        (field.data and not field.name == 'csrf_token'):
                    if sectionid == '1':
                        if '_' in field.name:
                            external = global_settings_dict[field.name]
                            internal = external
                        else:
                            external = CONFIG_RE.sub(u'',
                                        unicode(field.label.text))
                            internal = field.name
                    else:
                        external = CONFIG_RE.sub(u'',
                                    unicode(field.label.text))
                        internal = field.name
                    conf = self._get_setting(serverid, internal)
                    if conf is None:
                        if (field.type == 'SelectMultipleField' and
                            len(field.data) and field.data != field.default)\
                            or (field.type != 'SelectMultipleField' and
                                field.data != field.default):
                            check_field(field)
                            conf = ConfigSettings(
                                        internal=internal,
                                        external=external,
                                        section=sectionid
                                    )
                            conf.value = field.data
                            conf.server = server
                            updated = True
                            Session.add(conf)
                            Session.commit()
                            subs = dict(svr=server.hostname,
                                        s=external,
                                        a=conf.value)
                            info = auditmsgs.HOSTSETTING_MSG % subs
                            audit_log(c.user.username,
                                    3, unicode(info), request.host,
                                    request.remote_addr,
                                    arrow.utcnow().datetime)
                    else:
                        subs = dict(svr=conf.server.hostname,
                                    s=external,
                                    a=conf.value)
                        info = auditmsgs.HOSTSETTING_MSG % subs
                        check_value = get_check_value(field.type, conf.value)
                        if check_value != field.data:
                            # stored value not equal to updated value
                            if (field.type == 'SelectMultipleField' and
                                len(field.data) and
                                field.data != field.default) or \
                                (field.type != 'SelectMultipleField' and
                                field.data != field.default):
                                # not a default, we can update
                                check_field(field)
                                conf.value = field.data
                                updated = True
                                Session.add(conf)
                                Session.commit()
                                audit_log(c.user.username,
                                    2, unicode(info), request.host,
                                    request.remote_addr,
                                    arrow.utcnow().datetime)
                            else:
                                # is the default lets delete the stored value
                                Session.delete(conf)
                                Session.commit()
                                updated = True
                                audit_log(c.user.username,
                                        4, unicode(info), request.host,
                                        request.remote_addr,
                                        arrow.utcnow().datetime)

            if updated:
                flash(_('%(settings)s updated') % dict(
                    settings=CONFIG_SECTIONS[int(sectionid)]))
                create_ms_settings.apply_async(exchange=FANOUT_XCHG)
                update_serial.delay()
            else:
                flash_info(_('No configuration changes made'))
            # redirect(url('settings-scanner', serverid=serverid))
        elif request.method == 'POST' and not c.form.validate():
            msg = _("Error detected, check the settings below")
            flash_alert(msg)
            log.info(msg)
        return self.render('/settings/section.html')