from flask import Flask, request, send_file, render_template, redirect import io import os from csv2gpc import convert_csv_to_gpc, mapping_sparkasse, mapping_pko, mapping_wise from allegro import convert_csv_to_gpc_allegro, mapping_allegro import tempfile import datetime app = Flask(__name__) def convert_avizo_na_jednu_platbu(data): # Replace with your real conversion code return data def convert_avizo_full_auto(data): # Replace with your real conversion code return data def convert_allegro(data): # Replace with your real conversion code return data def create_download_response(file_data: bytes, filename: str, mimetype: str = "text/plain"): """Wrap the file data in a BytesIO stream and return it as a downloadable response.""" return send_file( io.BytesIO(file_data), download_name=filename, as_attachment=True, mimetype=mimetype ) @app.route("/", methods=["GET", "POST"]) def index(): if request.method == "POST": if "file" not in request.files or request.files["file"].filename == "": return redirect(request.url) file = request.files["file"] option = request.form.get("option") suffix = os.path.splitext(file.filename)[1] with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp_input: file.save(tmp_input.name) tmp_file_path = tmp_input.name day_of_year = int(datetime.date.today().strftime("%j")) out_filename = f"{option}_vypis_{day_of_year}.gpc" mimetype = "text/plain" # Choose the conversion function based on the selected option if option == "sparkasse": result_data = convert_csv_to_gpc(tmp_file_path, f"{option}_vypis_{day_of_year}.gpc", account_number=95850503000221267034, currency="EUR", mapping=mapping_sparkasse) elif option == "wise_huf": result_data = convert_csv_to_gpc(tmp_file_path, f"{option}_vypis_{day_of_year}.gpc", account_number=330005602964780100, currency="HUF", mapping=mapping_wise) elif option == "wise_ron": result_data = convert_csv_to_gpc(tmp_file_path, f"{option}_vypis_{day_of_year}.gpc", account_number=5602964780100, currency="RON", mapping=mapping_wise) elif option == "pko": result_data = convert_csv_to_gpc(tmp_file_path, f"{option}_vypis_{day_of_year}.gpc", account_number=95102013900000630206821286, currency="PLN", mapping=mapping_pko) elif option == "allegro": result_data = convert_csv_to_gpc_allegro(tmp_file_path, f"{option}_vypis_{day_of_year}.gpc", account_number=3214724742, currency="PLN", mapping=mapping_allegro) return send_file( result_data, download_name="allegro_files.zip", as_attachment=True, mimetype="application/zip" ) if isinstance(result_data, str): result_bytes = result_data.encode("utf-8") else: result_bytes = result_data return create_download_response(result_bytes, out_filename, mimetype) return render_template("index.html") if __name__ == '__main__': app.run(debug=True)