-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathextension.py
More file actions
48 lines (35 loc) · 1.31 KB
/
extension.py
File metadata and controls
48 lines (35 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import typing as t
from quart import Quart
from .. import signals
from ..config import SQLAlchemyConfig
from ..sqla import SQLAlchemy
from .cli import db_cli
class QuartSQLAlchemy(SQLAlchemy):
def __init__(
self,
config: t.Optional[SQLAlchemyConfig] = None,
app: t.Optional[Quart] = None,
):
initialize = config is not None
super().__init__(config, initialize=initialize)
if app is not None:
self.init_app(app)
def init_app(self, app: Quart) -> None:
if "sqlalchemy" in app.extensions:
raise RuntimeError(
f"A {type(self).__name__} instance has already been registered on this app"
)
if self.config is None:
self.config = SQLAlchemyConfig.from_framework(app.config)
self.initialize()
signals.before_framework_extension_initialization.send(self, app=app)
app.extensions["sqlalchemy"] = self
@app.shell_context_processor
def export_sqlalchemy_objects():
nonlocal self
return dict(
db=self,
**{m.class_.__name__: m.class_ for m in self.Base.registry.mappers},
)
app.cli.add_command(db_cli)
signals.before_framework_extension_initialization.send(self, app=app)