Repository: LAION-AI/Open-Assistant
File: backend/oasst_backend/user_repository.py (Python)

Code before:
            user.tos_acceptance_date = utcnow()

        self.db.add(user)
        return user

    @managed_tx_method(CommitMode.COMMIT)
    def mark_user_deleted(self, id: UUID) -> None:
        """
        Update a user by global user ID to set deleted flag. Only trusted clients may delete users.
        User deletion anonymises the data of the user.

        Raises:
            OasstError: 403 if untrusted client attempts to delete a user. 404 if user with ID not found.
        """
        if not self.api_client.trusted:
            raise OasstError("Forbidden", OasstErrorCode.API_CLIENT_NOT_AUTHORIZED, HTTP_403_FORBIDDEN)

        user: User = self.db.query(User).filter(User.id == id).first()

        if user is None:
            raise OasstError("User not found", OasstErrorCode.USER_NOT_FOUND, HTTP_404_NOT_FOUND)

        user.deleted = True

        # Anonymise user data
        user.display_name = "Deleted User"
        # Ensure uniqueness of (username, auth_method, api_client_id) Index
        user.username = f"deleted_{user.id}"
        user.show_on_leaderboard = False

        self.db.add(user)

    @managed_tx_method(CommitMode.COMMIT)
    def _lookup_user_tx(
        self,
        *,
        username: str,
        auth_method: str,
        display_name: Optional[str] = None,
        create_missing: bool = True,
    ) -> User | None:
        user: User = (
            self.db.query(User)
            .filter(
                User.api_client_id == self.api_client.id,
                User.username == username,
                User.auth_method == auth_method,
            )
            .first()
        )
        if user is None:

Review comment (suggestion):
I see the intention is it to keep this info coherent for inference and backend user data.
like it is used in inference/server/oasst_inference_server/routes/admin.py as well.

Can this be defined in oasst-shared/utils.py.  ?
example
` DELETED_USER = "Deleted User" `
`user.display_name = utils.DELETED_USER` 
Similar changes can be thought for the hardcoded prefix "deleted_"
` user.username = f"deleted_{user.id}` 


Code after:
        if tos_acceptance:
            user.tos_acceptance_date = utcnow()

        self.db.add(user)
        return user

    @managed_tx_method(CommitMode.COMMIT)
    def mark_user_deleted(self, id: UUID) -> None:
        """
        Update a user by global user ID to set deleted flag. Only trusted clients may delete users.
        User deletion anonymises the data of the user.

        Raises:
            OasstError: 403 if untrusted client attempts to delete a user. 404 if user with ID not found.
        """
        if not self.api_client.trusted:
            raise OasstError("Forbidden", OasstErrorCode.API_CLIENT_NOT_AUTHORIZED, HTTP_403_FORBIDDEN)

        user: User = self.db.query(User).filter(User.id == id).first()

        if user is None:
            raise OasstError("User not found", OasstErrorCode.USER_NOT_FOUND, HTTP_404_NOT_FOUND)

        user.deleted = True

        # Anonymise user data
        user.display_name = shared_utils.DELETED_USER_DISPLAY_NAME
        # Ensure uniqueness of (username, auth_method, api_client_id) Index
        user.username = f"{shared_utils.DELETED_USER_ID_PREFIX}{user.id}"
        user.show_on_leaderboard = False

        self.db.add(user)

    @managed_tx_method(CommitMode.COMMIT)
    def _lookup_user_tx(
        self,
        *,
        username: str,
        auth_method: str,
        display_name: Optional[str] = None,
        create_missing: bool = True,
    ) -> User | None:
        user: User = (
            self.db.query(User)
            .filter(
                User.api_client_id == self.api_client.id,
                User.username == username,
                User.auth_method == auth_method,
            )
            .first()
        )