From 22e7919fe5a2c0c14b8ff0421a56007490ebe424 Mon Sep 17 00:00:00 2001 From: candifloss Date: Mon, 7 Apr 2025 07:26:26 +0530 Subject: [PATCH] Fix: CSV format information - Dynamically make list of column headers - Generate example csv file from config --- routes/upload.py | 38 ++++++++++++++++++++++++++++++++++---- templates/upload.html | 35 ++++++++++++++++++++++++----------- 2 files changed, 58 insertions(+), 15 deletions(-) diff --git a/routes/upload.py b/routes/upload.py index 59ef4af..27964f9 100644 --- a/routes/upload.py +++ b/routes/upload.py @@ -2,9 +2,8 @@ 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 +from config import item_attributes, BrandingConfig from functions.auth import login_required -from config import BrandingConfig upload_bp = Blueprint('uploadcsv', __name__) @@ -95,7 +94,7 @@ def import_from_csv(): ) # Render the upload page for GET requests - return render_template('upload.html', mode="import", brandingconfig=BrandingConfig) + return render_template('upload.html', mode="import", item_attributes=item_attributes, brandingconfig=BrandingConfig) @upload_bp.route('/edit_using_csv/', methods=['GET', 'POST']) @login_required @@ -136,4 +135,35 @@ def edit_using_csv(): ) # Render the upload page for GET requests - return render_template('upload.html', mode="edit", brandingconfig=BrandingConfig) \ No newline at end of file + return render_template('upload.html', mode="edit", item_attributes=item_attributes, brandingconfig=BrandingConfig) + +@upload_bp.route('/download_template/') +@login_required +def download_template(): + """Generate and serve a CSV template""" + from io import StringIO + from csv import writer + + # Create in-memory CSV file + csv_data = StringIO() + csv_writer = writer(csv_data, delimiter='|') + + # Write headers + headers = [attrib.display_name for attrib in item_attributes] + csv_writer.writerow(headers) + + # Write example row with default values if available + example_row = [] + for attrib in item_attributes: + if attrib.default_val is not None: + example_row.append(str(attrib.default_val)) + else: + example_row.append(f"example_{attrib.attrib_name}") + csv_writer.writerow(example_row) + + # Prepare response + from flask import make_response + response = make_response(csv_data.getvalue()) + response.headers['Content-Type'] = 'text/csv' + response.headers['Content-Disposition'] = 'attachment; filename=csv_sample.csv' + return response \ No newline at end of file diff --git a/templates/upload.html b/templates/upload.html index f063113..6486dc1 100644 --- a/templates/upload.html +++ b/templates/upload.html @@ -84,19 +84,32 @@ CSV Format Help -
+
- -

Your CSV should include the following columns:

-