Fixed bug in redirect

This commit is contained in:
Candifloss 2025-03-06 13:24:46 +05:30
parent 34215b711a
commit 6d667b2001
4 changed files with 32 additions and 9 deletions

View File

@ -8,17 +8,25 @@ 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'
mode = session.get('csv_mode', 'import') # Default to 'import' if mode is not set
redirect_route = 'uploadcsv.import_from_csv' if mode == 'import' else 'uploadcsv.edit_using_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'))
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'))
return redirect(url_for(redirect_route))
# Validate each asset # Validate each asset
errors = [] errors = []
@ -31,14 +39,16 @@ 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'))
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'))
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()
@ -57,10 +67,12 @@ 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'))
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)
session.pop('existing_entries', None) session.pop('existing_entries', None)
session.pop('invalid_entries', None) session.pop('invalid_entries', None)
session.pop('csv_mode', None)
return redirect('/viewall') return redirect('/viewall')

View File

@ -56,6 +56,7 @@ def _process_csv_file(file, mode):
@upload_bp.route('/import_from_csv', methods=['GET', 'POST']) @upload_bp.route('/import_from_csv', methods=['GET', 'POST'])
def import_from_csv(): def import_from_csv():
session['csv_mode'] = 'import' # Store mode in session
if request.method == 'POST': if request.method == 'POST':
# Check if a file was uploaded # Check if a file was uploaded
if 'file' not in request.files: if 'file' not in request.files:
@ -83,16 +84,18 @@ def import_from_csv():
# Redirect to preview page # Redirect to preview page
return render_template( return render_template(
'csv_preview.html', 'csv_preview.html',
mode='import',
new_entries=result['new_entries'], new_entries=result['new_entries'],
invalid_entries=result['invalid_entries'], invalid_entries=result['invalid_entries'],
item_attributes=item_attributes item_attributes=item_attributes
) )
# Render the upload page for GET requests # Render the upload page for GET requests
return render_template('upload.html', mode="addnew") return render_template('upload.html', mode="import")
@upload_bp.route('/edit_using_csv', methods=['GET', 'POST']) @upload_bp.route('/edit_using_csv', methods=['GET', 'POST'])
def edit_using_csv(): def edit_using_csv():
session['csv_mode'] = 'edit' # Store mode in session
if request.method == 'POST': if request.method == 'POST':
# Check if a file was uploaded # Check if a file was uploaded
if 'file' not in request.files: if 'file' not in request.files:
@ -120,10 +123,11 @@ def edit_using_csv():
# Redirect to preview page # Redirect to preview page
return render_template( return render_template(
'csv_preview.html', 'csv_preview.html',
mode='edit',
existing_entries=result['existing_entries'], existing_entries=result['existing_entries'],
invalid_entries=result['invalid_entries'], invalid_entries=result['invalid_entries'],
item_attributes=item_attributes item_attributes=item_attributes
) )
# Render the upload page for GET requests # Render the upload page for GET requests
return render_template('upload.html', mode="update") return render_template('upload.html', mode="edit")

View File

@ -31,6 +31,13 @@
] %} ] %}
{% if entries %} {% if entries %}
<h2>{{ table_name }}</h2> <h2>{{ table_name }}</h2>
{% if table_name == 'Invalid Entries' %}
{% if mode == 'import' %}
<p>These entries already exist:</p>
{% elif mode == 'edit' %}
<p>These entries do not exist:</p>
{% endif %}
{% endif %}
<table border="1" class="{{ tableclass }}"> <table border="1" class="{{ tableclass }}">
<thead> <thead>
<tr> <tr>

View File

@ -24,9 +24,9 @@
<!-- Upload form --> <!-- Upload form -->
<form method="POST" enctype="multipart/form-data" <form method="POST" enctype="multipart/form-data"
{% if mode == "addnew" %} {% if mode == "import" %}
action="/import_from_csv" action="/import_from_csv"
{% elif mode == "addnew" %} {% elif mode == "edit" %}
action="/edit_using_csv" action="/edit_using_csv"
{% endif %} {% endif %}
> >