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"> <div class="unit one-of-three">
A certification process for OpenStack service and product vendors. A certification process for OpenStack service and product vendors.
</div> </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"> <div class="unit span-grid">
For detailed specification, see <a href="https://etherpad.openstack.org/RefStackBlueprint">https://etherpad.openstack.org/RefStackBlueprint</a> For detailed specification, see <a href="https://etherpad.openstack.org/RefStackBlueprint">https://etherpad.openstack.org/RefStackBlueprint</a>
</div> </div>

View File

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