FIxed csv upload

This commit is contained in:
Candifloss 2025-03-05 11:54:41 +05:30
parent cba1615750
commit 71866cd883
3 changed files with 38 additions and 38 deletions

View File

@ -4,11 +4,12 @@ from config import item_attributes
def get_csv_data(file): def get_csv_data(file):
"""Extract and validate CSV data.""" """Extract and validate CSV data."""
try:
csv_file = TextIOWrapper(file, encoding='utf-8') csv_file = TextIOWrapper(file, encoding='utf-8')
# Check delimiter # Check delimiter
sample = csv_file.read(1024) sample = csv_file.read(1024)
csv_file.seek(0) csv_file.seek(0) # Reset the file pointer
if '|' not in sample: if '|' not in sample:
raise ValueError("CSV file must use '|' as the delimiter.") raise ValueError("CSV file must use '|' as the delimiter.")
@ -34,3 +35,7 @@ def get_csv_data(file):
{display_to_attrib[header]: row[header] for header in required_headers} {display_to_attrib[header]: row[header] for header in required_headers}
for row in reader for row in reader
] ]
except Exception as e:
# Log the error and re-raise it
print(f"Error processing CSV file: {str(e)}")
raise

View File

@ -42,11 +42,10 @@ def upload_file():
errors.append(f"Row {i}: {error}") errors.append(f"Row {i}: {error}")
if errors: if errors:
# Pass errors as a list to the template # Flash all errors and redirect back to the upload page
return render_template( for error in errors:
'upload.html', flash(error, "error")
errors=errors return redirect(request.url)
)
# Separate new and existing assets # Separate new and existing assets
new_assets = [] new_assets = []

View File

@ -35,16 +35,12 @@
type="{{ attrib.html_input_type }}" type="{{ attrib.html_input_type }}"
name="{{ attrib.attrib_name }}" name="{{ attrib.attrib_name }}"
{% if attrib.required %} required {% endif %} {% if attrib.required %} required {% endif %}
{% if attrib.html_input_type == "number" %}
{% if attrib.min_val is not none %} min="{{ attrib.min_val }}" {% endif %} {% if attrib.min_val is not none %} min="{{ attrib.min_val }}" {% endif %}
{% if attrib.max_val is not none %} max="{{ attrib.max_val }}" {% endif %} {% if attrib.max_val is not none %} max="{{ attrib.max_val }}" {% endif %}
{% if attrib.step is not none %} step="{{ attrib.step }}" {% endif %} {% if attrib.step is not none %} step="{{ attrib.step }}" {% endif %}
{% endif %}
{% if attrib.html_input_type == "text" %}
{% if attrib.max_length is not none %} maxlength="{{ attrib.max_length }}" {% endif %} {% if attrib.max_length is not none %} maxlength="{{ attrib.max_length }}" {% endif %}
{% if attrib.min_length is not none %} minlength="{{ attrib.min_length }}" {% endif %} {% if attrib.min_length is not none %} minlength="{{ attrib.min_length }}" {% endif %}
{% if attrib.regex is not none %} pattern="{{ attrib.regex }}" {% endif %} {% if attrib.regex is not none %} pattern="{{ attrib.regex }}" {% endif %}
{% endif %}
{% if attrib.default_val is not none %} value="{{ attrib.default_val }}" {% endif %} {% if attrib.default_val is not none %} value="{{ attrib.default_val }}" {% endif %}
/> />
{% endif %} {% endif %}