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 super().__on_define__(db)

28

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

30

31

32 class AuthMembership(TypedTable):

33 """

34 Model for w2p_auth_membership.

35 """

36

37 user_id: TypedField[AuthUser]

38 group_id: TypedField[AuthGroup]

39

40

41 class AuthPermission(TypedTable):

42 """

43 Model for w2p_auth_permission.

44 """

45

46 group_id: TypedField[AuthGroup]

47 name: TypedField[str]

48 table_name: TypedField[str]

49 record_id: TypedField[int]

50

51

52 class AuthEvent(TypedTable):

53 """

54 Model for w2p_auth_event.

55 """

56

57 time_stamp: TypedField[dt.datetime | None]

58 client_ip: TypedField[str | None]

59 user_id: TypedField[AuthUser | None]

60 origin: TypedField[str | None]

61 description = TextField(notnull=False)

62

63

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

65 """

66 Setup all the (default) web2py required tables.

67 """

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

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

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

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

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