Replace db.Model with DeclarativeBase from SQLAlchemy for Flask-SQLAlchemy-Lite migration
This commit is contained in:
@@ -29,7 +29,7 @@ from flask import Flask
|
||||
from flask.testing import FlaskCliRunner
|
||||
from sqlalchemy.sql.ddl import DropTable
|
||||
|
||||
from test_site import db
|
||||
from test_site import db, Base
|
||||
from testlib import create_test_app
|
||||
|
||||
|
||||
@@ -63,7 +63,7 @@ class ConsoleCommandTestCase(unittest.TestCase):
|
||||
# Drop every accounting table, to see if accounting-init-db
|
||||
# recreates them correctly.
|
||||
tables: list[sa.Table] \
|
||||
= [db.metadata.tables[x] for x in db.metadata.tables
|
||||
= [Base.metadata.tables[x] for x in Base.metadata.tables
|
||||
if x.startswith("accounting_")]
|
||||
for table in tables:
|
||||
db.session.execute(DropTable(table))
|
||||
@@ -207,7 +207,7 @@ class ConsoleCommandTestCase(unittest.TestCase):
|
||||
with self.__app.app_context():
|
||||
# Resets the accounts.
|
||||
tables: list[sa.Table] \
|
||||
= [db.metadata.tables[x] for x in db.metadata.tables
|
||||
= [Base.metadata.tables[x] for x in Base.metadata.tables
|
||||
if x.startswith("accounting_")]
|
||||
for table in tables:
|
||||
db.session.execute(DropTable(table))
|
||||
|
||||
@@ -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