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,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
]
# 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

View File

@ -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 = []

View File

@ -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 %}