コード例 #1
0
def _add_or_edit_rlms(rlmstype, rlmsversion, id):
    if not is_supported(rlmstype, rlmsversion):
        return "Not supported"

    rlms_version = _get_rlms_version(rlmstype, rlmsversion)
    if rlms_version is None:
        return render_template("labmanager_admin/rlms_errors.html")

    if id is not None:
        rlms = db_session.query(RLMS).filter_by(id = id).first()
        if rlms is None or rlms.rlms_version != rlms_version:
            return render_template("labmanager_admin/rlms_errors.html")


    AddForm = get_form_class(rlmstype, rlmsversion)
    form = AddForm(id is None)

    if form.validate_on_submit():
        configuration_dict = {}
        for field in form.get_field_names():
            if field not in ('location', 'name'):
                configuration_dict[field] = getattr(form, field).data

        configuration = json.dumps(configuration_dict)
        
        if id is None:
            new_rlms = RLMS(name = form.name.data, location = form.location.data, rlms_version = rlms_version, configuration = configuration)
            db_session.add(new_rlms)
        else:
            rlms = db_session.query(RLMS).filter_by(id = id).first()
            if rlms is None:
                return render_template("labmanager_admin/rlms_errors.html")
            rlms.name          = form.name.data
            rlms.location      = form.location.data
            rlms.configuration = AddForm.process_configuration(rlms.configuration, configuration)

        db_session.commit()
        return redirect(url_for('admin_rlms_rlms', rlmstype = rlmstype, rlmsversion = rlmsversion))

    if id is not None:
        rlms = db_session.query(RLMS).filter_by(id = id).first()
        if rlms is None:
            return render_template("labmanager_admin/rlms_errors.html")

        form.name.data     = rlms.name
        form.location.data = rlms.location
        if rlms.configuration is not None and rlms.configuration != '':
            configuration = json.loads(rlms.configuration)
            for key in configuration:
                getattr(form, key).data = configuration[key]

    return render_template("labmanager_admin/rlms_rlms_add.html", rlmss = rlms_version.rlms, name = rlms_version.rlms_type.name, version = rlms_version.version, form = form)
コード例 #2
0
    def _add_or_edit(self, rlms, version, add_or_edit, obj, config):
        edit_id = request.args.get('id')
        form_class = get_form_class(rlms, version)
        form = form_class(add_or_edit=add_or_edit, obj=obj)
        error_messages = []
        if form.validate_on_submit():
            configuration = config
            for key in form.get_field_names():
                if key not in dir(forms.AddForm):
                    field = getattr(form, key)
                    is_password = '******' in unicode(field.type).lower()
                    # If we're editing, and this field is a password, do not change it
                    if is_password and not add_or_edit and field.data == '':
                        continue
                    configuration[key] = field.data
            config_json = json.dumps(configuration)

            current_rlms_id = None if add_or_edit else edit_id
            ManagerClass = get_manager_class(rlms, version, current_rlms_id)
            rlms_instance = ManagerClass(config_json)
            if hasattr(rlms_instance, 'test'):
                try:
                    error_messages = rlms_instance.test() or []
                except Exception as e:
                    error_messages.append(
                        u'%s%s' % (gettext("Error testing the RLMS:"), e))
                    traceback.print_exc()

            if form.publicly_available.data and len(
                    form.public_identifier.data) == 0:
                form.public_identifier.errors = [
                    gettext(
                        "If the RLMS is public, it must have a public identifier"
                    )
                ]
                error_messages.append(gettext("Invalid public identifier"))
            elif form.publicly_available.data and len(
                    form.public_identifier.data) < 4:
                form.public_identifier.errors = [
                    gettext(
                        "If the RLMS is public, it must have a public identifier with at least 4 characters"
                    )
                ]
                error_messages.append(gettext("Invalid public identifier"))

            elif form.publicly_available.data:  # If publicly available, retrieve existing RLMS with that public identifier
                existing_objects = self.session.query(RLMS).filter_by(
                    public_identifier=form.public_identifier.data).all()
                if not add_or_edit:  # If editing, don't count the one being edited
                    existing_objects = [
                        existing_obj for existing_obj in existing_objects
                        if unicode(existing_obj.id) != unicode(edit_id)
                    ]
                if existing_objects:
                    form.public_identifier.errors = [
                        gettext("That identifier is already taken")
                    ]
                    error_messages.append(
                        gettext(
                            "Use other identifier or don't make the RLMS public"
                        ))

            if not error_messages:
                if add_or_edit:
                    rlms_obj = RLMS(
                        kind=rlms,
                        version=version,
                        name=form.name.data,
                        url=form.url.data,
                        location=form.location.data,
                        configuration=config_json,
                        publicly_available=form.publicly_available.data,
                        public_identifier=form.public_identifier.data,
                        default_autoload=form.default_autoload.data)
                else:
                    rlms_obj = self.session.query(RLMS).filter_by(
                        id=edit_id).first()
                    rlms_obj.url = form.url.data
                    rlms_obj.location = form.location.data
                    rlms_obj.name = form.name.data
                    rlms_obj.default_autoload = form.default_autoload.data
                    rlms_obj.publicly_available = form.publicly_available.data
                    rlms_obj.public_identifier = form.public_identifier.data
                    rlms_obj.configuration = config_json

                self.session.add(rlms_obj)
                try:
                    self.session.commit()
                except:
                    self.session.rollback()
                    raise

                if add_or_edit:
                    rlms_id = rlms_obj.id
                else:
                    rlms_id = edit_id

                labs_url = url_for('.labs', id=rlms_id, _external=True)
                if rlms == http_plugin.PLUGIN_NAME:
                    if add_or_edit:
                        # First, store the rlms identifier in the database in the context_id
                        configuration['context_id'] = rlms_id
                        config_json = json.dumps(configuration)
                        rlms_obj.configuration = config_json
                        try:
                            self.session.commit()
                        except:
                            self.session.rollback()
                            raise

                    # Then, re-create the manager class and call setup
                    rlms_instance = ManagerClass(config_json)

                    try:
                        setup_url = rlms_instance.setup(back_url=labs_url)
                    except Exception as e:
                        flash(
                            gettext(
                                "Couldn't load the setup URL! (this usually means that the plug-in is not correctly configured). Error message: %s"
                                % e))
                        return redirect(url_for('.edit_view', id=rlms_id))
                    else:
                        return redirect(setup_url)

                return redirect(labs_url)

        if not add_or_edit and rlms == http_plugin.PLUGIN_NAME:
            setup_url = url_for('.plugin_setup', rlms_id=edit_id)
        else:
            setup_url = None

        return self.render('labmanager_admin/create-rlms-step-2.html',
                           name=rlms,
                           version=version,
                           form=form,
                           fields=form.get_field_names(),
                           error_messages=error_messages,
                           edit_id=edit_id,
                           setup_url=setup_url)
