Added initial CSV export functionality.
This commit is contained in:
parent
1ec15328b0
commit
4342d73811
2 changed files with 54 additions and 6 deletions
|
@ -1,5 +1,10 @@
|
|||
import io
|
||||
import csv
|
||||
import base64
|
||||
|
||||
import datetime
|
||||
from flask import request, render_template, url_for, jsonify
|
||||
from sqlalchemy.inspection import inspect
|
||||
|
||||
from . import main
|
||||
from .helpers import FILTER_MAP, inventory_headers, worklog_headers
|
||||
|
@ -207,9 +212,35 @@ def delete_inventory_item(id):
|
|||
db.session.rollback()
|
||||
return jsonify({"success": False, "error": str(e)}), 400
|
||||
|
||||
@main.route("/api/inventory/export", methods=["GET"])
|
||||
@main.route("/api/inventory/export", methods=["POST"])
|
||||
def get_inventory_csv():
|
||||
return jsonify({"success": True}), 200
|
||||
data = request.get_json()
|
||||
ids = data.get('ids', [])
|
||||
|
||||
if not ids:
|
||||
return jsonify({"success": False, "error": "No IDs provided"}), 400
|
||||
|
||||
rows = db.session.query(Inventory).filter(Inventory.id.in_(ids)).all()
|
||||
|
||||
output = io.StringIO()
|
||||
writer = csv.writer(output)
|
||||
|
||||
model = Inventory
|
||||
columns = [c.key for c in inspect(model).mapper.column_attrs]
|
||||
|
||||
writer.writerow(columns)
|
||||
|
||||
for item in rows:
|
||||
writer.writerow([getattr(item, col) for col in columns])
|
||||
|
||||
csv_string = output.getvalue()
|
||||
output.close()
|
||||
|
||||
return jsonify({
|
||||
"success": True,
|
||||
"csv": base64.b64encode(csv_string.encode()).decode(),
|
||||
"count": len(rows)
|
||||
})
|
||||
|
||||
@main.route("/inventory_available")
|
||||
def inventory_available():
|
||||
|
|
|
@ -12,17 +12,34 @@
|
|||
|
||||
try {
|
||||
const response = await fetch('/api/{{ csv_route }}/export', {
|
||||
method: "GET",
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json"
|
||||
},
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
|
||||
const result = await response.json();
|
||||
console.log(result);
|
||||
|
||||
if (result.success) {
|
||||
const decodedCsv = atob(result.csv);
|
||||
const blob = new Blob([decodedCsv], { type: "text/csv" });
|
||||
const url = URL.createObjectURL(blob);
|
||||
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
link.download = "{{ csv_route }}_export.csv";
|
||||
link.click();
|
||||
|
||||
console.log(url);
|
||||
|
||||
URL.revokeObjectURL(url);
|
||||
} else {
|
||||
renderToast({ message: `Export failed: ${result.error}` });
|
||||
}
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
renderToast({ message: `Export failed: ${err}` });
|
||||
}
|
||||
{% endset %}
|
||||
{% set toolbarButtons %}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue