FIxed csv upload
This commit is contained in:
parent
cba1615750
commit
71866cd883
@ -4,33 +4,38 @@ 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."""
|
||||||
csv_file = TextIOWrapper(file, encoding='utf-8')
|
try:
|
||||||
|
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.")
|
||||||
|
|
||||||
reader = csv.DictReader(csv_file, delimiter='|')
|
reader = csv.DictReader(csv_file, delimiter='|')
|
||||||
|
|
||||||
# Validate CSV headers based on config
|
# Validate CSV headers based on config
|
||||||
required_headers = {attrib.display_name for attrib in item_attributes} # Use display names
|
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
|
csv_headers = set(reader.fieldnames) # Get headers present in the CSV file
|
||||||
|
|
||||||
# Check if the required headers exist
|
# Check if the required headers exist
|
||||||
if not required_headers.issubset(csv_headers):
|
if not required_headers.issubset(csv_headers):
|
||||||
raise ValueError(f"CSV file must include headers: {', '.join(required_headers)}.")
|
raise ValueError(f"CSV file must include headers: {', '.join(required_headers)}.")
|
||||||
|
|
||||||
# Check for invalid headers
|
# Check for invalid headers
|
||||||
if extra_headers := csv_headers - required_headers:
|
if extra_headers := csv_headers - required_headers:
|
||||||
raise ValueError(f"Unexpected headers found: {', '.join(extra_headers)}")
|
raise ValueError(f"Unexpected headers found: {', '.join(extra_headers)}")
|
||||||
|
|
||||||
# Map display names to attribute names
|
# Map display names to attribute names
|
||||||
display_to_attrib = {attrib.display_name: attrib.attrib_name for attrib in item_attributes}
|
display_to_attrib = {attrib.display_name: attrib.attrib_name for attrib in item_attributes}
|
||||||
|
|
||||||
# Extract data dynamically based on config
|
# Extract data dynamically based on config
|
||||||
return [
|
return [
|
||||||
{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
|
@ -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 = []
|
||||||
|
@ -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 %}
|
{% if attrib.max_length is not none %} maxlength="{{ attrib.max_length }}" {% endif %}
|
||||||
{% endif %}
|
{% if attrib.min_length is not none %} minlength="{{ attrib.min_length }}" {% endif %}
|
||||||
{% if attrib.html_input_type == "text" %}
|
{% if attrib.regex is not none %} pattern="{{ attrib.regex }}" {% 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 %}
|
|
||||||
{% 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 %}
|
||||||
|
Loading…
Reference in New Issue
Block a user