Improving upload.py
This commit is contained in:
parent
d4e7c0833b
commit
a3fe6f922b
@ -8,6 +8,12 @@ upload_bp = Blueprint('uploadcsv', __name__)
|
|||||||
|
|
||||||
@upload_bp.route('/uploadcsv', methods=['GET', 'POST'])
|
@upload_bp.route('/uploadcsv', methods=['GET', 'POST'])
|
||||||
def upload_file():
|
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':
|
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:
|
||||||
@ -22,54 +28,48 @@ def upload_file():
|
|||||||
return redirect(request.url)
|
return redirect(request.url)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Extract and validate CSV data
|
# Extract CSV data
|
||||||
csvdata = get_csv_data(file)
|
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
|
# Validate each row of data
|
||||||
errors = []
|
errors = []
|
||||||
for i, row in enumerate(csvdata, start=1):
|
for i, row in enumerate(csvdata, start=1):
|
||||||
error = validate_values(row)
|
error = validate_values(row)
|
||||||
if error:
|
if error:
|
||||||
errors.append(f"Row {i}: {error}")
|
errors.append(f"Row {i}: {error}")
|
||||||
|
|
||||||
if errors:
|
if errors:
|
||||||
# Pass errors as a list to the template
|
# Pass errors as a list to the template
|
||||||
return render_template(
|
|
||||||
'upload.html',
|
|
||||||
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 = []
|
|
||||||
for row in csvdata:
|
|
||||||
primary_value = row[primary_attrib.attrib_name]
|
|
||||||
asset_exists = Asset.query.filter_by(**{primary_attrib.attrib_name: primary_value}).first()
|
|
||||||
if asset_exists:
|
|
||||||
existing_assets.append(row)
|
|
||||||
else:
|
|
||||||
new_assets.append(row)
|
|
||||||
|
|
||||||
session['assets'] = new_assets # Store new assets in session
|
|
||||||
|
|
||||||
# Redirect to preview page with the CSV data
|
|
||||||
return render_template(
|
return render_template(
|
||||||
'csv_preview.html',
|
'upload.html',
|
||||||
new_assets=new_assets,
|
errors=errors
|
||||||
existing=existing_assets,
|
|
||||||
item_attributes=item_attributes
|
|
||||||
)
|
)
|
||||||
|
|
||||||
except Exception as e:
|
# Separate new and existing assets
|
||||||
# Handle errors during file processing
|
new_assets = []
|
||||||
flash(f"Error processing CSV file: {str(e)}", "error")
|
existing_assets = []
|
||||||
return redirect(request.url)
|
for row in csvdata:
|
||||||
|
primary_value = row[primary_attrib.attrib_name]
|
||||||
|
asset_exists = Asset.query.filter_by(**{primary_attrib.attrib_name: primary_value}).first()
|
||||||
|
if asset_exists:
|
||||||
|
existing_assets.append(row)
|
||||||
|
else:
|
||||||
|
new_assets.append(row)
|
||||||
|
|
||||||
|
# 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_assets=existing_assets,
|
||||||
|
item_attributes=item_attributes
|
||||||
|
)
|
||||||
|
|
||||||
# Render the upload page for GET requests
|
# Render the upload page for GET requests
|
||||||
return render_template('upload.html')
|
return render_template('upload.html')
|
Loading…
Reference in New Issue
Block a user