assettrack/routes/delete.py

36 lines
1.2 KiB
Python

from flask import Blueprint, request, render_template, redirect, flash
from definitions.models import Asset, db
from config import item_attributes
delete_bp = Blueprint('deleteasset', __name__)
@delete_bp.route('/delete/<path:primary_value>/', methods=['GET', 'POST'])
def delete(primary_value):
# Identify the primary attribute
primary_attrib = next(
(attrib for attrib in item_attributes if attrib.primary),
None
)
if not primary_attrib:
flash("Primary attribute not defined in configuration.", "error")
return redirect('/viewall')
# Fetch the item using the primary attribute
item = Asset.query.filter_by(**{primary_attrib.attrib_name: primary_value}).first()
if not item:
flash("Item not found.", "error")
return redirect('/viewall')
if request.method == 'POST':
db.session.delete(item)
db.session.commit()
flash("Item successfully deleted.", "success")
return redirect('/viewall')
# Render the delete confirmation page with a preview of the item
return render_template(
'delete.html',
item=item,
item_attributes=item_attributes
)