Coverage for src/typedal/for_py4web.py: 100%

1"""

2 ONLY USE IN COMBINATION WITH PY4WEB!

3"""

4 from typing import Any

5

6 import threadsafevariable

7 from py4web.core import ICECUBE

8 from py4web.core import Fixture as _Fixture

9

10 from .core import TypeDAL

11 from .web2py_py4web_shared import AuthUser

12

13

14 class Fixture(_Fixture): # type: ignore

15 """

16 Make mypy happy.

17 """

18

19

20 class DAL(TypeDAL, Fixture): # pragma: no cover

21 """

22 Fixture similar to the py4web pydal fixture, but for typedal.

23 """

24

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)

31

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")

37

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")

43

44

45 def setup_py4web_tables(db: TypeDAL) -> None:

46 """

47 Setup all the (default) required auth table.

48 """

49 db.define(AuthUser, migrate=False, redefine=True)

50

51

52 __all__ = [

53 "AuthUser",

54 "Fixture",

55 "DAL",

56 "setup_py4web_tables",

57]