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-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(
|
|
|
|
(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()
|
2025-02-07 18:34:43 +00:00
|
|
|
if not item:
|
2025-02-07 19:58:37 +00:00
|
|
|
return f"{item_attributes[primary_attrib].display_name} '{primary_value}' not found." # Move this to a template
|
2025-02-07 18:34:43 +00:00
|
|
|
|
|
|
|
if request.method == 'GET':
|
|
|
|
return render_template('update.html', item=item, item_attributes=item_attributes)
|
|
|
|
|
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
|
|
|
|
form_data = {attrib: request.form[attrib] for attrib in item_attributes}
|
|
|
|
|
|
|
|
# Validate status (if it's an option field)
|
|
|
|
if 'status' in item_attributes and item_attributes['status'].html_input_type == "select":
|
|
|
|
if form_data['status'] not in item_attributes['status'].options:
|
|
|
|
return render_template('update.html', item=item, exc='status', item_attributes=item_attributes)
|
|
|
|
|
|
|
|
# Convert staffnum to int (if it's a number field)
|
|
|
|
if 'staffnum' in item_attributes and item_attributes['staffnum'].html_input_type == "number":
|
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
|
|
|
|
for attrib, value in form_data.items():
|
|
|
|
setattr(item, attrib, value)
|
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')
|