29 lines
867 B
Python
29 lines
867 B
Python
from flask import Blueprint, Response
|
|
from models import Asset
|
|
import csv
|
|
import io
|
|
|
|
export_csv_bp = Blueprint('export_csv', __name__)
|
|
|
|
@export_csv_bp.route('/export_csv', methods=['POST'])
|
|
def export_csv():
|
|
output = io.StringIO()
|
|
outcsv = csv.writer(output, 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])
|
|
|
|
output.seek(0) # Move cursor to the beginning
|
|
|
|
# Send the file as a download
|
|
return Response(
|
|
output.getvalue(),
|
|
mimetype="text/csv",
|
|
headers={"Content-Disposition": "attachment; filename=inventory_export.csv"}
|
|
) |