Fix: CSV format information
- Dynamically make list of column headers - Generate example csv file from config
This commit is contained in:
		
							parent
							
								
									bac2c440f1
								
							
						
					
					
						commit
						22e7919fe5
					
				@ -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)
 | 
			
		||||
    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
 | 
			
		||||
@ -84,19 +84,32 @@
 | 
			
		||||
                                CSV Format Help
 | 
			
		||||
                            </button>
 | 
			
		||||
                        </h2>
 | 
			
		||||
                        <div id="helpContent" 
 | 
			
		||||
                             class="accordion-collapse collapse" 
 | 
			
		||||
                             data-bs-parent="#helpAccordion">
 | 
			
		||||
                        <div id="helpContent" class="accordion-collapse collapse" data-bs-parent="#helpAccordion">
 | 
			
		||||
                            <div class="accordion-body">
 | 
			
		||||
                                <!-- Add your CSV format instructions here -->
 | 
			
		||||
                                <p>Your CSV should include the following columns:</p>
 | 
			
		||||
                                <ul>
 | 
			
		||||
                                    <li><code>id</code> - Required for edits</li>
 | 
			
		||||
                                    <li><code>name</code> - Item name</li>
 | 
			
		||||
                                    <!-- Add more columns as needed -->
 | 
			
		||||
                                <p>Your CSV must include these columns (case-sensitive):</p>
 | 
			
		||||
                                <ul class="list-unstyled">
 | 
			
		||||
                                    {% for attrib in item_attributes %}
 | 
			
		||||
                                    <li>
 | 
			
		||||
                                        <code>{{ attrib.display_name }}</code>
 | 
			
		||||
                                        {% if attrib.required %}<span class="badge bg-danger ms-2">Required</span>{% endif %}
 | 
			
		||||
                                        {% if attrib.primary %}<span class="badge bg-primary ms-2">Primary</span>{% endif %}
 | 
			
		||||
                                        {% if attrib.unique %}<span class="badge bg-warning text-dark ms-2">Unique</span>{% endif %}
 | 
			
		||||
                                    </li>
 | 
			
		||||
                                    {% endfor %}
 | 
			
		||||
                                </ul>
 | 
			
		||||
                                <a href="/sample.csv" class="btn btn-sm btn-outline-primary">
 | 
			
		||||
                                    Download Sample CSV
 | 
			
		||||
                                <div class="alert alert-info mt-3">
 | 
			
		||||
                                    <strong>Format requirements:</strong>
 | 
			
		||||
                                    <ul class="mt-2 mb-0">
 | 
			
		||||
                                        <li>Delimiter: <code>|</code> (pipe character)</li>
 | 
			
		||||
                                        <li>First row must contain headers exactly as shown above</li>
 | 
			
		||||
                                        {% if mode == "edit" %}
 | 
			
		||||
                                        <li>Existing items must include their primary key value</li>
 | 
			
		||||
                                        {% endif %}
 | 
			
		||||
                                    </ul>
 | 
			
		||||
                                </div>
 | 
			
		||||
                                
 | 
			
		||||
                                <a href="{{ url_for('uploadcsv.download_template') }}" class="btn btn-sm btn-outline-primary mt-2">
 | 
			
		||||
                                    <i class="bi bi-download me-1"></i>Download CSV Template
 | 
			
		||||
                                </a>
 | 
			
		||||
                            </div>
 | 
			
		||||
                        </div>
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user