From 133e4805fd4a462f7ee92c08c99fad58d57c5ab9 Mon Sep 17 00:00:00 2001 From: 0ceanSlim Date: Thu, 15 Feb 2024 08:42:34 -0500 Subject: [PATCH] all utility functions moved to src/util/utils --- app.py | 21 ++------------------- src/util/utils.py | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/app.py b/app.py index 8c9171f..08031d0 100644 --- a/app.py +++ b/app.py @@ -1,28 +1,11 @@ from flask import Flask, render_template, g, abort, request, jsonify, send_from_directory, url_for -import os - from src.util.utils import * app = Flask(__name__) - -@app.before_request -def before_request(): - g.db = connect_db() - - -@app.teardown_request -def teardown_request(exception): - if hasattr(g, "db"): - g.db.close() - - -def get_js_files(): - js_folder = os.path.join(app.static_folder, "js") - js_files = [f for f in os.listdir(js_folder) if f.endswith(".js")] - return js_files - +app.before_request(before_request) +app.teardown_request(teardown_request) @app.route("/") def show_app(): diff --git a/src/util/utils.py b/src/util/utils.py index aacf2c0..bb18767 100644 --- a/src/util/utils.py +++ b/src/util/utils.py @@ -1,10 +1,25 @@ -import sqlite3, csv +from flask import g +from flask import current_app as app + +import sqlite3, csv, os DATABASE = "src/data/database.db" def connect_db(): return sqlite3.connect(DATABASE) +def before_request(): + g.db = connect_db() + +def teardown_request(exception): + if hasattr(g, "db"): + g.db.close() + +def get_js_files(): + js_folder = os.path.join(app.static_folder, "js") + js_files = [f for f in os.listdir(js_folder) if f.endswith(".js")] + return js_files + def read_csv(file_path): data = [] with open(file_path, 'r', newline='', encoding='utf-8') as csvfile: