Coverage for src/typedal/for_py4web.py: 100%
10 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-19 16:51 +0100
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-19 16:51 +0100
1"""
2ONLY USE IN COMBINATION WITH PY4WEB!
3"""
4from typing import Any
6import threadsafevariable
7from py4web.core import ICECUBE
8from py4web.core import Fixture as _Fixture
10from .core import TypeDAL
11from .web2py_py4web_shared import AuthUser
14class Fixture(_Fixture): # type: ignore
15 """
16 Make mypy happy.
17 """
20class DAL(TypeDAL, Fixture): # pragma: no cover
21 """
22 Fixture similar to the py4web pydal fixture, but for typedal.
23 """
25 def on_request(self, _: dict[str, Any]) -> None:
26 """
27 Make sure there is a database connection when a request comes in.
28 """
29 self.get_connection_from_pool_or_new()
30 threadsafevariable.ThreadSafeVariable.restore(ICECUBE)
32 def on_error(self, _: dict[str, Any]) -> None:
33 """
34 Rollback db on error.
35 """
36 self.recycle_connection_in_pool_or_close("rollback")
38 def on_success(self, _: dict[str, Any]) -> None:
39 """
40 Commit db on success.
41 """
42 self.recycle_connection_in_pool_or_close("commit")
45def setup_py4web_tables(db: TypeDAL) -> None:
46 """
47 Setup all the (default) required auth table.
48 """
49 db.define(AuthUser, migrate=False, redefine=True)
52__all__ = [
53 "AuthUser",
54 "Fixture",
55 "DAL",
56 "setup_py4web_tables",
57]