From ca5a5723636a5dc4f76e63104111898222ca57d4 Mon Sep 17 00:00:00 2001 From: candifloss Date: Sat, 8 Feb 2025 01:35:04 +0530 Subject: [PATCH] Update delete.py to use config --- routes/delete.py | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/routes/delete.py b/routes/delete.py index 427d940..783aee1 100644 --- a/routes/delete.py +++ b/routes/delete.py @@ -1,16 +1,28 @@ -from flask import Blueprint, request, render_template, redirect +from flask import Blueprint, request, render_template, redirect, abort from models import Asset, db +from config import item_attributes delete_bp = Blueprint('deleteasset', __name__) -@delete_bp.route('/delete//', methods=['GET', 'POST']) -def delete(assettag): - item = Asset.query.filter_by(assettag=assettag).first() - if request.method == 'POST': - if item: - db.session.delete(item) - db.session.commit() - return redirect('/viewall') - abort(404) +@delete_bp.route('/delete//', methods=['GET', 'POST']) +def delete(primary_value): + # Identify the primary attribute + primary_attrib = next( + (attrib for attrib, config in item_attributes.items() if config.primary), + None + ) - return render_template('delete.html') \ No newline at end of file + if not primary_attrib: + return "Primary attribute not defined in configuration." + + # Fetch the item using the primary attribute + item = Asset.query.filter_by(**{primary_attrib: primary_value}).first() + if not item: + abort(404) # Item not found + + if request.method == 'POST': + db.session.delete(item) + db.session.commit() + return redirect('/viewall') + + return render_template('delete.html', item=item) \ No newline at end of file