25 lines
847 B
Python
25 lines
847 B
Python
|
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)
|