flask_crud_app/routes/delete.py

28 lines
917 B
Python
Raw Permalink Normal View History

2025-02-07 20:05:04 +00:00
from flask import Blueprint, request, render_template, redirect, abort
2025-02-11 18:45:53 +00:00
from definitions.models import Asset, db
2025-02-07 20:05:04 +00:00
from config import item_attributes
2025-01-30 07:37:21 +00:00
delete_bp = Blueprint('deleteasset', __name__)
2025-02-07 20:05:04 +00:00
@delete_bp.route('/delete/<string: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),
2025-02-07 20:05:04 +00:00
None
)
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.attrib_name: primary_value}).first()
2025-02-07 20:05:04 +00:00
if not item:
abort(404) # Item not found
2025-01-30 07:37:21 +00:00
if request.method == 'POST':
2025-02-07 20:05:04 +00:00
db.session.delete(item)
db.session.commit()
return redirect('/viewall')
2025-01-30 07:37:21 +00:00
2025-02-07 20:05:04 +00:00
return render_template('delete.html', item=item)