2022-02-16 18:07:05 +01:00
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
|
|
def is_valid_email(email: str) -> bool:
|
|
|
|
"""
|
|
|
|
Return if email format is valid
|
|
|
|
"""
|
2022-03-23 18:29:49 +01:00
|
|
|
if not email:
|
|
|
|
return False
|
2022-02-16 18:07:05 +01:00
|
|
|
mail_pattern = r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
|
|
|
|
return re.match(mail_pattern, email) is not None
|
|
|
|
|
|
|
|
|
2022-03-13 08:39:50 +01:00
|
|
|
def check_password(password: str) -> str:
|
2022-02-16 18:07:05 +01:00
|
|
|
"""
|
2022-03-13 08:39:50 +01:00
|
|
|
Verify if password have more than 8 characters
|
|
|
|
If not, it returns error message
|
2022-02-16 18:07:05 +01:00
|
|
|
"""
|
|
|
|
if len(password) < 8:
|
2022-03-13 08:39:50 +01:00
|
|
|
return 'password: 8 characters required\n'
|
|
|
|
return ''
|
2022-02-16 18:07:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
def check_username(username: str) -> str:
|
|
|
|
"""
|
|
|
|
Return if username is valid
|
2022-03-13 08:39:50 +01:00
|
|
|
If not, it returns error messages
|
2022-02-16 18:07:05 +01:00
|
|
|
"""
|
|
|
|
ret = ''
|
2022-03-13 08:38:45 +01:00
|
|
|
if not (2 < len(username) < 31):
|
|
|
|
ret += 'username: 3 to 30 characters required\n'
|
2022-02-16 18:07:05 +01:00
|
|
|
if not re.match(r'^[a-zA-Z0-9_]+$', username):
|
|
|
|
ret += (
|
|
|
|
'username: only alphanumeric characters and the '
|
|
|
|
'underscore character "_" allowed\n'
|
|
|
|
)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2022-03-13 08:39:50 +01:00
|
|
|
def register_controls(username: str, email: str, password: str) -> str:
|
2022-02-16 18:07:05 +01:00
|
|
|
"""
|
|
|
|
Verify if username, email and passwords are valid
|
2022-03-13 08:39:50 +01:00
|
|
|
If not, it returns error messages
|
2022-02-16 18:07:05 +01:00
|
|
|
"""
|
|
|
|
ret = check_username(username)
|
|
|
|
if not is_valid_email(email):
|
|
|
|
ret += 'email: valid email must be provided\n'
|
2022-03-13 08:39:50 +01:00
|
|
|
ret += check_password(password)
|
2022-02-16 18:07:05 +01:00
|
|
|
return ret
|