Adding sqlalchemy bindings and models.

This commit is contained in:
Joshua McKenty 2013-04-17 13:25:52 -07:00
parent 4898b79378
commit b3a450d86b
3 changed files with 45 additions and 26 deletions

View File

@ -20,6 +20,14 @@
<div class="unit one-of-three">
A certification process for OpenStack service and product vendors.
</div>
<div class="unit span-grid">
Current vendors registered with RefStack are:<br/>
<ul>
{% for vendor in vendors %}
<li><a href="mailto:{{ vendor.contact_email }}">{{ vendor.vendor_name }}</a></li>
{% endfor %}
</ul>
</div>
<div class="unit span-grid">
For detailed specification, see <a href="https://etherpad.openstack.org/RefStackBlueprint">https://etherpad.openstack.org/RefStackBlueprint</a>
</div>

View File

@ -8,41 +8,51 @@ import random
import sqlite3
import sys
from flask import Flask, request, render_template, g, jsonify
from flask.ext.sqlalchemy import SQLAlchemy
from contextlib import closing
# TODO(JMC): Make me a config var
# TODO(JMC): Use real schema migrations(!)
DATABASE = '/var/www/refstack/database.db'
app = Flask(__name__)
app.debug = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////var/www/refstack/database.db'
db = SQLAlchemy(app)
def connect_db():
return sqlite3.connect(DATABASE)
class Vendor(db.Model):
id = db.Column(db.Integer, primary_key=True)
vendor_name = db.Column(db.String(80), unique=True)
contact_email = db.Column(db.String(120), unique=True)
@app.before_request
def before_request():
g.db = connect_db()
def __init__(self, username, email):
self.vendor_name = vendor_name
self.contact_email = contact_email
@app.teardown_request
def teardown_request(exception):
if hasattr(g, 'db'):
g.db.close()
def __repr__(self):
return '<Vendor %r>' % self.vendor_name
def query_db(query, args=(), one=False):
cur = g.db.execute(query, args)
rv = [dict((cur.description[idx][0], value)
for idx, value in enumerate(row)) for row in cur.fetchall()]
return (rv[0] if rv else None) if one else rv
def init_db():
with closing(connect_db()) as db:
with app.open_resource('schema.sql') as f:
db.cursor().executescript(f.read())
db.commit()
#
# @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 query_db(query, args=(), one=False):
# cur = g.db.execute(query, args)
# rv = [dict((cur.description[idx][0], value)
# for idx, value in enumerate(row)) for row in cur.fetchall()]
# return (rv[0] if rv else None) if one else rv
#
# def init_db():
# with closing(connect_db()) as db:
# with app.open_resource('schema.sql') as f:
# db.cursor().executescript(f.read())
# db.commit()
@app.route('/', methods=['POST','GET'])
def index():
return render_template('index.html')
vendors = Vendor.query.all()
return render_template('index.html', vendors = vendors)

View File

@ -1,3 +1,4 @@
requests
twisted
flask
Flask-SQLAlchemy