How to prevent flask-security from using db_session.query_property()

I’m trying to setup flask-security using the example at https://flask-security-too.readthedocs.io/en/stable/quickstart.html. I am using SQLAlchemy, not flask-sqlalchemy. I was able to get the example to work, but I’m having problems integrating it in my application. flask-security seems to require Base.query = db_session.query_property(), which in turn, seems to require using scoped-session, which I don’t use (there seem to be some strong opinions against using it)

This seems to be a pretty weird requirement of flask-security, which appears to be undocumented, except in the sample code. I’m just wondering if this will cause a problem with other parts of my application

There also seems to be some inconsistencies with various examples. Some of them attach the security object to the app with

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>app.security = Security(app, user_datastore)
</code>
<code>app.security = Security(app, user_datastore) </code>
app.security = Security(app, user_datastore)

and later uses

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>app.security.datastore.create_user(email="[email protected]"...
</code>
<code>app.security.datastore.create_user(email="[email protected]"... </code>
app.security.datastore.create_user(email="[email protected]"...

whereas in other places, I see

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>security = Security(app, user_datastore)
user_datastore.create_user(email="[email protected]"...
</code>
<code>security = Security(app, user_datastore) user_datastore.create_user(email="[email protected]"... </code>
security = Security(app, user_datastore)
user_datastore.create_user(email="[email protected]"...

I’m trying to get around it by doing something like this

database.py

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session
from root.config import config
def get_engine():
return create_engine(config.get('db').get('DATABASE_URI'), echo=False)
# Use this session for everything other than flask-security
def get_session():
engine = get_engine()
return sessionmaker(bind=engine)()
</code>
<code>from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker, scoped_session from root.config import config def get_engine(): return create_engine(config.get('db').get('DATABASE_URI'), echo=False) # Use this session for everything other than flask-security def get_session(): engine = get_engine() return sessionmaker(bind=engine)() </code>
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session

from root.config import config


def get_engine():
    return create_engine(config.get('db').get('DATABASE_URI'), echo=False)


# Use this session for everything other than flask-security
def get_session():
    engine = get_engine()
    return sessionmaker(bind=engine)()

app.py

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import os
from flask import Flask
from flask_security import SQLAlchemySessionUserDatastore, Security, hash_password
from sqlalchemy.orm import scoped_session, sessionmaker
from root.db.ModelBase import ModelBase
from root.db.database import get_engine
from root.db.models import User, Role
from root.mail import mail_init
from root.views.calibration_views import calibration
nwm_app = Flask(__name__)
nwm_app.config["SECURITY_REGISTERABLE"] = True
# Use this session for flask-security
session = scoped_session(sessionmaker(bind=get_engine()))
# This is global. Is it going to affect other parts of my application if I'm using SQLAlchemy 'select'?
ModelBase.query = session.query_property()
user_datastore = SQLAlchemySessionUserDatastore(session, User, Role)
security = Security(nwm_app, user_datastore)
# Register blueprints or views here
nwm_app.register_blueprint(calibration)
# one time setup
with nwm_app.app_context():
# Create a user and role to test with
# nwm_app.security.datastore.find_or_create_role(
user_datastore.find_or_create_role(
# name="user", permissions={"user-read", "user-write"}
name="user"
)
print('created role')
session.commit()
# if not nwm_app.security.datastore.find_user(email="[email protected]"):
if not user_datastore.find_user(email="[email protected]"):
print('user not found')
# nwm_app.security.datastore.create_user(email="[email protected]",
user_datastore.create_user(email="[email protected]",
password=hash_password("password"), roles=["user"])
session.commit()
if __name__ == '__main__':
nwm_app.run()
</code>
<code>import os from flask import Flask from flask_security import SQLAlchemySessionUserDatastore, Security, hash_password from sqlalchemy.orm import scoped_session, sessionmaker from root.db.ModelBase import ModelBase from root.db.database import get_engine from root.db.models import User, Role from root.mail import mail_init from root.views.calibration_views import calibration nwm_app = Flask(__name__) nwm_app.config["SECURITY_REGISTERABLE"] = True # Use this session for flask-security session = scoped_session(sessionmaker(bind=get_engine())) # This is global. Is it going to affect other parts of my application if I'm using SQLAlchemy 'select'? ModelBase.query = session.query_property() user_datastore = SQLAlchemySessionUserDatastore(session, User, Role) security = Security(nwm_app, user_datastore) # Register blueprints or views here nwm_app.register_blueprint(calibration) # one time setup with nwm_app.app_context(): # Create a user and role to test with # nwm_app.security.datastore.find_or_create_role( user_datastore.find_or_create_role( # name="user", permissions={"user-read", "user-write"} name="user" ) print('created role') session.commit() # if not nwm_app.security.datastore.find_user(email="[email protected]"): if not user_datastore.find_user(email="[email protected]"): print('user not found') # nwm_app.security.datastore.create_user(email="[email protected]", user_datastore.create_user(email="[email protected]", password=hash_password("password"), roles=["user"]) session.commit() if __name__ == '__main__': nwm_app.run() </code>
import os

from flask import Flask
from flask_security import SQLAlchemySessionUserDatastore, Security, hash_password
from sqlalchemy.orm import scoped_session, sessionmaker

from root.db.ModelBase import ModelBase
from root.db.database import get_engine
from root.db.models import User, Role
from root.mail import mail_init
from root.views.calibration_views import calibration

nwm_app = Flask(__name__)

nwm_app.config["SECURITY_REGISTERABLE"] = True

# Use this session for flask-security
session = scoped_session(sessionmaker(bind=get_engine()))

# This is global.  Is it going to affect other parts of my application if I'm using SQLAlchemy 'select'?
ModelBase.query = session.query_property()
user_datastore = SQLAlchemySessionUserDatastore(session, User, Role)
security = Security(nwm_app, user_datastore)

# Register blueprints or views here

nwm_app.register_blueprint(calibration)


# one time setup
with nwm_app.app_context():
    # Create a user and role to test with
    # nwm_app.security.datastore.find_or_create_role(
    user_datastore.find_or_create_role(
        # name="user", permissions={"user-read", "user-write"}
        name="user"
    )
    print('created role')
    session.commit()
    # if not nwm_app.security.datastore.find_user(email="[email protected]"):
    if not user_datastore.find_user(email="[email protected]"):
        print('user not found')
        # nwm_app.security.datastore.create_user(email="[email protected]",
        user_datastore.create_user(email="[email protected]",
                                   password=hash_password("password"), roles=["user"])
    session.commit()

if __name__ == '__main__':
    nwm_app.run()

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật