From db7de872c4caf8d67784ad257551598a3a0e6035 Mon Sep 17 00:00:00 2001 From: Michaƫl Ball Date: Fri, 27 Nov 2015 19:33:01 +0000 Subject: DB marshalling --- mach2.py | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/mach2.py b/mach2.py index f631a79..1b3f068 100644 --- a/mach2.py +++ b/mach2.py @@ -1,18 +1,54 @@ import configparser import json import mimetypes +import sqlite3 + +from flask import Flask, Response, current_app, g, redirect, render_template +from flask import request, url_for +from flask.ext.compress import Compress +from flask.ext.login import LoginManager, current_user, login_required +from flask.ext.login import login_user, logout_user -from flask import Flask -from flask import Response from models.album import Album from models.artist import Artist from models.track import Track +DATABASE = "app.db" + app = Flask(__name__) app.config.from_object(__name__) +config = configparser.ConfigParser() +config.read("mach2.ini") + +app.config["DEBUG"] = config["DEFAULT"]["debug"] + + +def get_db(): + db = getattr(g, "_database", None) + if db is None: + db = sqlite3.connect(DATABASE) + db.row_factory = sqlite3.Row + setattr(g, "_database", db) + + return db + + +@app.teardown_appcontext +def close_connection(exception): + db = getattr(g, "_database", None) + if db is not None: + db.close() + + +def query_db(query, args=(), one=False): + cur = get_db().execute(query, args) + rv = cur.fetchall() + cur.close() + return (rv[0] if rv else None) if one else rv + @app.route("/") def hello(): -- cgit v1.2.3