From bdfe1f3750f83143a7b776bf20161d21c8f2fd7d Mon Sep 17 00:00:00 2001 From: Mardone Date: Wed, 18 Dec 2024 11:06:44 -0300 Subject: [PATCH 1/2] add delete links to commerce api --- nexus/intelligences/api/tests/test_views.py | 28 ++++- nexus/intelligences/api/views.py | 129 ++++++++++++++------ nexus/usecases/intelligences/get_by_uuid.py | 2 +- nexus/usecases/intelligences/retrieve.py | 13 ++ 4 files changed, 134 insertions(+), 38 deletions(-) diff --git a/nexus/intelligences/api/tests/test_views.py b/nexus/intelligences/api/tests/test_views.py index fea1b0a4..9d5e6980 100644 --- a/nexus/intelligences/api/tests/test_views.py +++ b/nexus/intelligences/api/tests/test_views.py @@ -572,8 +572,34 @@ def test_list(self): self.assertEqual(response.status_code, 200) response_json = json.loads(response.content) - instructions = response_json[0].get('instructions', []) + + instructions = response_json.get('personalization').get('instructions') self.assertEqual(len(instructions), 2) self.assertEqual(instructions[0]['instruction'], 'Try to use emojis') self.assertEqual(instructions[1]['instruction'], 'Dont change the subject') + + @mock.patch('django.conf.settings.DEFAULT_RETAIL_INSTRUCTIONS', ['Try to use emojis', 'Dont change the subject']) + def test_without_links(self): + + data = { + "agent": { + "name": "test", + "role": "Doubt analyst", + "personality": "Friendly", + "goal": "Answer user questions" + } + } + + request = self.factory.post(self.url, data=data, format='json') + force_authenticate(request, user=self.user) + response = self.view(request, project_uuid=str(self.project.uuid)) + response.render() + + self.assertEqual(response.status_code, 200) + + response_json = json.loads(response.content) + instructions = response_json.get('personalization').get('instructions') + + self.assertEqual(len(instructions), 2) + self.assertEqual(response_json.get('links'), None) diff --git a/nexus/intelligences/api/views.py b/nexus/intelligences/api/views.py index 0a723894..a7a353cc 100644 --- a/nexus/intelligences/api/views.py +++ b/nexus/intelligences/api/views.py @@ -853,6 +853,42 @@ def get(self, request, project_uuid): class RouterRetailViewSet(views.APIView): + def _create_links( + self, + links: list, + user: User, + content_base: ContentBase, + project: Project + ) -> list: + created_links = [] + if links: + for link in links: + + link_serializer = ContentBaseLinkSerializer(data={"link": link}) + link_serializer.is_valid(raise_exception=True) + link_dto = intelligences.ContentBaseLinkDTO( + link=link, + user_email=user.email, + content_base_uuid=str(content_base.uuid) + ) + content_base_link = intelligences.CreateContentBaseLinkUseCase().create_content_base_link(link_dto) + + if project.indexer_database == Project.BEDROCK: + bedrock_send_link.delay( + link=link, + user_email=user.email, + content_base_link_uuid=str(content_base_link.uuid) + ) + else: + send_link.delay( + link=link, + user_email=user.email, + content_base_link_uuid=str(content_base_link.uuid) + ) + + link_serializer = CreatedContentBaseLinkSerializer(content_base_link).data + created_links.append(link_serializer) + # TODO - Refactor this view to have only one searializer and no dependencies def post(self, request, project_uuid): user: User = request.user @@ -871,44 +907,21 @@ def post(self, request, project_uuid): project = ProjectsUseCase().get_project_by_content_base_uuid(content_base.uuid) - for link in links: - - link_serializer = ContentBaseLinkSerializer(data={"link": link}) - link_serializer.is_valid(raise_exception=True) - link_dto = intelligences.ContentBaseLinkDTO( - link=link, - user_email=user.email, - content_base_uuid=str(content_base.uuid) - ) - content_base_link = intelligences.CreateContentBaseLinkUseCase().create_content_base_link(link_dto) - - if project.indexer_database == Project.BEDROCK: - bedrock_send_link.delay( - link=link, - user_email=user.email, - content_base_link_uuid=str(content_base_link.uuid) - ) - else: - send_link.delay( - link=link, - user_email=user.email, - content_base_link_uuid=str(content_base_link.uuid) - ) - - link_serializer = CreatedContentBaseLinkSerializer(content_base_link).data + created_links = self._create_links(links, user, content_base, project) # ContentBasePersonalization agent_data = request.data.get("agent") - default_instructions: list = settings.DEFAULT_RETAIL_INSTRUCTIONS instructions_objects = [] - for instruction in default_instructions: - instructions_objects.append( - { - "instruction": instruction, - } - ) + if not content_base.instructions.exists(): + default_instructions: list = settings.DEFAULT_RETAIL_INSTRUCTIONS + for instruction in default_instructions: + instructions_objects.append( + { + "instruction": instruction, + } + ) agent = { "agent": agent_data, @@ -929,13 +942,57 @@ def post(self, request, project_uuid): if personalization_serializer.is_valid(): personalization_serializer.save() - response = [ - personalization_serializer.data, - link_serializer - ] + response = { + "personalization": personalization_serializer.data, + "links": created_links + } return Response(response, status=200) + def destroy(self, request, project_uuid): + + user: User = request.user + module_permission = user.has_perm("users.can_communicate_internally") + + if not module_permission: + return Response(status=status.HTTP_401_UNAUTHORIZED) + + use_case = intelligences.RetrieveContentBaseUseCase() + content_base = use_case.get_default_by_project( + project_uuid, + user.email, + is_superuser=module_permission + ) + content_base_uuid: str = content_base.uuid + + use_case = intelligences.RetrieveContentBaseLinkUseCase() + existing_links = use_case.get_content_base_link_by_link( + content_base_uuid=content_base_uuid, + link=request.data.get("link") + ) + + for link in existing_links: + project_use_case = ProjectsUseCase() + project = project_use_case.get_project_by_content_base_uuid(content_base_uuid) + indexer = project_use_case.get_indexer_database_by_project(project) + + use_case = intelligences.DeleteContentBaseLinkUseCase(indexer) + use_case.delete_by_object( + link, + ) + + if project.indexer_database == Project.BEDROCK: + start_ingestion_job.delay("", post_delete=True) + + event_manager.notify( + event="contentbase_link_activity", + action_type="D", + content_base_link=link, + user=user + ) + + return Response(status=status.HTTP_204_NO_CONTENT) + class LLMViewset(views.APIView): permission_classes = [IsAuthenticated] diff --git a/nexus/usecases/intelligences/get_by_uuid.py b/nexus/usecases/intelligences/get_by_uuid.py index 91160563..c2998c22 100644 --- a/nexus/usecases/intelligences/get_by_uuid.py +++ b/nexus/usecases/intelligences/get_by_uuid.py @@ -59,7 +59,7 @@ def get_by_content_base_file_uuid(content_base_uuid: str) -> ContentBaseFile: raise (f"[ ContentBaseFile ] - ContentBaseFile error to get - error: `{exception}`") -def get_by_content_base_link_uuid(content_base_uuid: str) -> ContentBaseFile: +def get_by_content_base_link_uuid(content_base_uuid: str) -> ContentBaseLink: try: return ContentBaseLink.objects.get(uuid=content_base_uuid) except ContentBaseLink.DoesNotExist: diff --git a/nexus/usecases/intelligences/retrieve.py b/nexus/usecases/intelligences/retrieve.py index f4bc7f5d..178ed089 100644 --- a/nexus/usecases/intelligences/retrieve.py +++ b/nexus/usecases/intelligences/retrieve.py @@ -17,6 +17,7 @@ ContentBaseLinkDoesNotExist, ) from nexus.projects.permissions import has_project_permission +from nexus.intelligences.models import ContentBaseLink class RetrieveIntelligenceUseCase(): @@ -123,6 +124,18 @@ def get_contentbaselink(self, contentbaselink_uuid: str, user_email: str): raise IntelligencePermissionDenied() return get_by_content_base_link_uuid(contentbaselink_uuid) + def get_content_base_link_by_link(self, link: str, content_base_uuid: str): + + links = ContentBaseLink.objects.filter( + link=link, + content_base__uuid=content_base_uuid + ) + + if not links.exists(): + raise ContentBaseLink.DoesNotExist() + + return links + def get_file_info(file_uuid: str) -> Dict: try: From 10cb4a60329288c2ce2da0f5868e7ba4449be808 Mon Sep 17 00:00:00 2001 From: Mardone Date: Wed, 18 Dec 2024 11:12:32 -0300 Subject: [PATCH 2/2] Fix missing method statement --- nexus/intelligences/api/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nexus/intelligences/api/views.py b/nexus/intelligences/api/views.py index a7a353cc..41ec1ea1 100644 --- a/nexus/intelligences/api/views.py +++ b/nexus/intelligences/api/views.py @@ -949,7 +949,7 @@ def post(self, request, project_uuid): return Response(response, status=200) - def destroy(self, request, project_uuid): + def delete(self, request, project_uuid): user: User = request.user module_permission = user.has_perm("users.can_communicate_internally")