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

1"""

2 Both py4web and web2py can share this Auth User table definition.

3"""

4

5 import datetime as dt

6

7 from pydal.validators import CRYPT, IS_EMAIL, IS_NOT_EMPTY, IS_NOT_IN_DB, IS_STRONG

8

9 from .core import TypeDAL, TypedField, TypedTable

10 from .fields import PasswordField

11

12

13 class AuthUser(TypedTable):

14 """

15 Class for db.auth_user in py4web and web2py.

16 """

17

18 # call db.define with redefine=True and migrate=False on this when ready

19

20 email = TypedField(str)

21 password = PasswordField(requires=[IS_STRONG(entropy=45), CRYPT()])

22 first_name = TypedField(str, requires=IS_NOT_EMPTY())

23 last_name = TypedField(str, requires=IS_NOT_EMPTY())

24 sso_id = TypedField(str)

25 action_token = TypedField(str)

26 last_password_change = TypedField(dt.datetime, default=dt.datetime.now)

27 registration_key = TypedField(str)

28 reset_password_key = TypedField(str)

29 registration_id = TypedField(str)

30

31 @classmethod

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

33 """

34 When we have access to 'db', set the IS_NOT_IN_DB requirement.

35 """

36 cls.email.requires = (

37 IS_EMAIL(),

38 IS_NOT_IN_DB(

39 db,

40 "auth_user.email",

41 ),

42 )