Exemple #1
0
def edit_tool(id):
    """
    Edit a tool.
    :param id:
    :return:
    """
    check_admin()
    tool = Tool.query.get_or_404(id)
    form = ToolForm(obj=tool)
    if form.validate_on_submit():
        tool.name = form.name.data
        tool.description = form.description.data
        try:
            db.session.add(tool)
            db.session.commit()
            flash('Successfully edited the tool: "%s".' % str(tool.name))
        except:
            db.session.rollback()
            flash('Failed to edit the tool: "%s".' % str(tool.name))
        return redirect(url_for('admin.tools'))
    form.name.data = tool.name
    form.description.data = tool.description
    return render_template('admin/tools/edit_tool.html',
                           title='Edit Tool',
                           tool=tool,
                           form=form)  # type: ToolForm
Exemple #2
0
def new_tool():
    form = ToolForm()
    if form.validate_on_submit():
        tool = Tool()
        form.populate_obj(tool)
        db.session.add(tool)
        db.session.commit()
        return redirect(url_for('list_tools'))
    return render_template('tool.html', form=form, is_new=True)
Exemple #3
0
def edit_tool(id):
    tool = Tool.query.get_or_404(id)
    form = ToolForm(obj=tool)
    if form.validate_on_submit():
        form.populate_obj(tool)
        db.session.merge(tool)
        db.session.commit()
        db.session.refresh(tool)
        return redirect(url_for('list_tools'))
    return render_template('tool.html', form=form,
                           tool=tool)
Exemple #4
0
def edit(id):
    qry = db_session.query(Bin).filter(Bin.id == id)
    bin = qry.first()

    if bin:
        form = ToolForm(formdata=request.form, obj=bin)
        if request.method == 'POST' and form.validate():
            # save edit
            save_changes(bin, form)
            flash('Tool listing updated successfully.')
            return redirect('/')
        return render_template('edit_tool.html', form=form)
    else:
        return 'Error loading #{id}'.format(id=id)
Exemple #5
0
def new_tool():
    """
    Add a new special tool
    """
    form = ToolForm(request.form)

    if request.method == 'POST' and form.validate():
        # save the tool
        bin = Bin()
        save_changes(bin, form, new=True)
        flash('Tool listing created successfully.')
        return redirect('/')

    return render_template('new_tool.html', form=form)
Exemple #6
0
def add_biotool(request):
	if request.POST:
	   form = ToolForm(request.POST, request.FILES)
	   if form.is_valid():
		form.save()

		return HttpResponseRedirect('/biotools/all')
	else:
	   form = ToolForm()

	args = {}
	args.update(csrf(request))
	
	args['form'] = form

	return render_to_response('add_biotool.html', args)
Exemple #7
0
def add_tool():
    """
    Add a tool to the database.
    :return:
    """
    check_admin()
    form = ToolForm()
    if form.validate_on_submit():
        tool = Tool(name=form.name.data, description=form.description.data)
        try:
            db.session.add(tool)
            db.session.commit()
            flash('Successfully added a new tool: "%s".' % str(tool.name))
        except:
            db.session.rollback()
            flash('Failed to add the tool: "%s".' % str(tool.name))
        return redirect(url_for('admin.tools'))
    return render_template('admin/tools/add_tool.html',
                           title='Add Tool',
                           action='Add',
                           form=form)  # type: ToolForm
Exemple #8
0
def delete(id):
    """
    Delete the item in the database
    """
    qry = db_session.query(Bin).filter(Bin.id == id)
    bin = qry.first()
    delete = Delete(qry)
    delete.border = True

    if bin:
        form = ToolForm(formdata=request.form, obj=bin)
        if request.method == 'POST' and form.validate():
            # delete the item from the databse
            db_session.delete(bin)
            db_session.commit()

            flash('Tool listing deleted successfully.')
            return redirect('/')
        return render_template('delete_tool.html', table=delete)
    else:
        return 'Error deleting #{id}'.format(id=id)
