From 71064a20c17589ffdf6c5eca4b08b2589552f0c9 Mon Sep 17 00:00:00 2001 From: xen Date: Fri, 10 Jan 2014 01:49:44 +0200 Subject: [PATCH 1/3] Update to follow current state in documentations --- social/apps/flask_app/models.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/social/apps/flask_app/models.py b/social/apps/flask_app/models.py index cca20e303..a89d3d1f3 100644 --- a/social/apps/flask_app/models.py +++ b/social/apps/flask_app/models.py @@ -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'),) @@ -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'),) @@ -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'),) @@ -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) From 1de998b9d2111ace5176416585f0eefcf8309fc5 Mon Sep 17 00:00:00 2001 From: xen Date: Fri, 10 Jan 2014 01:51:38 +0200 Subject: [PATCH 2/3] Simplify SQLAlchemy API usage --- examples/flask_example/__init__.py | 22 ++++------------------ examples/flask_example/manage.py | 8 ++++---- examples/flask_example/models/user.py | 18 ++++++++---------- examples/flask_example/requirements.txt | 17 +++++++---------- examples/flask_example/settings.py | 4 +++- 5 files changed, 26 insertions(+), 43 deletions(-) diff --git a/examples/flask_example/__init__.py b/examples/flask_example/__init__.py index 6b2060b43..db82c077f 100644 --- a/examples/flask_example/__init__.py +++ b/examples/flask_example/__init__.py @@ -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 @@ -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 @@ -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 diff --git a/examples/flask_example/manage.py b/examples/flask_example/manage.py index 36c05fabb..9953c5aa6 100755 --- a/examples/flask_example/manage.py +++ b/examples/flask_example/manage.py @@ -5,15 +5,14 @@ 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 })) @@ -21,7 +20,8 @@ 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() diff --git a/examples/flask_example/models/user.py b/examples/flask_example/models/user.py index 0ca1e920a..32727f6c1 100644 --- a/examples/flask_example/models/user.py +++ b/examples/flask_example/models/user.py @@ -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 diff --git a/examples/flask_example/requirements.txt b/examples/flask_example/requirements.txt index 5810037aa..97de88fe2 100644 --- a/examples/flask_example/requirements.txt +++ b/examples/flask_example/requirements.txt @@ -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 \ No newline at end of file diff --git a/examples/flask_example/settings.py b/examples/flask_example/settings.py index 8fd301f93..8d3bad995 100644 --- a/examples/flask_example/settings.py +++ b/examples/flask_example/settings.py @@ -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' From f9321e3d66968bedbb4dcbe855d6e67f2edac400 Mon Sep 17 00:00:00 2001 From: xen Date: Fri, 10 Jan 2014 01:55:29 +0200 Subject: [PATCH 3/3] Cleanup docs --- docs/configuration/flask.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/configuration/flask.rst b/docs/configuration/flask.rst index eca098e3b..c47e92ade 100644 --- a/docs/configuration/flask.rst +++ b/docs/configuration/flask.rst @@ -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