From ac09c9c8d80b61a70181c379e157b3c4d9854965 Mon Sep 17 00:00:00 2001 From: nukeop Date: Mon, 2 Jan 2017 02:18:00 +0100 Subject: [PATCH] Partially migrated to SQLAlchemy --- requirements.txt | 1 + smash/__init__.py | 15 ++++++++++++--- smash/models_sqlalchemy.py | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 smash/models_sqlalchemy.py diff --git a/requirements.txt b/requirements.txt index 43b710f..4d906af 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ flask mock psycopg2 +flask-sqlalchemy diff --git a/smash/__init__.py b/smash/__init__.py index 457fe18..2a1774e 100644 --- a/smash/__init__.py +++ b/smash/__init__.py @@ -1,12 +1,16 @@ import os from flask import Flask -from . import config, database_postgresql, log, models +from flask.ext.sqlalchemy import SQLAlchemy +from . import config, database_sqlalchemy, log, models log.configure_logging() app = Flask(__name__) conf = config.Config('config.json') +# Load database URL for SQLAlchemy from environment +app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DATABASE_URL'] + # This flag tells the program it's deployed on heroku if 'HEROKU' in os.environ: conf.add(('HEROKU', 1)) @@ -36,8 +40,13 @@ if 'SECRETKEY' in conf.config: else: exit("Secret key not set.") -db = database_postgresql.DatabasePostgreSQL() -models.init_models(db) +db = SQLAlchemy(app) + +from smash.models_sqlalchemy import * + +db.create_all() + +#models.init_models(db) from . import views diff --git a/smash/models_sqlalchemy.py b/smash/models_sqlalchemy.py new file mode 100644 index 0000000..3305a54 --- /dev/null +++ b/smash/models_sqlalchemy.py @@ -0,0 +1,38 @@ +from smash import db + + +class Quote(db.Model): + __tablename__ = 'quotes' + + id = db.Column(db.Integer, primary_key=True) + rating = db.Column(db.Integer) + content = db.Column(db.String()) + approved = db.Column(db.Boolean) + author_ip = db.Column(db.String()) + time = db.Column(db.String()) + + + def __init__(self, content, author_ip, time): + self.rating = 0 + self.content = content + self.approved = False + self.author_ip = author_ip + self.time = time + + +class Tag(db.Model): + __tablename__ = 'tags' + + id = db.Column(db.Integer, primary_key=True) + name = db.Column(db.String()) + + + def __init(self, name): + self.name = name + + +tags_to_quotes = db.Table( + 'tagsToQuotes', + db.Column('tagid', db.Integer, db.ForeignKey('tags.id')), + db.Column('quoteid', db.Integer, db.ForeignKey('quotes.id')) +)