2025-01-30 07:37:21 +00:00
|
|
|
from flask import Blueprint, request, render_template, redirect
|
2025-01-30 09:58:34 +00:00
|
|
|
import csv
|
|
|
|
from io import TextIOWrapper
|
|
|
|
from models import Asset
|
|
|
|
from process_csv import get_csv_data # Import the CSV processing function
|
2025-01-30 07:37:21 +00:00
|
|
|
|
2025-01-30 09:58:34 +00:00
|
|
|
# Create a Blueprint for upload
|
2025-01-30 07:37:21 +00:00
|
|
|
upload_bp = Blueprint('uploadcsv', __name__)
|
|
|
|
|
|
|
|
@upload_bp.route('/uploadcsv', methods=['GET', 'POST'])
|
|
|
|
def upload_file():
|
|
|
|
if request.method == 'POST':
|
|
|
|
# Check if a file was uploaded
|
|
|
|
if 'file' not in request.files:
|
2025-01-30 09:58:34 +00:00
|
|
|
return redirect('uploadcsv')
|
2025-01-30 07:37:21 +00:00
|
|
|
|
|
|
|
file = request.files['file']
|
|
|
|
|
|
|
|
# Check if the file is a CSV
|
|
|
|
if file.filename == '' or not file.filename.endswith('.csv'):
|
2025-01-30 09:58:34 +00:00
|
|
|
return redirect('uploadcsv')
|
2025-01-30 07:37:21 +00:00
|
|
|
|
|
|
|
try:
|
2025-01-30 09:58:34 +00:00
|
|
|
# Extract data from the CSV file
|
|
|
|
assets = get_csv_data(file)
|
2025-01-30 07:37:21 +00:00
|
|
|
|
2025-01-30 09:58:34 +00:00
|
|
|
# Redirect to preview page with the CSV data
|
|
|
|
return render_template('csv_preview.html', assets=assets)
|
2025-01-30 07:37:21 +00:00
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
# Handle errors during file processing
|
2025-01-30 09:58:34 +00:00
|
|
|
print(f"Error processing CSV file: {str(e)}")
|
|
|
|
return redirect('uploadcsv')
|
2025-01-30 07:37:21 +00:00
|
|
|
|
|
|
|
# Render the upload page for GET requests
|
|
|
|
return render_template('upload.html')
|