Beispiel #1
0
def upload():
    err_msg = get_stats()
    if err_msg:
        flash('No Vector is Connected. Error message: ' + err_msg, 'warning')

    vector_status = Status.query.first()
    form = UploadScript()
    applications = Application.query.all()

    if form.validate_on_submit():
        if applications:
            for application in applications:
                if application.script_name.lower() == \
                        form.script_name.data.lower():
                    flash(
                        'Application named "' + application.script_name +
                        '" already exists, please rename the existing \
                          application and try again.', 'warning')
                    return redirect(url_for('application_system.upload'))

        if form.script.data:
            random_hex = save_script(form.script.data)
            if form.script_helpers.data:
                for helper in form.script_helpers.data:
                    is_in_db = save_script_helpers(helper, random_hex)
                    if is_in_db is True:
                        flash("Helper File Already in Lib!", 'warning')
                        return redirect(url_for('application_system.upload'))

            if form.icon.data:
                icon_fn = save_icon(form.icon.data, random_hex)

            else:
                icon_fn = 'default.png'

            application = Application(hex_id=random_hex,
                                      script_name=form.script_name.data,
                                      description=form.description.data,
                                      icon=icon_fn,
                                      run_in_bkrd=form.run_in_bkrd.data,
                                      author=form.author.data,
                                      website=form.website.data)
            db.session.add(application)
            db.session.commit()
            settings_file = AppSupport(hex_id=application.hex_id,
                                       file_name=application.hex_id + '.ini')
            db.session.add(settings_file)
            db.session.commit()
            settings_file_fn = os.path.join(lib_folder,
                                            application.hex_id + '.ini')
            config_file = open(settings_file_fn, "w+")
            config_file.write('[' + application.script_name + ']\n')
            flash("Application Saved!", 'success')
            return redirect(url_for('main.home'))
        else:
            flash("No script uploaded", 'warning')
        return redirect(url_for('application_system.upload'))
    elif request.method == 'GET':
        form.script_helpers.data = None

    return render_template('applications/upload.html',
                           title='Upload',
                           form=form,
                           vector_status=vector_status,
                           sdk_version=sdk_version)
Beispiel #2
0
def home():
    prompt = AppPrompt.query.first()
    if prompt:
        if prompt.question:
            return redirect(url_for('main.prompt'))

    settings = Settings.query.first()
    search_term = None
    num_results = 0

    if sdk_version != vectorcloud_sdk_version:
        flash(
            'You are using a different version of the SDK than VectorCloud!\
               You are using ' + sdk_version + ' VectorCloud is using ' +
            vectorcloud_sdk_version, 'warning')

    output = Output.query.all()
    for out in output:
        flash(out, 'success')
    db.session.query(Output).delete()
    db.session.commit()

    app_list = Application.query.all()
    store_app_list = ApplicationStore.query.all()

    for store_app in store_app_list:
        for main_app in app_list:
            if store_app.script_name.lower() == main_app.script_name.lower():
                store_app.installed = True
                db.session.merge(store_app)
                db.session.commit()
            else:
                store_app.installed = False

    app_support = AppSupport.query.all()

    for main_app in app_list:
        for settings_file in app_support:
            if main_app.hex_id in settings_file.file_name:
                set_db_settings(main_app.hex_id, settings_file)

    form = CommandForm()
    search_form = SearchForm()
    edit_form = UploadScript()
    settings_form = AppSettings()

    if request.method == 'POST':
        form_name = request.form['form-name']

        if form_name == 'command-form':
            robot_command = Command(command=form.command.data)
            db.session.add(robot_command)
            db.session.commit()
            return redirect(url_for('main.home'))

        elif form_name == 'search-form':
            settings.search_by_name = search_form.by_name.data
            settings.search_by_description = search_form.by_description.data
            settings.search_by_author = search_form.by_author.data
            db.session.merge(settings)
            db.session.commit()
            search_term = search_form.search.data
            apps_searched = []

            if search_form.by_name.data is True:
                for application in app_list:
                    if search_term.lower() in application.script_name.lower():
                        apps_searched.append(application.script_name)

            if search_form.by_description.data is True:
                for application in app_list:
                    if search_term.lower() in application.description.lower():
                        apps_searched.append(application.script_name)

            if search_form.by_author.data is True:
                for application in app_list:
                    if search_term.lower() in application.author.lower():
                        apps_searched.append(application.script_name)

            app_list = Application.query\
                .filter(Application.script_name.in_(apps_searched))
            apps_searched = set(apps_searched)
            num_results = len(apps_searched)

        elif form_name == 'settings-form':
            hex_id = settings_form.hex_id.data
            settings_file_fn = join(lib_folder, hex_id + '.ini')
            settings_file = open(settings_file_fn, "w")
            settings_file.write(settings_form.variable.data)
            settings_file.close()
            flash('Settings saved!', 'success')
            return redirect(url_for('main.home'))

        elif form_name == 'edit-form':
            script_hex_id = edit_form.hex_id.data
            application = Application.query.filter_by(
                hex_id=script_hex_id).first()

            application.run_in_bkrd = edit_form.run_in_bkrd.data
            application.script_name = edit_form.script_name.data
            application.author = edit_form.author.data
            application.website = edit_form.website.data
            application.description = edit_form.description.data
            db.session.merge(application)
            db.session.commit()
            flash('Application Edited!', 'success')
            return redirect(url_for('main.home'))

        elif form_name == 'files-form':
            script_hex_id = edit_form.hex_id.data
            application = Application.query.filter_by(
                hex_id=script_hex_id).first()

            if edit_form.script.data:
                scriptn = script_hex_id + '.py'
                script_path = join(root_folder, scriptn)
                remove(script_path)
                edit_form.script.data.save(script_path)

            if edit_form.icon.data:
                if application.icon != 'default.png':
                    icon_path = join(app.root_path, 'static/app_icons',
                                     application.icon)
                    remove(icon_path)

                icon_fn = save_icon(edit_form.icon.data, script_hex_id)
                application.icon = icon_fn

            if edit_form.script_helpers.data:
                for helper in edit_form.script_helpers.data:
                    is_in_db = save_script_helpers(helper, script_hex_id)
                    if is_in_db is True:
                        flash("Helper File Already in Lib!", 'warning')
                        return redirect(url_for('main.home'))

            flash('Files Edited!', 'success')

    elif request.method == 'GET':
        search_form.by_name.data = settings.search_by_name
        search_form.by_description.data = settings.search_by_description
        search_form.by_author.data = settings.search_by_author

    command_list = Command.query.all()

    err_msg = get_stats()
    if err_msg:
        flash('No Vector is Connected. Error message: ' + err_msg, 'warning')

    vector_status = Status.query.first()
    support_files = AppSupport.query.all()

    return render_template('home.html',
                           vector_status=vector_status,
                           form=form,
                           command_list=command_list,
                           app_list=app_list,
                           sdk_version=sdk_version,
                           search_form=search_form,
                           search_term=search_term,
                           num_results=num_results,
                           edit_form=edit_form,
                           support_files=support_files,
                           settings_form=settings_form)