Skip to content

Commit

Permalink
fix don't init flask-sqlalchemy if it's already inited
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgaspar committed Jun 5, 2024
1 parent b49bd34 commit fba2a39
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 23 deletions.
10 changes: 3 additions & 7 deletions examples/base_filters/app/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import datetime

from flask_sqlalchemy.model import NameMixin
from flask_appbuilder import Model
from flask_appbuilder.models.mixins import AuditMixin
from sqlalchemy import Column, Date, ForeignKey, Integer, String
Expand All @@ -9,26 +8,23 @@
mindate = datetime.date(datetime.MINYEAR, 1, 1)


class ContactGroup(NameMixin, Model):
class ContactGroup(Model):
id = Column(Integer, primary_key=True)
name = Column(String(50), unique=True, nullable=False)

def __repr__(self):
return self.name


class Gender(NameMixin, Model):
class Gender(Model):
id = Column(Integer, primary_key=True)
name = Column(String(50), unique=True, nullable=False)

def __repr__(self):
return self.name


from flask_appbuilder.security.sqla.models import User


class Contact(AuditMixin, NameMixin, Model):
class Contact(AuditMixin, Model):
id = Column(Integer, primary_key=True)
name = Column(String(150), unique=True, nullable=False)
address = Column(String(564))
Expand Down
2 changes: 1 addition & 1 deletion flask_appbuilder/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__author__ = "Daniel Vaz Gaspar"
__version__ = "4.5.0"
__version__ = "5.0.0rc1"

from .actions import action # noqa: F401
from .api import ModelRestApi # noqa: F401
Expand Down
5 changes: 3 additions & 2 deletions flask_appbuilder/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,9 @@ def init_app(self, app: Flask) -> None:
app.config.setdefault("FAB_STATIC_URL_PATH", self.static_url_path)

self._init_extension(app)
# init flask-sqlalchemy
db.init_app(app)
# init flask-sqlalchemy if needed
if "sqlalchemy" not in app.extensions:
db.init_app(app)

self.base_template = app.config.get("FAB_BASE_TEMPLATE", self.base_template)
self.static_folder = app.config.get("FAB_STATIC_FOLDER", self.static_folder)
Expand Down
8 changes: 6 additions & 2 deletions flask_appbuilder/models/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ def created_by_fk(cls):

@declared_attr
def created_by(cls):
from flask_appbuilder.security.sqla.models import User

return relationship(
"User",
User,
primaryjoin="%s.created_by_fk == User.id" % cls.__name__,
enable_typechecks=False,
)
Expand All @@ -80,8 +82,10 @@ def changed_by_fk(cls):

@declared_attr
def changed_by(cls):
from flask_appbuilder.security.sqla.models import User

return relationship(
"User",
User,
primaryjoin="%s.changed_by_fk == User.id" % cls.__name__,
enable_typechecks=False,
)
Expand Down
16 changes: 6 additions & 10 deletions flask_appbuilder/security/sqla/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,18 @@ def register_views(self) -> None:
def create_db(self) -> None:
if not current_app.config.get("FAB_CREATE_DB", True):
return
try:
# Check if an application context does not exist
if not has_app_context():
# Create a new application context
with self.appbuilder.app.app_context():
self._create_db()
else:
if not has_app_context():
# Create a new application context
with self.appbuilder.app.app_context():
self._create_db()
except Exception as e:
log.error(c.LOGMSG_ERR_SEC_CREATE_DB, e)
exit(1)
else:
self._create_db()

def _create_db(self) -> None:
from flask_appbuilder.extensions import db

inspector = inspector = inspect(db.engine)
inspector = inspect(db.engine)
if "ab_user" not in inspector.get_table_names():
log.info(c.LOGMSG_INF_SEC_NO_DB)
db.create_all()
Expand Down
2 changes: 1 addition & 1 deletion flask_appbuilder/security/sqla/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class Role(Model):
secondary=assoc_permissionview_role,
backref="role",
)
users: Mapped[List["User"]] = relationship(
user: Mapped[List["User"]] = relationship(
"User", secondary=assoc_user_role, backref="roles", enable_typechecks=False
)

Expand Down

0 comments on commit fba2a39

Please sign in to comment.