示例#1
0
    def create_user(cls, user_data):
        if UserDatabaseClient.get_user_by_username(
                user_data.username) is not None:
            cls.logger().info(
                f"Failing to create user #{user_data.username}. Username already in use."
            )
            return BadRequestUserMessageResponse(
                "Username already in use for other user.",
                UserResponseStatus.ALREADY_REGISTERED.value)
        try:
            new_client = UserDatabaseClient.add_client()
            new_user = User(user_id=new_client.id,
                            username=user_data.username,
                            email=user_data.email,
                            password=hashing.hash(user_data.password),
                            first_name=user_data.first_name,
                            last_name=user_data.last_name,
                            profile_pic=user_data.profile_pic,
                            role=user_data.role or UserRoles.USER.value,
                            token=Authenticator.generate(
                                new_client.id, user_data.password))
            UserDatabaseClient.add_user(new_user)
            DatabaseClient.commit()
            cls.logger().info(f"User #{new_client.id} created.")
            headers = {"auth_token": new_user.token}
            return SuccessfulUserResponse(new_user, headers)

        except IntegrityError as exc:
            DatabaseClient.rollback()
            if UserDatabaseClient.get_user_by_email(
                    user_data.email) is not None:
                cls.logger().info(
                    f"Failing to create user {user_data.username}. Email already in use.",
                    exc)
                return BadRequestUserMessageResponse(
                    "Email already in use for other user.",
                    UserResponseStatus.ALREADY_REGISTERED.value)
            else:
                cls.logger().info(
                    f"Failing to create user #{user_data.username}.")
                return UnsuccessfulClientResponse("Couldn't create user.")
        except:
            DatabaseClient.rollback()
            cls.logger().info(f"Failing to create user #{user_data.username}.")
            return UnsuccessfulClientResponse("Couldn't create user.")
示例#2
0
    def update_user(cls, update_data):
        user = Authenticator.authenticate(update_data)

        user.username = \
            update_data.updated_user["username"] if "username" in update_data.updated_user else user.username
        user.email = \
            update_data.updated_user["email"] if "email" in update_data.updated_user else user.email
        user.password = \
            hashing.hash(
                update_data.updated_user["password"]) if "password" in update_data.updated_user else user.password
        user.first_name = \
            update_data.updated_user["first_name"] if "first_name" in update_data.updated_user else user.first_name
        user.last_name = \
            update_data.updated_user["last_name"] if "last_name" in update_data.updated_user else user.last_name
        user.profile_pic = \
            update_data.updated_user["profile_pic"] if "profile_pic" in update_data.updated_user else user.profile_pic

        try:
            UserDatabaseClient.update_user(user)
            DatabaseClient.commit()
            cls.logger().info(f"User {user.id} information updated.")
            return SuccessfulUserResponse(user)
        except IntegrityError:
            DatabaseClient.rollback()
            new_username = update_data.updated_user.get("username")
            new_email = update_data.updated_user.get("email")

            if UserDatabaseClient.get_user_by_username(
                    new_username) is not None:
                cls.logger().info(
                    f"Name {new_email} is taken for another user.")
                return BadRequestUserMessageResponse(
                    f"Name {new_username} is already in use!",
                    UserResponseStatus.ALREADY_REGISTERED.value)
            elif UserDatabaseClient.get_user_by_email(new_email) is not None:
                cls.logger().info(
                    f"Email {new_email} is taken for another user.")
                return BadRequestUserMessageResponse(
                    f"Email {new_email} is already in use!",
                    UserResponseStatus.ALREADY_REGISTERED.value)
            else:
                cls.logger().error(
                    f"Couldn't update user {user.id} information.")
                return UnsuccessfulClientResponse(
                    "Couldn't update user information!")