From aa6cb5932cad883c20f3d41e5e4093a0dfb84a55 Mon Sep 17 00:00:00 2001 From: Jarret Raim Date: Sat, 16 Feb 2013 19:10:11 -0600 Subject: [PATCH] Adding SQLAlchemy persistence Basic structure for SQLAlchemy and models. Uses a hardcoded DB location which should be moved to a standard configuration. --- .gitignore | 4 ++++ barbican.py | 21 +++++++++++++++++++-- barbican_api.py | 2 +- schema.sql | 0 templates/layout.html | 10 ++++++++++ 5 files changed, 34 insertions(+), 3 deletions(-) delete mode 100644 schema.sql diff --git a/.gitignore b/.gitignore index 61ec8e44c..92d99a673 100644 --- a/.gitignore +++ b/.gitignore @@ -36,3 +36,7 @@ nosetests.xml # Pycharm .idea + +# Sqlite databases +*.sqlite3 +*.db diff --git a/barbican.py b/barbican.py index 80fad8b9c..d540c21c1 100644 --- a/barbican.py +++ b/barbican.py @@ -12,12 +12,16 @@ :copyright: (c) 2013 by Jarret Raim :license: Apache 2.0, see LICENSE for details """ - -from flask import Flask +import os +from flask import Flask, render_template from barbican_api import api +from database import db_session, init_db +from models import User + app = Flask(__name__) app.register_blueprint(api) +app.config['DEBUG'] = True @app.route("/") @@ -25,5 +29,18 @@ def hello(): return "Hello world!" +@app.route('/users') +def users_list(): + users = User.query.all() + return render_template('users.html', users=users) + + +@app.teardown_request +def shutdown_session(exception=None): + db_session.remove() + + if __name__ == '__main__': + if not os.path.exists('/tmp/barbican.db'): + init_db() app.run() \ No newline at end of file diff --git a/barbican_api.py b/barbican_api.py index 121b500ab..862529935 100644 --- a/barbican_api.py +++ b/barbican_api.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """ Barbican API - ~~~~~~ + ~~~~~~~~~~~~ The API for Barbican. diff --git a/schema.sql b/schema.sql deleted file mode 100644 index e69de29bb..000000000 diff --git a/templates/layout.html b/templates/layout.html index e69de29bb..b27451fcd 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -0,0 +1,10 @@ + + + + + +
+ {% block body %}{% endblock %} +
+ + \ No newline at end of file