From d4e7c0833b06d23494820dcd6ef5c0febb8ed064 Mon Sep 17 00:00:00 2001 From: candifloss Date: Mon, 24 Feb 2025 02:24:00 +0530 Subject: [PATCH] Attempt validation for csv --- functions/process_csv.py | 8 +++++ routes/upload.py | 38 +++++++++++++------- templates/csv_preview.html | 71 +++++++++++++++----------------------- templates/upload.html | 29 +++++++++++++--- 4 files changed, 87 insertions(+), 59 deletions(-) diff --git a/functions/process_csv.py b/functions/process_csv.py index ee96241..c3155cd 100644 --- a/functions/process_csv.py +++ b/functions/process_csv.py @@ -3,7 +3,15 @@ from io import TextIOWrapper from config import item_attributes def get_csv_data(file): + """Extract and validate CSV data.""" 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.") + reader = csv.DictReader(csv_file, delimiter='|') # Validate CSV headers based on config diff --git a/routes/upload.py b/routes/upload.py index 0598285..02c8aeb 100644 --- a/routes/upload.py +++ b/routes/upload.py @@ -1,6 +1,7 @@ -from flask import Blueprint, request, render_template, redirect, session +from flask import Blueprint, request, render_template, redirect, session, flash from definitions.models import Asset, db from functions.process_csv import get_csv_data +from functions.validate_values import validate_values from config import item_attributes upload_bp = Blueprint('uploadcsv', __name__) @@ -10,26 +11,39 @@ def upload_file(): if request.method == 'POST': # Check if a file was uploaded if 'file' not in request.files: - return redirect('uploadcsv') + flash("No file uploaded.", "error") + return redirect(request.url) file = request.files['file'] # Check if the file is a CSV if file.filename == '' or not file.filename.endswith('.csv'): - return redirect('uploadcsv') + flash("Please upload a valid CSV file.", "error") + return redirect(request.url) try: - # Extract data from the CSV file + # Extract and validate CSV data csvdata = get_csv_data(file) - # Identify the primary attribute - primary_attrib = next( - (attrib for attrib in item_attributes if attrib.primary), - None - ) + # Validate each row of data + errors = [] + for i, row in enumerate(csvdata, start=1): + error = validate_values(row) + if error: + errors.append(f"Row {i}: {error}") + if errors: + # 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: - return "Primary attribute not defined in configuration." + flash("Primary attribute not defined in configuration.", "error") + return redirect(request.url) # Separate new and existing assets new_assets = [] @@ -54,8 +68,8 @@ def upload_file(): except Exception as e: # Handle errors during file processing - print(f"Error processing CSV file: {str(e)}") - return redirect('uploadcsv') + flash(f"Error processing CSV file: {str(e)}", "error") + return redirect(request.url) # Render the upload page for GET requests return render_template('upload.html') \ No newline at end of file diff --git a/templates/csv_preview.html b/templates/csv_preview.html index d73a2f0..ee8159a 100644 --- a/templates/csv_preview.html +++ b/templates/csv_preview.html @@ -9,50 +9,35 @@

CSV Preview

- - {% if new_assets %} -

New assets:

- - - - {% for attrib in item_attributes %} - - {% endfor %} - - - - {% for asset in new_assets %} - - {% for attrib in item_attributes %} - + + {% for table_name, assets, editable in [ + ('New Assets', new_assets, true), + ('Existing Assets', existing, false) + ] %} + {% if assets %} +

{{ table_name }}

+
{{ attrib.display_name }}
{{ asset[attrib.attrib_name] }}
+ + + {% for attrib in item_attributes %} + + {% endfor %} + + + + {% for asset in assets %} + + {% for attrib in item_attributes %} + + {% endfor %} + {% endfor %} - - {% endfor %} - -
{{ attrib.display_name }}
+ {{ asset[attrib.attrib_name] }} +
- {% endif %} - - {% if existing %} -

These assets are already in the database:

- - - - {% for attrib in item_attributes %} - - {% endfor %} - - - - {% for asset in existing %} - - {% for attrib in item_attributes %} - - {% endfor %} - - {% endfor %} - -
{{ attrib.display_name }}
{{ asset[attrib.attrib_name] }}
- {% endif %} + + + {% endif %} + {% endfor %} {% if new_assets %} diff --git a/templates/upload.html b/templates/upload.html index 66887ca..d128491 100644 --- a/templates/upload.html +++ b/templates/upload.html @@ -7,15 +7,36 @@

Upload CSV File

+ + + {% with messages = get_flashed_messages(with_categories=true) %} + {% if messages %} + {% for category, message in messages %} +

{{ message }}

+ {% endfor %} + {% endif %} + {% endwith %} + + + {% if errors %} +
+ + Errors found in the CSV file (click to expand): + + +
+ {% endif %} + +


- - {% if error %} -

{{ error }}

- {% endif %} \ No newline at end of file