2025-02-08 10:31:56 +00:00
|
|
|
from flask import Blueprint, redirect, session, request, jsonify
|
2025-02-11 18:45:53 +00:00
|
|
|
from definitions.models import Asset, db
|
2025-02-04 19:11:03 +00:00
|
|
|
import json
|
2025-02-08 10:31:56 +00:00
|
|
|
from config import item_attributes # Import the configuration
|
2025-02-01 18:19:02 +00:00
|
|
|
|
|
|
|
confirm_save_bp = Blueprint('confirm_save', __name__)
|
|
|
|
|
|
|
|
@confirm_save_bp.route('/confirm_save', methods=['POST'])
|
|
|
|
def confirm_save():
|
2025-02-08 10:31:56 +00:00
|
|
|
if 'assets' not in request.form:
|
|
|
|
return "No assets data found in the request.", 400
|
|
|
|
|
|
|
|
try:
|
|
|
|
edited_assets = json.loads(request.form['assets'])
|
|
|
|
except json.JSONDecodeError:
|
|
|
|
return "Invalid JSON data in the request.", 400
|
|
|
|
|
2025-02-04 19:11:03 +00:00
|
|
|
session['assets'] = edited_assets
|
|
|
|
|
|
|
|
for asset_data in edited_assets:
|
2025-02-08 10:31:56 +00:00
|
|
|
# Dynamically create the Asset object using item_attributes
|
|
|
|
asset = Asset(**{
|
|
|
|
attrib: asset_data[attrib]
|
|
|
|
for attrib in item_attributes
|
|
|
|
})
|
2025-02-01 18:19:02 +00:00
|
|
|
db.session.add(asset)
|
2025-02-08 10:31:56 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
db.session.commit()
|
|
|
|
except Exception as e:
|
|
|
|
db.session.rollback()
|
|
|
|
return f"Error saving data to the database: {str(e)}", 500
|
|
|
|
|
2025-02-01 18:19:02 +00:00
|
|
|
session.pop('assets', None) # Clear session data
|
|
|
|
return redirect('/viewall')
|