Improving upload.py

This commit is contained in:
Candifloss 2025-02-24 16:08:11 +05:30
parent d4e7c0833b
commit a3fe6f922b

View File

@ -8,6 +8,12 @@ upload_bp = Blueprint('uploadcsv', __name__)
@upload_bp.route('/uploadcsv', methods=['GET', 'POST'])
def upload_file():
# Check if primary attribute is defined
primary_attrib = next((attrib for attrib in item_attributes if attrib.primary), None)
if not primary_attrib:
flash("Primary attribute not defined in configuration.", "error")
return redirect(request.url)
if request.method == 'POST':
# Check if a file was uploaded
if 'file' not in request.files:
@ -22,8 +28,11 @@ def upload_file():
return redirect(request.url)
try:
# Extract and validate CSV data
# Extract CSV data
csvdata = get_csv_data(file)
except Exception as e:
flash(f"Error reading CSV file: {str(e)}", "error")
return redirect(request.url)
# Validate each row of data
errors = []
@ -39,12 +48,6 @@ def upload_file():
errors=errors
)
# Identify the primary attribute
primary_attrib = next((attrib for attrib in item_attributes if attrib.primary), None)
if not primary_attrib:
flash("Primary attribute not defined in configuration.", "error")
return redirect(request.url)
# Separate new and existing assets
new_assets = []
existing_assets = []
@ -56,20 +59,17 @@ def upload_file():
else:
new_assets.append(row)
session['assets'] = new_assets # Store new assets in session
# Store new assets in session for further processing
session['new_assets'] = new_assets
session['existing_assets'] = existing_assets
# Redirect to preview page with the CSV data
return render_template(
'csv_preview.html',
new_assets=new_assets,
existing=existing_assets,
existing_assets=existing_assets,
item_attributes=item_attributes
)
except Exception as e:
# Handle errors during file processing
flash(f"Error processing CSV file: {str(e)}", "error")
return redirect(request.url)
# Render the upload page for GET requests
return render_template('upload.html')