Update delete.py to use config
This commit is contained in:
		
							parent
							
								
									13c1344013
								
							
						
					
					
						commit
						ca5a572363
					
				@ -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 models import Asset, db
 | 
				
			||||||
 | 
					from config import item_attributes
 | 
				
			||||||
 | 
					
 | 
				
			||||||
delete_bp = Blueprint('deleteasset', __name__)
 | 
					delete_bp = Blueprint('deleteasset', __name__)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@delete_bp.route('/delete/<string:assettag>/', methods=['GET', 'POST'])
 | 
					@delete_bp.route('/delete/<string:primary_value>/', methods=['GET', 'POST'])
 | 
				
			||||||
def delete(assettag):
 | 
					def delete(primary_value):
 | 
				
			||||||
    item = Asset.query.filter_by(assettag=assettag).first()
 | 
					    # Identify the primary attribute
 | 
				
			||||||
 | 
					    primary_attrib = next(
 | 
				
			||||||
 | 
					        (attrib for attrib, config in item_attributes.items() if config.primary),
 | 
				
			||||||
 | 
					        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: primary_value}).first()
 | 
				
			||||||
 | 
					    if not item:
 | 
				
			||||||
 | 
					        abort(404)  # Item not found
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if request.method == 'POST':
 | 
					    if request.method == 'POST':
 | 
				
			||||||
        if item:
 | 
					 | 
				
			||||||
        db.session.delete(item)
 | 
					        db.session.delete(item)
 | 
				
			||||||
        db.session.commit()
 | 
					        db.session.commit()
 | 
				
			||||||
        return redirect('/viewall')
 | 
					        return redirect('/viewall')
 | 
				
			||||||
        abort(404)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return render_template('delete.html')
 | 
					    return render_template('delete.html', item=item)
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user