Fixed csv submission and redirection
This commit is contained in:
parent
6d667b2001
commit
ce8373f613
@ -8,25 +8,23 @@ confirm_save_bp = Blueprint('confirm_save', __name__)
|
|||||||
|
|
||||||
@confirm_save_bp.route('/confirm_save', methods=['POST'])
|
@confirm_save_bp.route('/confirm_save', methods=['POST'])
|
||||||
def confirm_save():
|
def confirm_save():
|
||||||
# Determine if the operation was 'import' or 'edit'
|
# Retrieve mode from session
|
||||||
mode = session.get('csv_mode', 'import') # Default to 'import' if mode is not set
|
mode = session.get('csv_mode')
|
||||||
redirect_route = 'uploadcsv.import_from_csv' if mode == 'import' else 'uploadcsv.edit_using_csv'
|
if mode not in ['import', 'edit']:
|
||||||
|
flash("Invalid operation mode. Please try again.", "error")
|
||||||
|
return redirect(url_for('uploadcsv.import_from_csv'))
|
||||||
|
|
||||||
# Check if assets data is present in the request
|
# Check if assets data is present in the request
|
||||||
if 'assets' not in request.form:
|
if 'assets' not in request.form:
|
||||||
flash("No assets data found in the request.", "error")
|
flash("No assets data found in the request.", "error")
|
||||||
#return redirect(url_for('uploadcsv.import_from_csv'))
|
return redirect(url_for('uploadcsv.import_from_csv' if mode == 'import' else 'uploadcsv.edit_using_csv'))
|
||||||
return redirect(url_for(redirect_route))
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Parse the JSON data from the form
|
# Parse the JSON data from the form
|
||||||
edited_assets = json.loads(request.form['assets'])
|
edited_assets = json.loads(request.form['assets'])
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
flash("Invalid JSON data in the request.", "error")
|
flash("Invalid JSON data in the request.", "error")
|
||||||
#return redirect(url_for('uploadcsv.import_from_csv'))
|
return redirect(url_for('uploadcsv.import_from_csv' if mode == 'import' else 'uploadcsv.edit_using_csv'))
|
||||||
return redirect(url_for(redirect_route))
|
|
||||||
|
|
||||||
# Validate each asset
|
# Validate each asset
|
||||||
errors = []
|
errors = []
|
||||||
@ -39,16 +37,14 @@ def confirm_save():
|
|||||||
# If there are validation errors, flash them and redirect back to the preview page
|
# If there are validation errors, flash them and redirect back to the preview page
|
||||||
for error in errors:
|
for error in errors:
|
||||||
flash(error, "error")
|
flash(error, "error")
|
||||||
#return redirect(url_for('uploadcsv.import_from_csv'))
|
return redirect(url_for('uploadcsv.import_from_csv' if mode == 'import' else 'uploadcsv.edit_using_csv'))
|
||||||
return redirect(url_for(redirect_route))
|
|
||||||
|
|
||||||
# Save validated assets to the database
|
# Save validated assets to the database
|
||||||
for asset_data in edited_assets:
|
for asset_data in edited_assets:
|
||||||
primary_attrib = next((attrib for attrib in item_attributes if attrib.primary), None)
|
primary_attrib = next((attrib for attrib in item_attributes if attrib.primary), None)
|
||||||
if not primary_attrib:
|
if not primary_attrib:
|
||||||
flash("Primary attribute not defined in configuration.", "error")
|
flash("Primary attribute not defined in configuration.", "error")
|
||||||
#return redirect(url_for('uploadcsv.import_from_csv'))
|
return redirect(url_for('uploadcsv.import_from_csv' if mode == 'import' else 'uploadcsv.edit_using_csv'))
|
||||||
return redirect(url_for(redirect_route))
|
|
||||||
|
|
||||||
primary_value = asset_data[primary_attrib.attrib_name]
|
primary_value = asset_data[primary_attrib.attrib_name]
|
||||||
asset = Asset.query.filter_by(**{primary_attrib.attrib_name: primary_value}).first()
|
asset = Asset.query.filter_by(**{primary_attrib.attrib_name: primary_value}).first()
|
||||||
@ -67,8 +63,7 @@ def confirm_save():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
db.session.rollback()
|
db.session.rollback()
|
||||||
flash(f"Error saving data to the database: {str(e)}", "error")
|
flash(f"Error saving data to the database: {str(e)}", "error")
|
||||||
#return redirect(url_for('uploadcsv.import_from_csv'))
|
return redirect(url_for('uploadcsv.import_from_csv' if mode == 'import' else 'uploadcsv.edit_using_csv'))
|
||||||
return redirect(url_for(redirect_route))
|
|
||||||
|
|
||||||
# Clear session data after successful insertion
|
# Clear session data after successful insertion
|
||||||
session.pop('new_entries', None)
|
session.pop('new_entries', None)
|
||||||
|
Loading…
Reference in New Issue
Block a user