2025-01-30 07:37:21 +00:00
|
|
|
from flask import Blueprint, request, render_template, redirect
|
2025-02-11 18:45:53 +00:00
|
|
|
from definitions.models import Asset, db
|
2025-02-06 10:43:09 +00:00
|
|
|
from config import item_attributes
|
2025-02-07 18:34:43 +00:00
|
|
|
from sqlalchemy import exc # Import exc for database exceptions
|
2025-02-22 18:58:10 +00:00
|
|
|
from definitions.attribute import selectAttribute, intAttribute # Import attribute classes
|
2025-01-30 07:37:21 +00:00
|
|
|
|
|
|
|
update_bp = Blueprint('editasset', __name__)
|
|
|
|
|
2025-02-07 19:58:37 +00:00
|
|
|
@update_bp.route('/update/<string:primary_value>/', methods=['GET', 'POST'])
|
|
|
|
def update(primary_value):
|
|
|
|
# Identify the primary attribute
|
|
|
|
primary_attrib = next(
|
2025-02-22 18:58:10 +00:00
|
|
|
(attrib for attrib in item_attributes if attrib.primary),
|
2025-02-07 19:58:37 +00:00
|
|
|
None
|
|
|
|
)
|
|
|
|
|
|
|
|
if not primary_attrib:
|
|
|
|
return "Primary attribute not defined in configuration."
|
|
|
|
|
|
|
|
# Fetch the item using the primary attribute
|
2025-02-22 18:58:10 +00:00
|
|
|
item = Asset.query.filter_by(**{primary_attrib.attrib_name: primary_value}).first()
|
2025-02-07 18:34:43 +00:00
|
|
|
if not item:
|
2025-02-22 18:58:10 +00:00
|
|
|
return f"{primary_attrib.display_name} '{primary_value}' not found."
|
2025-02-07 18:34:43 +00:00
|
|
|
|
|
|
|
if request.method == 'GET':
|
2025-02-22 18:58:10 +00:00
|
|
|
return render_template(
|
|
|
|
'update.html',
|
|
|
|
item=item,
|
|
|
|
item_attributes=item_attributes,
|
|
|
|
isinstance=isinstance, # Pass isinstance to the template
|
|
|
|
hasattr=hasattr, # Pass hasattr to the template
|
|
|
|
selectAttribute=selectAttribute, # Pass selectAttribute to the template
|
|
|
|
intAttribute=intAttribute # Pass intAttribute to the template
|
|
|
|
)
|
2025-02-07 18:34:43 +00:00
|
|
|
|
2025-01-30 07:37:21 +00:00
|
|
|
if request.method == 'POST':
|
2025-02-07 18:34:43 +00:00
|
|
|
# Dynamically get form data using item_attributes
|
2025-02-22 18:58:10 +00:00
|
|
|
form_data = {attrib.attrib_name: request.form[attrib.attrib_name] for attrib in item_attributes}
|
2025-02-07 18:34:43 +00:00
|
|
|
|
|
|
|
# Validate status (if it's an option field)
|
2025-02-22 18:58:10 +00:00
|
|
|
status_attrib = next((attrib for attrib in item_attributes if attrib.attrib_name == 'status'), None)
|
|
|
|
if status_attrib and isinstance(status_attrib, selectAttribute):
|
|
|
|
if form_data['status'] not in status_attrib.options:
|
2025-02-07 18:34:43 +00:00
|
|
|
return render_template('update.html', item=item, exc='status', item_attributes=item_attributes)
|
|
|
|
|
|
|
|
# Convert staffnum to int (if it's a number field)
|
2025-02-22 18:58:10 +00:00
|
|
|
staffnum_attrib = next((attrib for attrib in item_attributes if attrib.attrib_name == 'staffnum'), None)
|
|
|
|
if staffnum_attrib and isinstance(staffnum_attrib, intAttribute):
|
2025-01-30 07:37:21 +00:00
|
|
|
try:
|
2025-02-07 18:34:43 +00:00
|
|
|
form_data['staffnum'] = int(form_data['staffnum'])
|
2025-01-30 07:37:21 +00:00
|
|
|
except ValueError:
|
2025-02-06 10:43:09 +00:00
|
|
|
return render_template('update.html', item=item, exc='staffnum', item_attributes=item_attributes)
|
2025-01-30 07:37:21 +00:00
|
|
|
|
2025-02-07 18:34:43 +00:00
|
|
|
# Update the item with the new data
|
2025-02-22 18:58:10 +00:00
|
|
|
for attrib in item_attributes:
|
|
|
|
setattr(item, attrib.attrib_name, form_data[attrib.attrib_name])
|
2025-01-30 07:37:21 +00:00
|
|
|
|
2025-02-07 18:34:43 +00:00
|
|
|
try:
|
|
|
|
db.session.commit()
|
|
|
|
except exc.IntegrityError:
|
|
|
|
return render_template('update.html', item=item, exc='integrity', item_attributes=item_attributes)
|
|
|
|
except (exc.StatementError, exc.InvalidRequestError):
|
|
|
|
return render_template('update.html', item=item, exc='status', item_attributes=item_attributes)
|
2025-01-30 07:37:21 +00:00
|
|
|
|
2025-02-07 18:34:43 +00:00
|
|
|
return redirect('/viewall')
|