From 71866cd883267b59e342261283fb845869815599 Mon Sep 17 00:00:00 2001 From: candifloss Date: Wed, 5 Mar 2025 11:54:41 +0530 Subject: [PATCH] FIxed csv upload --- functions/process_csv.py | 51 ++++++++++++++++++++++------------------ routes/upload.py | 9 ++++--- templates/create.html | 16 +++++-------- 3 files changed, 38 insertions(+), 38 deletions(-) diff --git a/functions/process_csv.py b/functions/process_csv.py index c3155cd..3b59392 100644 --- a/functions/process_csv.py +++ b/functions/process_csv.py @@ -4,33 +4,38 @@ from config import item_attributes def get_csv_data(file): """Extract and validate CSV data.""" - csv_file = TextIOWrapper(file, encoding='utf-8') + try: + csv_file = TextIOWrapper(file, encoding='utf-8') - # Check delimiter - sample = csv_file.read(1024) - csv_file.seek(0) - if '|' not in sample: - raise ValueError("CSV file must use '|' as the delimiter.") + # Check delimiter + sample = csv_file.read(1024) + csv_file.seek(0) # Reset the file pointer + if '|' not in sample: + raise ValueError("CSV file must use '|' as the delimiter.") - reader = csv.DictReader(csv_file, delimiter='|') + reader = csv.DictReader(csv_file, delimiter='|') - # Validate CSV headers based on config - required_headers = {attrib.display_name for attrib in item_attributes} # Use display names - csv_headers = set(reader.fieldnames) # Get headers present in the CSV file + # Validate CSV headers based on config + required_headers = {attrib.display_name for attrib in item_attributes} # Use display names + csv_headers = set(reader.fieldnames) # Get headers present in the CSV file - # Check if the required headers exist - if not required_headers.issubset(csv_headers): - raise ValueError(f"CSV file must include headers: {', '.join(required_headers)}.") + # Check if the required headers exist + if not required_headers.issubset(csv_headers): + raise ValueError(f"CSV file must include headers: {', '.join(required_headers)}.") - # Check for invalid headers - if extra_headers := csv_headers - required_headers: - raise ValueError(f"Unexpected headers found: {', '.join(extra_headers)}") + # Check for invalid headers + if extra_headers := csv_headers - required_headers: + raise ValueError(f"Unexpected headers found: {', '.join(extra_headers)}") - # Map display names to attribute names - display_to_attrib = {attrib.display_name: attrib.attrib_name for attrib in item_attributes} + # Map display names to attribute names + display_to_attrib = {attrib.display_name: attrib.attrib_name for attrib in item_attributes} - # Extract data dynamically based on config - return [ - {display_to_attrib[header]: row[header] for header in required_headers} - for row in reader - ] \ No newline at end of file + # Extract data dynamically based on config + return [ + {display_to_attrib[header]: row[header] for header in required_headers} + for row in reader + ] + except Exception as e: + # Log the error and re-raise it + print(f"Error processing CSV file: {str(e)}") + raise \ No newline at end of file diff --git a/routes/upload.py b/routes/upload.py index ba56c34..222eede 100644 --- a/routes/upload.py +++ b/routes/upload.py @@ -42,11 +42,10 @@ def upload_file(): errors.append(f"Row {i}: {error}") if errors: - # Pass errors as a list to the template - return render_template( - 'upload.html', - errors=errors - ) + # Flash all errors and redirect back to the upload page + for error in errors: + flash(error, "error") + return redirect(request.url) # Separate new and existing assets new_assets = [] diff --git a/templates/create.html b/templates/create.html index 3b4c7fc..06ce7b3 100644 --- a/templates/create.html +++ b/templates/create.html @@ -35,16 +35,12 @@ type="{{ attrib.html_input_type }}" name="{{ attrib.attrib_name }}" {% 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.max_val is not none %} max="{{ attrib.max_val }}" {% 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.min_length is not none %} minlength="{{ attrib.min_length }}" {% endif %} - {% if attrib.regex is not none %} pattern="{{ attrib.regex }}" {% endif %} - {% 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.step is not none %} step="{{ attrib.step }}" {% 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.regex is not none %} pattern="{{ attrib.regex }}" {% endif %} {% if attrib.default_val is not none %} value="{{ attrib.default_val }}" {% endif %} /> {% endif %}