database.py 文件源码

python
阅读 120 收藏 0 点赞 0 评论 0

项目:arc 作者: lap00zza 项目源码 文件源码
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`
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号