Moved export csv to a separate route

This commit is contained in:
Candifloss 2025-02-11 13:17:57 +05:30
parent 48a07b7e31
commit 7b6418f185
4 changed files with 32 additions and 16 deletions

2
app.py
View File

@ -4,6 +4,7 @@ from models import *
# Import the Blueprints
from routes.viewall import viewall_bp
from routes.export_csv import export_csv_bp
from routes.viewasset import viewasset_bp
from routes.create import addasset_bp
from routes.update import update_bp
@ -19,6 +20,7 @@ db.init_app(app)
# Register the Blueprints
app.register_blueprint(viewall_bp)
app.register_blueprint(export_csv_bp)
app.register_blueprint(viewasset_bp)
app.register_blueprint(addasset_bp)
app.register_blueprint(update_bp)

24
routes/export_csv.py Normal file
View File

@ -0,0 +1,24 @@
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)

View File

@ -1,21 +1,11 @@
from flask import Blueprint, request, render_template, send_file
from models import Asset, db
import csv
from flask import Blueprint, render_template
from models import Asset
from config import item_attributes
viewall_bp = Blueprint('viewall', __name__)
@viewall_bp.route('/viewall/', methods=['GET', 'POST'])
@viewall_bp.route('/viewall/', methods=['GET'])
def view_list():
if request.method == 'POST':
outfile = open('inventory_export.csv', 'w', newline='')
outcsv = csv.writer(outfile, delimiter='|')
records = db.session.query(Asset).all()
outcsv.writerow([column.name for column in Asset.__mapper__.columns])
[outcsv.writerow([getattr(curr, column.name) for column in Asset.__mapper__.columns]) for curr in records]
outfile.close()
return send_file('inventory_export.csv', as_attachment=True)
items = Asset.query.all()
primary_attrib = next(
(attrib for attrib, config in item_attributes.items() if config.primary),

View File

@ -9,14 +9,14 @@
<table border="1" align="center">
<tr>
<!-- Dynamically generate table headers -->
<!-- Table headers -->
{% for attrib, config in item_attributes.items() %}
<th>{{ config.display_name }}</th>
{% endfor %}
<th colspan="2">Actions</th>
</tr>
<!-- Dynamically generate table rows -->
<!-- Table rows -->
{% for item in items %}
<tr>
{% for attrib, config in item_attributes.items() %}
@ -43,7 +43,7 @@
<form action="/uploadcsv" method="get">
<button type="submit">Import from CSV</button>
</form>
<form method="POST">
<form action="/export_csv" method="POST">
<button type="submit">Export Data</button>
</form>
</body>