24 lines
758 B
Python
24 lines
758 B
Python
from flask import Blueprint, send_file
|
|
from models import Asset
|
|
import csv
|
|
|
|
export_csv_bp = Blueprint('export_csv', __name__)
|
|
|
|
@export_csv_bp.route('/export_csv', methods=['POST'])
|
|
def export_csv():
|
|
outfile = open('inventory_export.csv', 'w', newline='')
|
|
outcsv = csv.writer(outfile, delimiter='|')
|
|
|
|
records = Asset.query.all() # Fetch all records from the database
|
|
|
|
# Write headers
|
|
outcsv.writerow([column.name for column in Asset.__mapper__.columns])
|
|
|
|
# Write data rows
|
|
for record in records:
|
|
outcsv.writerow([getattr(record, column.name) for column in Asset.__mapper__.columns])
|
|
|
|
outfile.close()
|
|
|
|
# Send the file as a download
|
|
return send_file('inventory_export.csv', as_attachment=True) |