コード例 #3
0
ファイル: admin.py プロジェクト: gateway4labs/labmanager
    def _add_or_edit(self, rlms, version, add_or_edit, obj, config):
        edit_id = request.args.get('id')
        form_class = get_form_class(rlms, version)
        form = form_class(add_or_edit=add_or_edit, obj = obj)
        error_messages = []
        if form.validate_on_submit():
            configuration = config
            for key in form.get_field_names():
                if key not in dir(forms.AddForm):
                    field = getattr(form, key)
                    is_password = '******' in unicode(field.type).lower()
                    # If we're editing, and this field is a password, do not change it
                    if is_password and not add_or_edit and field.data == '':
                        continue
                    configuration[key] = field.data
            config_json = json.dumps(configuration)
            
            current_rlms_id = None if add_or_edit else edit_id
            ManagerClass = get_manager_class(rlms, version, current_rlms_id)
            rlms_instance = ManagerClass(config_json)
            if hasattr(rlms_instance, 'test'):
                try:
                    error_messages = rlms_instance.test() or []
                except Exception as e:
                    error_messages.append(u'%s%s' % (gettext("Error testing the RLMS:"), e))
                    traceback.print_exc()

            if form.publicly_available.data and len(form.public_identifier.data) == 0:
                form.public_identifier.errors = [gettext("If the RLMS is public, it must have a public identifier")]
                error_messages.append(gettext("Invalid public identifier"))
            elif form.publicly_available.data and len(form.public_identifier.data) < 4:
                form.public_identifier.errors = [gettext("If the RLMS is public, it must have a public identifier with at least 4 characters")]
                error_messages.append(gettext("Invalid public identifier"))

            elif form.publicly_available.data: # If publicly available, retrieve existing RLMS with that public identifier
                existing_objects = self.session.query(RLMS).filter_by(public_identifier = form.public_identifier.data).all()
                if not add_or_edit: # If editing, don't count the one being edited
                    existing_objects = [ existing_obj for existing_obj in existing_objects if unicode(existing_obj.id) != unicode(edit_id)]
                if existing_objects:
                    form.public_identifier.errors = [gettext("That identifier is already taken")]
                    error_messages.append(gettext("Use other identifier or don't make the RLMS public"))


            if not error_messages:
                if add_or_edit:
                    rlms_obj = RLMS(kind = rlms, version = version, name = form.name.data,
                                url = form.url.data, location = form.location.data,
                                configuration = config_json, 
                                publicly_available = form.publicly_available.data,
                                public_identifier = form.public_identifier.data,
                                default_autoload = form.default_autoload.data)
                else:
                    rlms_obj = self.session.query(RLMS).filter_by(id = edit_id).first()
                    rlms_obj.url = form.url.data
                    rlms_obj.location = form.location.data
                    rlms_obj.name = form.name.data
                    rlms_obj.default_autoload = form.default_autoload.data
                    rlms_obj.publicly_available = form.publicly_available.data
                    rlms_obj.public_identifier = form.public_identifier.data
                    rlms_obj.configuration = config_json


                self.session.add(rlms_obj)
                try:
                    self.session.commit()
                except:
                    self.session.rollback()
                    raise
                
                if add_or_edit:
                    rlms_id = rlms_obj.id
                else:
                    rlms_id = edit_id
    
                labs_url = url_for('.labs', id = rlms_id, _external = True)
                if rlms == http_plugin.PLUGIN_NAME:
                    if add_or_edit:
                        # First, store the rlms identifier in the database in the context_id
                        configuration['context_id'] = rlms_id
                        config_json = json.dumps(configuration)
                        rlms_obj.configuration = config_json
                        try:
                            self.session.commit()
                        except:
                            self.session.rollback()
                            raise
                    
                    # Then, re-create the manager class and call setup
                    rlms_instance = ManagerClass(config_json)
                    
                    try:
                        setup_url = rlms_instance.setup(back_url = labs_url)
                    except Exception as e:
                        flash(gettext("Couldn't load the setup URL! (this usually means that the plug-in is not correctly configured). Error message: %s" % e))
                        return redirect(url_for('.edit_view', id = rlms_id))
                    else:
                        return redirect(setup_url)

                return redirect(labs_url)

        if not add_or_edit and rlms == http_plugin.PLUGIN_NAME:
            setup_url = url_for('.plugin_setup', rlms_id = edit_id)
        else:
            setup_url = None

        return self.render('labmanager_admin/create-rlms-step-2.html', name = rlms, version = version, form = form, fields = form.get_field_names(), error_messages = error_messages, edit_id = edit_id, setup_url = setup_url)
