diff --git a/routes/upload.py b/routes/upload.py index 83d37b3..3f6388c 100644 --- a/routes/upload.py +++ b/routes/upload.py @@ -3,6 +3,7 @@ import csv from io import TextIOWrapper from models import Asset, db from process_csv import get_csv_data # Import the CSV processing function +from config import item_attributes # Import the configuration # Create a Blueprint for upload upload_bp = Blueprint('uploadcsv', __name__) @@ -23,20 +24,31 @@ def upload_file(): try: # Extract data from the CSV file csvdata = get_csv_data(file) + + # Identify the primary attribute + primary_attrib = next( + (attrib for attrib, config in item_attributes.items() if config.primary), + None + ) + + if not primary_attrib: + return "Primary attribute not defined in configuration." + + # Separate new and existing assets new_assets = [] existing_assets = [] for row in csvdata: - tag = row['assettag'] - asset_exists = Asset.query.filter_by(assettag=tag).first() + primary_value = row[primary_attrib] + asset_exists = Asset.query.filter_by(**{primary_attrib: primary_value}).first() if asset_exists: existing_assets.append(row) else: new_assets.append(row) - session['assets'] = new_assets # Store assets in session + session['assets'] = new_assets # Store new assets in session # Redirect to preview page with the CSV data - return render_template('csv_preview.html', new_assets=new_assets, existing=existing_assets) + return render_template('csv_preview.html', new_assets=new_assets, existing=existing_assets, item_attributes=item_attributes) except Exception as e: # Handle errors during file processing @@ -44,4 +56,4 @@ def upload_file(): return redirect('uploadcsv') # Render the upload page for GET requests - return render_template('upload.html') + return render_template('upload.html') \ No newline at end of file diff --git a/templates/csv_preview.html b/templates/csv_preview.html index ee7aa02..b146a60 100644 --- a/templates/csv_preview.html +++ b/templates/csv_preview.html @@ -4,7 +4,7 @@ CSV Preview - +

CSV Preview

@@ -15,21 +15,17 @@ - - - - - + {% for attrib, config in item_attributes.items() %} + + {% endfor %} {% for asset in new_assets %} - - - - - + {% for attrib, config in item_attributes.items() %} + + {% endfor %} {% endfor %} @@ -41,21 +37,17 @@
Asset TagHostnameWarranty FromStatusStaff Number{{ config.display_name }}
{{ asset.assettag }}{{ asset.hostname }}{{ asset.warrantyfrom }}{{ asset.status }}{{ asset.staffnum }}{{ asset[attrib] }}
- - - - - + {% for attrib, config in item_attributes.items() %} + + {% endfor %} {% for asset in existing %} - - - - - + {% for attrib, config in item_attributes.items() %} + + {% endfor %} {% endfor %} @@ -64,10 +56,11 @@ {% if new_assets %} - + {% endif %} +
Asset TagHostnameWarranty FromStatusStaff Number{{ config.display_name }}
{{ asset.assettag }}{{ asset.hostname }}{{ asset.warrantyfrom }}{{ asset.status }}{{ asset.staffnum }}{{ asset[attrib] }}