diff --git a/app.py b/app.py index 359dd29..f6e4d28 100644 --- a/app.py +++ b/app.py @@ -1,8 +1,6 @@ -from flask import Flask, request, render_template, redirect, abort, send_file +from flask import Flask, redirect from sqlalchemy import exc from models import * -import csv -from process_csv import process_csv # Import the CSV processing function # Import the Blueprints from routes.viewall import viewall_bp diff --git a/process_csv.py b/process_csv.py index 27b524e..f733412 100644 --- a/process_csv.py +++ b/process_csv.py @@ -1,34 +1,29 @@ import csv from io import TextIOWrapper -from models import db, Asset # Import your database and model -def process_csv(file): +def get_csv_data(file): """ - Processes the uploaded CSV file and returns a list of Asset objects. + Processes the uploaded CSV file and returns a list of asset dictionaries. Raises an exception if the file is invalid or data is incorrect. """ + # Open and process the CSV file csv_file = TextIOWrapper(file, encoding='utf-8') - reader = csv.DictReader(csv_file) + reader = csv.DictReader(csv_file, delimiter='|') # Validate CSV headers required_headers = ['assettag', 'hostname', 'warrantyfrom', 'status', 'staffnum'] if not all(header in reader.fieldnames for header in required_headers): raise ValueError("CSV file must include headers: assettag, hostname, warrantyfrom, status, staffnum.") - # Process each row + # Extract data from the CSV file assets = [] for row in reader: - # Validate required fields - if not row['assettag']: - raise ValueError("Missing 'assettag' in one or more rows.") + assets.append({ + 'assettag': row['assettag'], + 'hostname': row['hostname'], + 'warrantyfrom': row['warrantyfrom'], + 'status': row['status'], + 'staffnum': row['staffnum'] + }) - # Create Asset object - assets.append(Asset( - assettag=row['assettag'], - hostname=row['hostname'], - warrantyfrom=row['warrantyfrom'], - status=row['status'], - staffnum=row['staffnum'] - )) - - return assets + return assets \ No newline at end of file diff --git a/routes/csvpreview.py b/routes/csvpreview.py deleted file mode 100644 index 94e3804..0000000 --- a/routes/csvpreview.py +++ /dev/null @@ -1,25 +0,0 @@ -from flask import Blueprint, request, render_template -from models import Asset, db - -viewcsv_bp = Blueprint('viewcsv', __name__) - -@viewcsv_bp.route('/csv_preview', methods=['GET', 'POST']) -def csv_preview(): - # Retrieve the CSV data from the session - csv_data = session.get('csv_data', []) - - if request.method == 'POST': - # Save the data to the database - assets = [AssetTest(**row) for row in csv_data] - db.session.add_all(assets) - db.session.commit() - - # Clear the session data - session.pop('csv_data', None) - - # Redirect to view list page with success message - flash("Data saved to the database successfully!", 'success') - return redirect(url_for('view_list')) - - # Render the preview page for GET requests - return render_template('csv_preview.html', csv_data=csv_data) \ No newline at end of file diff --git a/routes/upload.py b/routes/upload.py index e8d3df3..ca6d59a 100644 --- a/routes/upload.py +++ b/routes/upload.py @@ -1,6 +1,10 @@ from flask import Blueprint, request, render_template, redirect -from models import Asset, db +import csv +from io import TextIOWrapper +from models import Asset +from process_csv import get_csv_data # Import the CSV processing function +# Create a Blueprint for upload upload_bp = Blueprint('uploadcsv', __name__) @upload_bp.route('/uploadcsv', methods=['GET', 'POST']) @@ -8,30 +12,25 @@ def upload_file(): if request.method == 'POST': # Check if a file was uploaded if 'file' not in request.files: - flash("No file uploaded.", 'error') - return redirect(url_for('upload_file')) + return redirect('uploadcsv') file = request.files['file'] # Check if the file is a CSV if file.filename == '' or not file.filename.endswith('.csv'): - flash("Please upload a valid CSV file.", 'error') - return redirect(url_for('upload_file')) + return redirect('uploadcsv') try: - # Process the CSV file - assets = process_csv(file) + # Extract data from the CSV file + assets = get_csv_data(file) - # Store the processed data in the session for preview - session['csv_data'] = [asset.__dict__ for asset in assets] - - # Redirect to preview page - return redirect(url_for('csv_preview')) + # Redirect to preview page with the CSV data + return render_template('csv_preview.html', assets=assets) except Exception as e: # Handle errors during file processing - flash(f"Error processing CSV file: {str(e)}", 'error') - return redirect(url_for('upload_file')) + print(f"Error processing CSV file: {str(e)}") + return redirect('uploadcsv') # Render the upload page for GET requests return render_template('upload.html') \ No newline at end of file diff --git a/templates/csv_preview.html b/templates/csv_preview.html index ce3a0d6..b212f0b 100644 --- a/templates/csv_preview.html +++ b/templates/csv_preview.html @@ -3,19 +3,10 @@ - Preview CSV Data + CSV Preview -

Preview CSV Data

- - - {% with messages = get_flashed_messages(with_categories=true) %} - {% if messages %} - {% for category, message in messages %} -

{{ message }}

- {% endfor %} - {% endif %} - {% endwith %} +

CSV Preview

@@ -29,20 +20,20 @@ - {% for row in csv_data %} + {% for asset in assets %} - - - - - + + + + + {% endfor %}
{{ row.assettag }}{{ row.hostname }}{{ row.warrantyfrom }}{{ row.status }}{{ row.staffnum }}{{ asset.assettag }}{{ asset.hostname }}{{ asset.warrantyfrom }}{{ asset.status }}{{ asset.staffnum }}
- -
+ +