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

1"""

2 ONLY USE IN COMBINATION WITH WEB2PY!

3"""

4

5 import datetime as dt

6

7 from pydal.validators import IS_NOT_IN_DB

8

9 from .core import TypeDAL, TypedField, TypedTable

10 from .fields import TextField

11 from .web2py_py4web_shared import AuthUser

12

13

14 class AuthGroup(TypedTable):

15 """

16 Model for w2p_auth_group.

17 """

18

19 role: TypedField[str | None]

20 description = TextField(notnull=False)

21

22 @classmethod

23 def __on_define__(cls, db: TypeDAL) -> None:

24 """

25 When we have access to 'db', set the NOT IN DB requirement to make the role unique.

26 """

27 cls.role.requires = IS_NOT_IN_DB(db, "w2p_auth_group.role")

28

29

30 class AuthMembership(TypedTable):

31 """

32 Model for w2p_auth_membership.

33 """

34

35 user_id: TypedField[AuthUser]

36 group_id: TypedField[AuthGroup]

37

38

39 class AuthPermission(TypedTable):

40 """

41 Model for w2p_auth_permission.

42 """

43

44 group_id: TypedField[AuthGroup]

45 name: TypedField[str]

46 table_name: TypedField[str]

47 record_id: TypedField[int]

48

49

50 class AuthEvent(TypedTable):

51 """

52 Model for w2p_auth_event.

53 """

54

55 time_stamp: TypedField[dt.datetime | None]

56 client_ip: TypedField[str | None]

57 user_id: TypedField[AuthUser | None]

58 origin: TypedField[str | None]

59 description = TextField(notnull=False)

60

61

62 def setup_web2py_tables(db: TypeDAL, migrate: bool = False) -> None:

63 """

64 Setup all the (default) web2py required tables.

65 """

66 db.define(AuthUser, redefine=True, migrate=migrate)

67 db.define(AuthGroup, rname="w2p_auth_group", redefine=True, migrate=migrate)

68 db.define(AuthMembership, rname="w2p_auth_membership", redefine=True, migrate=migrate)

69 db.define(AuthPermission, rname="w2p_auth_permission", redefine=True, migrate=migrate)

70 db.define(AuthEvent, rname="w2p_auth_event", redefine=True, migrate=migrate)