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 08:33:13 +00:00
|
|
|
from config import item_attributes
|
2025-02-11 20:45:25 +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
|
|
|
|
|
|
|
addasset_bp = Blueprint('addasset', __name__)
|
|
|
|
|
|
|
|
@addasset_bp.route('/create/', methods=['GET', 'POST'])
|
|
|
|
def create():
|
|
|
|
if request.method == 'GET':
|
2025-02-07 06:31:55 +00:00
|
|
|
# Render the "add item" form
|
2025-02-22 18:58:10 +00:00
|
|
|
return render_template(
|
|
|
|
'create.html',
|
|
|
|
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-01-30 07:37:21 +00:00
|
|
|
|
2025-02-07 06:31:55 +00:00
|
|
|
# Process submitted form
|
2025-01-30 07:37:21 +00:00
|
|
|
if request.method == 'POST':
|
2025-02-07 06:31:55 +00:00
|
|
|
# Get data from form
|
2025-02-22 18:58:10 +00:00
|
|
|
form_data = {attrib.attrib_name: request.form[attrib.attrib_name] for attrib in item_attributes}
|
|
|
|
|
|
|
|
# Validate status (if it's an option field)
|
|
|
|
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:
|
|
|
|
return render_template('create.html', item_attributes=item_attributes, exc='status')
|
|
|
|
|
|
|
|
# Convert staffnum to int (if it's a number field)
|
|
|
|
staffnum_attrib = next((attrib for attrib in item_attributes if attrib.attrib_name == 'staffnum'), None)
|
|
|
|
if staffnum_attrib and isinstance(staffnum_attrib, intAttribute):
|
|
|
|
try:
|
|
|
|
form_data['staffnum'] = int(form_data['staffnum'])
|
|
|
|
except ValueError:
|
|
|
|
return render_template('create.html', item_attributes=item_attributes, exc='staffnum')
|
2025-01-30 07:37:21 +00:00
|
|
|
|
2025-02-22 18:58:10 +00:00
|
|
|
# Create the Asset object
|
2025-02-07 06:31:55 +00:00
|
|
|
item = Asset(**form_data)
|
2025-01-30 07:37:21 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
db.session.add(item)
|
|
|
|
db.session.commit()
|
|
|
|
except exc.IntegrityError:
|
2025-02-07 18:20:26 +00:00
|
|
|
return render_template('create.html', item_attributes=item_attributes, exc='integrity')
|
2025-01-30 07:37:21 +00:00
|
|
|
except exc.StatementError:
|
2025-02-07 18:20:26 +00:00
|
|
|
return render_template('create.html', item_attributes=item_attributes, exc='status')
|
2025-01-30 07:37:21 +00:00
|
|
|
|
|
|
|
return redirect('/viewall')
|