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

18 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-01-29 16:15 +0100

1""" 

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

3""" 

4 

5import datetime as dt 

6 

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

8 

9from .core import TypeDAL, TypedField, TypedTable 

10from .fields import PasswordField 

11 

12 

13class 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 first_name = TypedField(str, requires=IS_NOT_EMPTY()) 

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

22 email = TypedField(str) 

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

24 sso_id = TypedField(str, readable=False, writable=False, notnull=False) 

25 action_token = TypedField(str, readable=False, writable=False, notnull=False) 

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

27 registration_key = TypedField(str, readable=False, writable=False, notnull=False) 

28 reset_password_key = TypedField(str, readable=False, writable=False, notnull=False) 

29 registration_id = TypedField(str, readable=False, writable=False, notnull=False) 

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 )