def make_app(): babel = Babel() security = Security() social = Social() user_datastore = SQLAlchemyUserDatastore(mdl.DB, mdl.User, mdl.Role) app = Flask(__name__) @login_failed.connect_via(app) def auto_add_user(sender, provider, oauth_response): connection_values = get_connection_values_from_oauth_response( provider, oauth_response) email = connection_values['email'] if not email or not email.strip(): email = '' if provider.name.lower() == 'facebook': fname = connection_values['full_name'] email = fb.get_email(oauth_response) elif provider.name.lower() == 'twitter': fname = connection_values['display_name'][1:] # cut off leading @ else: fname = connection_values['display_name'] user = user_datastore.create_user( email=email, name=fname, active=True, confirmed_at=datetime.now(), ) role_query = mdl.DB.session.query(mdl.Role).filter_by( name='authenticated') try: role = role_query.one() except NoResultFound: role = mdl.Role(name='authenticated') user.roles.append(role) user_datastore.commit() connection_values['user_id'] = user.id connect_handler(connection_values, provider) login_user(user) mdl.DB.session.commit() flash(gettext( 'Successfully linked login with {}. ' 'Ignore the message saying: "{}"').format( provider.name, '... account not associated with an existing user'), 'info') return redirect(url_for('root.profile')) app.user_datastore = user_datastore app.localconf = lconf = Config('mamerwiselen', 'lost-tracker', version='2.0', require_load=True) app.config['SECRET_KEY'] = lconf.get('app', 'secret_key') app.config['SQLALCHEMY_DATABASE_URI'] = lconf.get('db', 'dsn') mdl.DB.init_app(app) # Social connections social_connections = {} _add_social_params(social_connections, 'facebook', app.localconf) _add_social_params(social_connections, 'twitter', app.localconf) _add_social_params(social_connections, 'google', app.localconf) if len(social_connections) < 1: LOG.error('No Social/OAuth providers defined! Users will not be ' 'able to log-in!') app.config.update(social_connections) social.init_app(app, SQLAlchemyConnectionDatastore(mdl.DB, mdl.Connection)) security.init_app(app, user_datastore) app.register_blueprint(COMMENT, url_prefix=COMMENT_PREFIX) app.register_blueprint(GROUP, url_prefix=GROUP_PREFIX) app.register_blueprint(PHOTO, url_prefix=PHOTO_PREFIX) app.register_blueprint(QR, url_prefix=QR_PREFIX) app.register_blueprint(REGISTRATION, url_prefix=REGISTRATION_PREFIX) app.register_blueprint(ROOT) app.register_blueprint(STATION, url_prefix=STATION_PREFIX) app.register_blueprint(TABULAR, url_prefix=TABULAR_PREFIX) app.register_blueprint(USER, url_prefix=USER_PREFIX) babel.init_app(app) babel.localeselector(get_locale) return app
app = Flask(__name__) app.config.from_object('app.config.Config') api = Api(app) db = SQLAlchemy(app) es = ES('127.0.0.1:9200') import api_views import views import models security_ds = SQLAlchemyUserDatastore(db, models.User, models.UserRole) social_ds = SQLAlchemyConnectionDatastore(db, models.Connection) app.security = Security(app, security_ds) app.social = Social(app, social_ds) @login_failed.connect_via(app) def on_login_failed(sender, provider, oauth_response): connection_values = get_connection_values_from_oauth_response( provider, oauth_response) ds = app.security.datastore email = connection_values.pop('email', None) user = ds.create_user(email=email) ds.commit() connection_values['user_id'] = user.id connect_handler(connection_values, provider) login_user(user) db.session.commit()