コード例 #4
0
def _add_or_edit_rlms(rlmstype, rlmsversion, id):
    if not is_supported(rlmstype, rlmsversion):
        return "Not supported"

    rlms_version = _get_rlms_version(rlmstype, rlmsversion)
    if rlms_version is None:
        return render_template("labmanager_admin/rlms_errors.html")

    if id is not None:
        rlms = db_session.query(RLMS).filter_by(id=id).first()
        if rlms is None or rlms.rlms_version != rlms_version:
            return render_template("labmanager_admin/rlms_errors.html")

    AddForm = get_form_class(rlmstype, rlmsversion)
    form = AddForm(id is None)

    if form.validate_on_submit():
        configuration_dict = {}
        for field in form.get_field_names():
            if field not in ('location', 'name'):
                configuration_dict[field] = getattr(form, field).data

        configuration = json.dumps(configuration_dict)

        if id is None:
            new_rlms = RLMS(name=form.name.data,
                            location=form.location.data,
                            rlms_version=rlms_version,
                            configuration=configuration)
            db_session.add(new_rlms)
        else:
            rlms = db_session.query(RLMS).filter_by(id=id).first()
            if rlms is None:
                return render_template("labmanager_admin/rlms_errors.html")
            rlms.name = form.name.data
            rlms.location = form.location.data
            rlms.configuration = AddForm.process_configuration(
                rlms.configuration, configuration)

        db_session.commit()
        return redirect(
            url_for('admin_rlms_rlms',
                    rlmstype=rlmstype,
                    rlmsversion=rlmsversion))

    if id is not None:
        rlms = db_session.query(RLMS).filter_by(id=id).first()
        if rlms is None:
            return render_template("labmanager_admin/rlms_errors.html")

        form.name.data = rlms.name
        form.location.data = rlms.location
        if rlms.configuration is not None and rlms.configuration != '':
            configuration = json.loads(rlms.configuration)
            for key in configuration:
                getattr(form, key).data = configuration[key]

    return render_template("labmanager_admin/rlms_rlms_add.html",
                           rlmss=rlms_version.rlms,
                           name=rlms_version.rlms_type.name,
                           version=rlms_version.version,
                           form=form)