def create_user(self, username: str, email: str, password: str):
"""
Create a new user. This method is called during the registration process.
Raises
------
LengthError
Raised when password length is less than 8 or greater than 72 characters.
Why 72? because bcrypt only works properly till 72.
"""
if len(password) < 8 or len(password) > 72:
raise LengthError("password", "Password length should be between 8 and 72 characters.")
if not self._is_valid_email(email):
raise ValidationError("Please enter a valid email-id.")
with self.conn:
with self.conn.cursor() as cur:
try:
cur.execute(
"""
INSERT INTO users (user_id, user_name, user_email, user_password_hash, user_timestamp,
user_avatar)
VALUES (%s, %s, %s, %s, %s, %s)
""",
(snowflake.generate(), username, email, self._hash_password(password), datetime.utcnow(),
self._hash_email(email))
)
return True
# For now, this happens when the unique email constraint
# is violated.
except psycopg2.IntegrityError:
print("This email_id already exists. Sorry bruh!")
return False
# TODO: verify_user is probably better off in `auth.py`
评论列表
文章目录