Moved export csv to a separate route
This commit is contained in:
parent
48a07b7e31
commit
7b6418f185
2
app.py
2
app.py
@ -4,6 +4,7 @@ from models import *
|
|||||||
|
|
||||||
# Import the Blueprints
|
# Import the Blueprints
|
||||||
from routes.viewall import viewall_bp
|
from routes.viewall import viewall_bp
|
||||||
|
from routes.export_csv import export_csv_bp
|
||||||
from routes.viewasset import viewasset_bp
|
from routes.viewasset import viewasset_bp
|
||||||
from routes.create import addasset_bp
|
from routes.create import addasset_bp
|
||||||
from routes.update import update_bp
|
from routes.update import update_bp
|
||||||
@ -19,6 +20,7 @@ db.init_app(app)
|
|||||||
|
|
||||||
# Register the Blueprints
|
# Register the Blueprints
|
||||||
app.register_blueprint(viewall_bp)
|
app.register_blueprint(viewall_bp)
|
||||||
|
app.register_blueprint(export_csv_bp)
|
||||||
app.register_blueprint(viewasset_bp)
|
app.register_blueprint(viewasset_bp)
|
||||||
app.register_blueprint(addasset_bp)
|
app.register_blueprint(addasset_bp)
|
||||||
app.register_blueprint(update_bp)
|
app.register_blueprint(update_bp)
|
||||||
|
24
routes/export_csv.py
Normal file
24
routes/export_csv.py
Normal 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)
|
@ -1,21 +1,11 @@
|
|||||||
from flask import Blueprint, request, render_template, send_file
|
from flask import Blueprint, render_template
|
||||||
from models import Asset, db
|
from models import Asset
|
||||||
import csv
|
|
||||||
from config import item_attributes
|
from config import item_attributes
|
||||||
|
|
||||||
viewall_bp = Blueprint('viewall', __name__)
|
viewall_bp = Blueprint('viewall', __name__)
|
||||||
|
|
||||||
@viewall_bp.route('/viewall/', methods=['GET', 'POST'])
|
@viewall_bp.route('/viewall/', methods=['GET'])
|
||||||
def view_list():
|
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()
|
items = Asset.query.all()
|
||||||
primary_attrib = next(
|
primary_attrib = next(
|
||||||
(attrib for attrib, config in item_attributes.items() if config.primary),
|
(attrib for attrib, config in item_attributes.items() if config.primary),
|
||||||
|
@ -9,14 +9,14 @@
|
|||||||
|
|
||||||
<table border="1" align="center">
|
<table border="1" align="center">
|
||||||
<tr>
|
<tr>
|
||||||
<!-- Dynamically generate table headers -->
|
<!-- Table headers -->
|
||||||
{% for attrib, config in item_attributes.items() %}
|
{% for attrib, config in item_attributes.items() %}
|
||||||
<th>{{ config.display_name }}</th>
|
<th>{{ config.display_name }}</th>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
<th colspan="2">Actions</th>
|
<th colspan="2">Actions</th>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
||||||
<!-- Dynamically generate table rows -->
|
<!-- Table rows -->
|
||||||
{% for item in items %}
|
{% for item in items %}
|
||||||
<tr>
|
<tr>
|
||||||
{% for attrib, config in item_attributes.items() %}
|
{% for attrib, config in item_attributes.items() %}
|
||||||
@ -43,7 +43,7 @@
|
|||||||
<form action="/uploadcsv" method="get">
|
<form action="/uploadcsv" method="get">
|
||||||
<button type="submit">Import from CSV</button>
|
<button type="submit">Import from CSV</button>
|
||||||
</form>
|
</form>
|
||||||
<form method="POST">
|
<form action="/export_csv" method="POST">
|
||||||
<button type="submit">Export Data</button>
|
<button type="submit">Export Data</button>
|
||||||
</form>
|
</form>
|
||||||
</body>
|
</body>
|
||||||
|
Loading…
Reference in New Issue
Block a user