Example #1
0
def edit(asset_id):
    asset = Asset.query.filter_by(id=asset_id).first_or_404()
    form = EditAssetForm(asset_id=asset.id)

    if form.validate_on_submit():
        asset.name = form.name.data
        asset.type = form.type.data
        asset.description = form.description.data
        asset.serial_no = form.serial_no.data
        asset.code = form.code.data
        asset.purchased = form.purchased.data
        db.session.add(asset)
        db.session.commit()
        flash("Asset saved", "success")
        return redirect(url_for('assets.index'))

    if request.method == 'GET':
        form.name.data = asset.name
        form.type.data = asset.type
        form.description.data = asset.description
        form.serial_no.data = asset.serial_no
        form.code.data = asset.code
        form.purchased.data = asset.purchased

    return render_template('assets/edit.html',
                           form=form,
                           heading='Edit asset',
                           asset_id=asset.id)
Example #2
0
def edit_asset(asset_tag):
    form = EditAssetForm()
    edit_asset = Motor.query.filter_by(asset_tag = asset_tag).first_or_404()
    if request.method == 'POST':
        if form.validate_on_submit():# Confirms if all requirements for the form are met (No empty fields in this case).
            try:
                asset_tag_input = form.asset_tag.data #Form input for Asset Tag.
                serial = form.serial.data
                manufacturer = form.manufacturer.data
                model_number = form.model_number.data
                current = form.current.data
                frequency = form.frequency.data
                voltage = form.voltage.data #Form input for voltage of corresponding asset.
                secondary_voltage = form.secondary_voltage.data #Form input for secondary voltage of corresponding asset.
                power_factor = form.power_factor.data
                efficiency = form.efficiency.data
                horsepower = form.horsepower.data #Form input for Horsepower of corresponding asset.
                rpm = form.rpm.data #Form input for RPM of corresponding asset.
                design = form.design.data
                frame = form.frame.data #Form input for Frame of corresponding asset.
                enclosure = form.enclosure.data #Form input for enclosure of corresponding asset.
                attachments = form.attachments.data #Form input for any attachments of corresponding asset.
                location = form.location.data
                status = form.status.data
                edit_data = {'asset_tag': asset_tag_input, 'serial' : serial, 'manufacturer' : manufacturer, 'model_number' : model_number, 'current' : current, 'frequency' : frequency,
                'voltage' : voltage, 'secondary_voltage' : secondary_voltage, 'power_factor' : power_factor, 'efficiency' : efficiency, 'horsepower' : horsepower,
                'rpm' : rpm, 'design' : design, 'frame' : frame, 'enclosure' : enclosure, 'attachments' : attachments, 'location' : location, 'status' : status}
                edited_data = {key: value for (key, value) in edit_data.items() if value}
                db.session.query(Motor).filter_by(asset_tag = edit_asset.asset_tag).update(edited_data, synchronize_session='fetch')
                db.session.commit()
                flash("Asset %s Updated successfully!" % (edit_asset.asset_tag))
            except Exception as e:
                flash(e)
                db.session.rollback()
        else:
            flash("Form did not validate.")
    return render_template('edit_asset.html', asset=edit_asset, form=form)