Write csv data to DB

This commit is contained in:
Candifloss 2025-02-01 23:23:13 +05:30
parent f7009d6e82
commit 9866bcfa12
2 changed files with 22 additions and 4 deletions

View File

@ -1,7 +1,7 @@
from flask import Blueprint, request, render_template, redirect
from flask import Blueprint, request, render_template, redirect, session
import csv
from io import TextIOWrapper
from models import Asset
from models import Asset, db
from process_csv import get_csv_data # Import the CSV processing function
# Create a Blueprint for upload
@ -23,6 +23,7 @@ def upload_file():
try:
# Extract data from the CSV file
assets = get_csv_data(file)
session['assets'] = assets # Store assets in session
# Redirect to preview page with the CSV data
return render_template('csv_preview.html', assets=assets)
@ -34,3 +35,20 @@ def upload_file():
# Render the upload page for GET requests
return render_template('upload.html')
# When confirmed, write the csv data to the database
@upload_bp.route('/confirm_save', methods=['POST'])
def confirm_save():
assets = session.get('assets', []) # Retrieve assets from session
for asset_data in assets:
asset = Asset(
assettag=asset_data['assettag'],
hostname=asset_data['hostname'],
warrantyfrom=asset_data['warrantyfrom'],
status=asset_data['status'],
staffnum=asset_data['staffnum']
)
db.session.add(asset)
db.session.commit()
session.pop('assets', None) # Clear session data
return redirect('/viewall')

View File

@ -32,7 +32,7 @@
</tbody>
</table>
<!-- Form to confirm and save data -->
<!-- Form button to confirm and save data -->
<form action="/confirm_save" method="POST">
<button type="submit">Confirm and Save to Database</button>
</form>