Skip to content

Commit

Permalink
Merge pull request #150 from xen/master
Browse files Browse the repository at this point in the history
Update Flask integration to most recent version
  • Loading branch information
omab committed Jan 10, 2014
2 parents f1653d1 + f9321e3 commit cc0bee3
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 50 deletions.
2 changes: 1 addition & 1 deletion docs/configuration/flask.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ and the database are defined, call ``init_social`` to register the models::

from social.apps.flask_app.models import init_social

social_storage = init_social(app, db)
init_social(app, db)

So far I wasn't able to find another way to define the models on another way
rather than making it as a side-effect of calling this function since the
Expand Down
22 changes: 4 additions & 18 deletions examples/flask_example/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import sys

from sqlalchemy import create_engine

from flask import Flask, g
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext import login

from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base


sys.path.append('../..')

from social.apps.flask_app.routes import social_auth
Expand All @@ -27,21 +21,13 @@

# DB
db = SQLAlchemy(app)
db.metadata.bind = create_engine(app.config['SQLALCHEMY_DATABASE_URI'])

engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'],
convert_unicode=True)
session = scoped_session(sessionmaker(bind=engine))
Base = declarative_base()
Base.query = session.query_property()

app.register_blueprint(social_auth)
social_storage = init_social(app, Base, session)
init_social(app, db)

login_manager = login.LoginManager()
login_manager.login_view = 'main'
login_manager.login_message = ''
login_manager.setup_app(app)
login_manager.init_app(app)

from flask_example import models
from flask_example import routes
Expand All @@ -63,12 +49,12 @@ def global_user():
@app.teardown_appcontext
def commit_on_success(error=None):
if error is None:
session.commit()
db.session.commit()


@app.teardown_request
def shutdown_session(exception=None):
session.remove()
db.session.remove()


@app.context_processor
Expand Down
8 changes: 4 additions & 4 deletions examples/flask_example/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@

sys.path.append('..')

from flask_example import app, db, models, Base, engine
from flask_example import app, db


manager = Manager(app)
manager.add_command('runserver', Server())
manager.add_command('shell', Shell(make_context=lambda: {
'app': app,
'db': db,
'models': models
'db': db
}))


@manager.command
def syncdb():
from flask_example.models import user
from social.apps.flask_app import models
Base.metadata.create_all(bind=engine)
db.drop_all()
db.create_all()

if __name__ == '__main__':
manager.run()
18 changes: 8 additions & 10 deletions examples/flask_example/models/user.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
from sqlalchemy import Column, Integer, String, Boolean

from flask.ext.login import UserMixin

from flask_example import Base
from flask_example import db


class User(Base, UserMixin):
class User(db.Model, UserMixin):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
username = Column(String(200))
password = Column(String(200), default='')
name = Column(String(100))
email = Column(String(200))
active = Column(Boolean, default=True)
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(200))
password = db.Column(db.String(200), default='')
name = db.Column(db.String(100))
email = db.Column(db.String(200))
active = db.Column(db.Boolean, default=True)

def is_active(self):
return self.active
17 changes: 7 additions & 10 deletions examples/flask_example/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
Flask==0.10.1
Flask-SQLAlchemy==0.16
Flask-Login==0.1.3
Flask-Evolution==0.6
Flask-Script==0.3.3
Jinja2==2.6
SQLAlchemy==0.7.8
Werkzeug==0.8.3
wsgiref==0.1.2
pysqlite==2.6.3
Flask
Flask-SQLAlchemy
Flask-Login
Flask-Script
Werkzeug
pysqlite
Jinja2
4 changes: 3 additions & 1 deletion examples/flask_example/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
SECRET_KEY = 'random-secret-key'
SESSION_COOKIE_NAME = 'psa_session'
DEBUG = False
SQLALCHEMY_DATABASE_URI = 'sqlite:///test.db'
from os.path import dirname, abspath
SQLALCHEMY_DATABASE_URI = 'sqlite:////%s/test.sqlite' % dirname(abspath(__file__))

DEBUG_TB_INTERCEPT_REDIRECTS = False
SESSION_PROTECTION = 'strong'

Expand Down
12 changes: 6 additions & 6 deletions social/apps/flask_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ class FlaskStorage(BaseSQLAlchemyStorage):
code = None


def init_social(app, Base, session):
def init_social(app, db):
UID_LENGTH = app.config.get(setting_name('UID_LENGTH'), 255)
User = module_member(app.config[setting_name('USER_MODEL')])
app_session = session
app_session = db.session

class _AppSession(object):
@classmethod
def _session(cls):
return app_session

class UserSocialAuth(_AppSession, Base, SQLAlchemyUserMixin):
class UserSocialAuth(_AppSession, db.Model, SQLAlchemyUserMixin):
"""Social Auth association model"""
__tablename__ = 'social_auth_usersocialauth'
__table_args__ = (UniqueConstraint('provider', 'uid'),)
Expand All @@ -50,7 +50,7 @@ def username_max_length(cls):
def user_model(cls):
return User

class Nonce(_AppSession, Base, SQLAlchemyNonceMixin):
class Nonce(_AppSession, db.Model, SQLAlchemyNonceMixin):
"""One use numbers"""
__tablename__ = 'social_auth_nonce'
__table_args__ = (UniqueConstraint('server_url', 'timestamp', 'salt'),)
Expand All @@ -59,7 +59,7 @@ class Nonce(_AppSession, Base, SQLAlchemyNonceMixin):
timestamp = Column(Integer)
salt = Column(String(40))

class Association(_AppSession, Base, SQLAlchemyAssociationMixin):
class Association(_AppSession, db.Model, SQLAlchemyAssociationMixin):
"""OpenId account association"""
__tablename__ = 'social_auth_association'
__table_args__ = (UniqueConstraint('server_url', 'handle'),)
Expand All @@ -71,7 +71,7 @@ class Association(_AppSession, Base, SQLAlchemyAssociationMixin):
lifetime = Column(Integer)
assoc_type = Column(String(64))

class Code(_AppSession, Base, SQLAlchemyCodeMixin):
class Code(_AppSession, db.Model, SQLAlchemyCodeMixin):
__tablename__ = 'social_auth_code'
__table_args__ = (UniqueConstraint('code', 'email'),)
id = Column(Integer, primary_key=True)
Expand Down

0 comments on commit cc0bee3

Please sign in to comment.