Exemple #9
0
def view_tool():
    session['url'] = request.url[len(request.url_root):]
    if request.method == 'GET':
        if 'name' in request.args:
            name = request.args.get('name')
            result = db_session.query(Tool).filter_by(name=name).first()
            return render_template('tool.html',
                                   result=result,
                                   title='cutrenet',
                                   subtitle=name)
        elif not current_user.has_role('admin'):
            flash(u'No tienes permisos para editar esta herramienta', 'error')
            return redirect('/tools', code=302)
        elif 'add' in request.args:
            form = ToolForm()
            return render_template('tool.html',
                                   form=form,
                                   title='cutrenet',
                                   subtitle="new tool")
        elif 'edit' in request.args:
            ename = request.args.get('edit')
            form = ToolForm(self_edit=ename)
            result = db_session.query(Tool).filter_by(name=ename).first()
            form.description.data = result.description  # Prepopulate textarea with past information, can´t do it at render time
            if result.maintainer is not None:
                form.maintainer.data = result.maintainer.dni
            return render_template('tool.html',
                                   form=form,
                                   result=result,
                                   title='cutrenet',
                                   subtitle=ename)
        elif 'delete_img' in request.args:
            del_img = request.args.get('delete_img')
            tool = db_session.query(Tool).filter_by(name=del_img).first()
            if tool.image:
                os.remove(tool.image)  # Delete old image
                tool.image = None
                db_session.commit()
                flash(u'Imagen eliminada', 'success')
            else:
                flash(u'No hay imagen que eliminar', 'alert')
            return render_template('tool.html',
                                   result=tool,
                                   title='cutrenet',
                                   subtitle=tool.name)
        elif 'delete' in request.args:
            delete = request.args.get('delete')
            tool = db_session.query(Tool).filter_by(name=delete).first()
            db_session.delete(tool)
            db_session.commit()
            flash(u'Herramienta eliminada', 'success')
            return redirect('/tools', code=302)
        else:
            flash(u'Tienes que seleccionar una herramienta', 'error')
            return redirect('/tools', code=302)

    if request.method == 'POST' and current_user.has_role('admin'):
        if 'edit' in request.args:
            ename = request.args.get('edit')
            tool = db_session.query(Tool).filter_by(name=ename).first()
            form = ToolForm(self_edit=ename)
            if form.validate_on_submit():
                tool.name = request.form['name']
                tool.description = request.form['description']
                tool.location = request.form['location']
                tool.manual = request.form['manual']
                tool.documentation = request.form['documentation']

                maintainer = db_session.query(User).filter_by(
                    dni=request.form['maintainer']).first()
                if maintainer is not None:
                    maintainer.tool_maintainer.append(tool)

                if form.image.data:
                    if tool.image is not None:
                        os.remove(tool.image)  # Delete old image
                    f = form.image.data
                    filename = secure_filename(f.filename)
                    directory = app.config['UPLOAD_FOLDER'] + '/tools'
                    if not os.path.exists(directory):
                        os.makedirs(directory)
                    file_path = os.path.join(directory, filename)
                    f.save(file_path)
                    tool.image = file_path  # Save the file path of the Tool image in the database

                db_session.commit()
                flash(u'Herramienta editada', 'success')
            return render_template('tool.html',
                                   form=form,
                                   result=tool,
                                   title='cutrenet',
                                   subtitle=tool.name)
        elif 'add' in request.args:
            tool = Tool()
            form = ToolForm()
            if form.validate_on_submit():
                tool.name = request.form['name']
                tool.description = request.form['description']
                tool.location = request.form['location']
                tool.manual = request.form['manual']
                tool.documentation = request.form['documentation']

                maintainer = db_session.query(User).filter_by(
                    dni=request.form['maintainer']).first()
                if maintainer is not None:
                    maintainer.tool_maintainer.append(tool)

                if form.image.data:
                    if tool.image is not None:
                        os.remove(tool.image)  # Delete old image
                    f = form.image.data
                    filename = secure_filename(f.filename)
                    directory = app.config['UPLOAD_FOLDER'] + '/tools'
                    if not os.path.exists(directory):
                        os.makedirs(directory)
                    file_path = os.path.join(directory, filename)
                    f.save(file_path)
                    tool.image = file_path  # Save the file path of the Tool image in the database

                db_session.add(tool)
                db_session.commit()
                flash(u'Herramienta añadida', 'success')
                return redirect('tools', code=302)
            return render_template('tool.html',
                                   form=form,
                                   title='cutrenet',
                                   subtitle="new tool")