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

18 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-11-21 18:27 +0100

1""" 

2ONLY USE IN COMBINATION WITH PY4WEB! 

3""" 

4from datetime import datetime 

5from typing import Any, Optional 

6 

7import json_fix # noqa: F401 

8import threadsafevariable 

9from py4web.core import ICECUBE 

10from py4web.core import Fixture as _Fixture 

11 

12from .core import TypeDAL, TypedTable 

13 

14 

15class Fixture(_Fixture): # type: ignore 

16 """ 

17 Make mypy happy. 

18 """ 

19 

20 

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

22 """ 

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

24 """ 

25 

26 def on_request(self, _: dict[str, Any]) -> None: 

27 """ 

28 Make sure there is a database connection when a request comes in. 

29 """ 

30 self.get_connection_from_pool_or_new() 

31 threadsafevariable.ThreadSafeVariable.restore(ICECUBE) 

32 

33 def on_error(self, _: dict[str, Any]) -> None: 

34 """ 

35 Rollback db on error. 

36 """ 

37 self.recycle_connection_in_pool_or_close("rollback") 

38 

39 def on_success(self, _: dict[str, Any]) -> None: 

40 """ 

41 Commit db on success. 

42 """ 

43 self.recycle_connection_in_pool_or_close("commit") 

44 

45 

46class AuthUser(TypedTable): 

47 """ 

48 Class for db.auth_user in py4web (probably not w2p). 

49 """ 

50 

51 redefine = True 

52 migrate = False 

53 

54 # call db.define on this when ready 

55 

56 email: str 

57 password: str 

58 first_name: Optional[str] 

59 last_name: Optional[str] 

60 sso_id: Optional[str] 

61 action_token: Optional[str] 

62 last_password_change: Optional[datetime] 

63 # past_passwords_hash: Optional[str] 

64 # username: Optional[str] 

65 # phone_number: Optional[str]