From afec48d4291c80e932b34d8e0b38e60bae626525 Mon Sep 17 00:00:00 2001 From: Bernardus Rendy <37468936+bernardusrendy@users.noreply.github.com> Date: Tue, 18 Jun 2024 11:44:18 -0700 Subject: [PATCH] added request_user_input_with_note --- alab_management/user_input.py | 49 +++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/alab_management/user_input.py b/alab_management/user_input.py index b1bc4453..67e0df60 100644 --- a/alab_management/user_input.py +++ b/alab_management/user_input.py @@ -141,6 +141,28 @@ def get_all_pending_requests(self) -> list: self._input_collection.find({"status": UserRequestStatus.PENDING.value}), ) + def retrieve_user_input_with_note(self, request_id: ObjectId) -> tuple[str, str]: + """ + Retrive response from user for a given request. Blocks until request is marked as completed. + + Returns the user response, which is one of a list of options + """ + status = UserRequestStatus.PENDING + try: + while status == UserRequestStatus.PENDING: + request = self._input_collection.find_one({"_id": request_id}) + if request is None: + raise ValueError( + f"User input request id {request_id} does not exist!" + ) + status = UserRequestStatus(request["status"]) + time.sleep(0.5) + except: # noqa: E722 + self._input_collection.update_one( + {"_id": request_id}, {"$set": {"status": UserRequestStatus.ERROR.name}} + ) + raise + return request["response"], request["note"] def request_user_input( task_id: ObjectId | None, @@ -185,3 +207,30 @@ def request_maintenance_input(prompt: str, options: list[str]): maintenance=True, category="Maintenance", ) + +def request_user_input_with_note( + task_id: ObjectId | None, + prompt: str, + options: list[str], + maintenance: bool = False, + category: str = "Unknown Category", +) -> tuple[str, str]: + """ + Request user input through the dashboard. Blocks until response is given. + + task_id (ObjectId): task id requesting user input + prompt (str): prompt to give user + options (List[str]): response options to give user + maintenance (bool): if true, mark this as a request for overall system maintenance + + Returns user response as string. + """ + user_input_view = UserInputView() + request_id = user_input_view.insert_request( + task_id=task_id, + prompt=prompt, + options=options, + maintenance=maintenance, + category=category, + ) + return user_input_view.retrieve_user_input_with_note(request_id=request_id)