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:
-id
- Required for editsname
- Item nameYour CSV must include these columns (case-sensitive):
+{{ attrib.display_name }}
+ {% if attrib.required %}Required{% endif %}
+ {% if attrib.primary %}Primary{% endif %}
+ {% if attrib.unique %}Unique{% endif %}
+ |
(pipe character)