LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
fix/Prompt Studio cascade delete (#362)
Athul09079308508f
diff --git a/backend/file_management/file_management_helper.py b/backend/file_management/file_management_helper.pyindex 0004176b2..0c13812b4 100644--- a/backend/file_management/file_management_helper.py+++ b/backend/file_management/file_management_helper.py@@ -181,28 +181,50 @@ class FileManagerHelper:else:raise InvalidFileType+ @staticmethod+ def _delete_file(fs, file_path):+ try:+ fs.rm(file_path)+ except FileNotFoundError:+ FileManagerHelper.logger.info(f"File not found: {file_path}")+ except Exception as e:+ FileManagerHelper.logger.info(f"Unable to delete file {e}")+ raise FileDeletionFailed(f"Unable to delete file {e}")++ @staticmethod+ def _get_base_path(file_system: UnstractFileSystem, path: str):+ fs = file_system.get_fsspec_fs()+ base_path = getattr(+ file_system if hasattr(file_system, "path") else fs, "path", path+ )+ base_path = base_path.rstrip("/") + "/"+ return base_path+@staticmethoddef delete_file(file_system: UnstractFileSystem, path: str, file_name: str) -> bool:fs = file_system.get_fsspec_fs()+ base_path = FileManagerHelper._get_base_path(file_system, path)+ file_path = str(Path(base_path) / file_name)+ FileManagerHelper._delete_file(fs, file_path)+ return True- file_path = f"{path}"- try:- if file_system.path and (not path or path == "/"):- file_path = f"{file_system.path}/"- except AttributeError:- if fs.path and (not path or path == "/"):- file_path = f"{fs.path}/"+ @staticmethod+ def delete_related_files(+ file_system: UnstractFileSystem,+ path: str,+ file_name: str,+ directories: list[str],+ ) -> bool:+ fs = file_system.get_fsspec_fs()+ base_path = FileManagerHelper._get_base_path(file_system, path)- file_path = file_path + "/" if not file_path.endswith("/") else file_path+ base_file_name, _ = os.path.splitext(file_name)+ file_name_txt = base_file_name + ".txt"- # adding filename with path- file_path += file_name- try:- with fs.open(file_path, mode="wb") as remote_file:- return remote_file.fs.delete(remote_file.path) # type:ignore- except Exception as e:- FileManagerHelper.logger.info(f"Unable to delete file {e}")- raise FileDeletionFailed(f"Unable to delete file {e}")+ for directory in directories:+ file_path = str(Path(base_path) / directory / file_name_txt)+ FileManagerHelper._delete_file(fs, file_path)+ return True@staticmethoddef handle_sub_directory_for_tenants(diff --git a/backend/prompt_studio/prompt_studio_core/models.py b/backend/prompt_studio/prompt_studio_core/models.pyindex e9bbff42d..ca49db6d8 100644--- a/backend/prompt_studio/prompt_studio_core/models.py+++ b/backend/prompt_studio/prompt_studio_core/models.py@@ -1,3 +1,5 @@+import logging+import shutilimport uuidfrom typing import Any@@ -5,9 +7,12 @@ from account.models import Userfrom adapter_processor.models import AdapterInstancefrom django.db import modelsfrom django.db.models import QuerySet+from file_management.file_management_helper import FileManagerHelperfrom prompt_studio.prompt_studio_core.constants import DefaultPromptsfrom utils.models.base_model import BaseModel+logger = logging.getLogger(__name__)+class CustomToolModelManager(models.Manager):def get_queryset(self) -> QuerySet[Any]:@@ -120,3 +125,21 @@ class CustomTool(BaseModel):shared_users = models.ManyToManyField(User, related_name="shared_custom_tool")objects = CustomToolModelManager()++ def delete(self, organization_id=None, *args, **kwargs):+ # Delete the documents associated with the tool+ file_path = FileManagerHelper.handle_sub_directory_for_tenants(+ organization_id,+ is_create=False,+ user_id=self.created_by.user_id,+ tool_id=str(self.tool_id),+ )+ if organization_id:+ try:+ shutil.rmtree(file_path)+ except FileNotFoundError:+ logger.error(f"The folder {file_path} does not exist.")+ except OSError as e:+ logger.error(f"Error: {file_path} : {e.strerror}")+ # Continue with the deletion of the tool+ super().delete(*args, **kwargs)diff --git a/backend/prompt_studio/prompt_studio_core/views.py b/backend/prompt_studio/prompt_studio_core/views.pyindex af38e2584..1b0f2b19c 100644--- a/backend/prompt_studio/prompt_studio_core/views.py+++ b/backend/prompt_studio/prompt_studio_core/views.py@@ -90,6 +90,10 @@ class PromptStudioCoreView(viewsets.ModelViewSet):)return Response(serializer.data, status=status.HTTP_201_CREATED)+ def perform_destroy(self, instance: CustomTool) -> None:+ organization_id = UserSessionUtils.get_organization_id(self.request)+ instance.delete(organization_id)+def destroy(self, request: Request, *args: tuple[Any], **kwargs: dict[str, Any]) -> Response:@@ -452,9 +456,13 @@ class PromptStudioCoreView(viewsets.ModelViewSet):try:# Delete the document recorddocument.delete()-- # Delete the file+ # Delete the filesFileManagerHelper.delete_file(file_system, path, file_name)+ # Directories to delete the text files+ directories = ["extract/", "summarize/"]+ FileManagerHelper.delete_related_files(+ file_system, path, file_name, directories+ )return Response({"data": "File deleted succesfully."},status=status.HTTP_200_OK,