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

16 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-11-20 17:00 +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, Fixture 

10 

11from .core import TypeDAL, TypedTable 

12 

13 

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

15 """ 

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

17 """ 

18 

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

20 """ 

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

22 """ 

23 self.get_connection_from_pool_or_new() 

24 threadsafevariable.ThreadSafeVariable.restore(ICECUBE) 

25 

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

27 """ 

28 Rollback db on error. 

29 """ 

30 self.recycle_connection_in_pool_or_close("rollback") 

31 

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

33 """ 

34 Commit db on success. 

35 """ 

36 self.recycle_connection_in_pool_or_close("commit") 

37 

38 

39class AuthUser(TypedTable): 

40 """ 

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

42 """ 

43 

44 redefine = True 

45 migrate = False 

46 

47 # call db.define on this when ready 

48 

49 email: str 

50 password: str 

51 first_name: Optional[str] 

52 last_name: Optional[str] 

53 sso_id: Optional[str] 

54 action_token: Optional[str] 

55 last_password_change: Optional[datetime] 

56 # past_passwords_hash: Optional[str] 

57 # username: Optional[str] 

58 # phone_number: Optional[str]