Replace db.Model with DeclarativeBase from SQLAlchemy for Flask-SQLAlchemy-Lite migration
This commit is contained in:
@@ -28,6 +28,7 @@ from flask.testing import FlaskCliRunner
|
||||
from flask_babel_js import BabelJS
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from flask_wtf import CSRFProtect
|
||||
from sqlalchemy.orm import DeclarativeBase
|
||||
|
||||
bp: Blueprint = Blueprint("home", __name__)
|
||||
"""The global blueprint."""
|
||||
@@ -39,6 +40,10 @@ db: SQLAlchemy = SQLAlchemy()
|
||||
"""The database instance."""
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
"""The base class for all models."""
|
||||
|
||||
|
||||
def create_app(is_testing: bool = False, is_skip_accounts: bool = False,
|
||||
is_skip_currencies: bool = False) -> Flask:
|
||||
"""Create and configure the application.
|
||||
@@ -99,6 +104,10 @@ def create_app(is_testing: bool = False, is_skip_accounts: bool = False,
|
||||
from accounting.utils.next_uri import append_next
|
||||
return redirect(append_next(url_for("auth.login-form")))
|
||||
|
||||
@property
|
||||
def base(self) -> type[DeclarativeBase]:
|
||||
return Base
|
||||
|
||||
@property
|
||||
def cls(self) -> type[auth.User]:
|
||||
return auth.User
|
||||
@@ -137,7 +146,7 @@ def init_db(app: Flask, is_skip_accounts: bool,
|
||||
otherwise.
|
||||
:return: None.
|
||||
"""
|
||||
db.create_all()
|
||||
Base.metadata.create_all(db.engine)
|
||||
from .auth import User
|
||||
for username in ["viewer", "editor", "admin", "nobody"]:
|
||||
user: User | None = db.session.scalar(
|
||||
|
||||
@@ -24,13 +24,13 @@ from flask import Blueprint, render_template, Flask, redirect, url_for, \
|
||||
session, request, g, Response, abort
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from . import db
|
||||
from . import db, Base
|
||||
|
||||
bp: Blueprint = Blueprint("auth", __name__, url_prefix="/")
|
||||
"""The authentication blueprint."""
|
||||
|
||||
|
||||
class User(db.Model):
|
||||
class User(Base):
|
||||
"""A user."""
|
||||
__tablename__ = "users"
|
||||
"""The table name."""
|
||||
|
||||
Reference in New Issue
Block a user