diff --git a/bothub/api/v2/translation/views.py b/bothub/api/v2/translation/views.py index d51f38bd..8dd15f06 100644 --- a/bothub/api/v2/translation/views.py +++ b/bothub/api/v2/translation/views.py @@ -181,7 +181,7 @@ def retrieve(self, request, *args, **kwargs): # pragma: no cover end=entity.end + count_entity, ) count_entity += len(entity.entity.value) + 4 - + translated = RepositoryTranslatedExample.objects.filter( original_example=example.pk, language=for_the_language ) diff --git a/bothub/common/usecase/repositorylog/export.py b/bothub/common/usecase/repositorylog/export.py index 67190bed..f778fdb6 100644 --- a/bothub/common/usecase/repositorylog/export.py +++ b/bothub/common/usecase/repositorylog/export.py @@ -1,6 +1,7 @@ from django.http import HttpResponse from openpyxl import Workbook +from openpyxl.writer.excel import save_virtual_workbook class ExportRepositoryLogUseCase: @@ -13,17 +14,23 @@ def _create_xlsx_workbook( wb = Workbook() ws = wb.active - ws['A1'] = 'Repository' - ws['B1'] = 'Date' - ws['C1'] = 'Action' - ws['D1'] = 'User' + ws['A1'] = 'Text' + ws['B1'] = 'Created At' + ws['C1'] = 'Intent' + ws['D1'] = 'Confidence' + ws['E1'] = 'Entities' + ws['F1'] = 'Entities List' + + if repository_logs is None: + return wb row = 2 for repository_log in repository_logs: - ws['A{}'.format(row)] = repository_log.repository.name - ws['B{}'.format(row)] = repository_log.date - ws['C{}'.format(row)] = repository_log.action - ws['D{}'.format(row)] = repository_log.user.username + ws['A{}'.format(row)] = repository_log.nlp_log.text + ws['B{}'.format(row)] = repository_log.created_at + ws['C{}'.format(row)] = repository_log.nlp_log.intent.name + ws['D{}'.format(row)] = repository_log.nlp_log.intent.confidence + ws['F{}'.format(row)] = repository_log.nlp_log.entities row += 1 return wb @@ -34,8 +41,10 @@ def create_xlsx_response( ) -> HttpResponse: wb = self._create_xlsx_workbook(repository_logs) - response = HttpResponse(content_type='application/vnd.ms-excel') + response = HttpResponse( + content=save_virtual_workbook(wb), + content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + ) response['Content-Disposition'] = 'attachment; filename=repository_logs.xlsx' - wb.save(response) return response diff --git a/bothub/common/usecase/repositorylog/tests/__init__.py b/bothub/common/usecase/repositorylog/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/bothub/common/usecase/repositorylog/tests/test_export.py b/bothub/common/usecase/repositorylog/tests/test_export.py new file mode 100644 index 00000000..ea6082fd --- /dev/null +++ b/bothub/common/usecase/repositorylog/tests/test_export.py @@ -0,0 +1,60 @@ +import unittest +from unittest.mock import Mock +from django.http import HttpResponse +from openpyxl import Workbook +from ..export import ExportRepositoryLogUseCase + + +class TestExportRepositoryLogUseCase(unittest.TestCase): + + def setUp(self): + self.use_case = ExportRepositoryLogUseCase() + self.mock_repository_logs = [ + Mock( + nlp_log=Mock( + text='text', + intent=Mock( + name='intent', + confidence='confidence' + ), + entities='entities' + ), created_at='created_at' + ) + ] + for log in self.mock_repository_logs: + log.nlp_log.intent.name = 'intent_name' + + def test_create_xlsx_workbook(self): + # Chama a função _create_xlsx_workbook + result = self.use_case._create_xlsx_workbook(self.mock_repository_logs) + self.assertIsInstance(result, Workbook) + + def test_create_xlsx_response(self): + result = self.use_case.create_xlsx_response(self.mock_repository_logs) + self.assertIsInstance(result, HttpResponse) + self.assertEqual( + result['Content-Type'], + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' + ) + self.assertEqual( + result['Content-Disposition'], + 'attachment; filename=repository_logs.xlsx' + ) + + def test_create_xlsx_workbook_with_none(self): + + result = self.use_case._create_xlsx_workbook(None) + self.assertIsInstance(result, Workbook) + + def test_create_xlsx_response_with_none(self): + + result = self.use_case.create_xlsx_response(None) + self.assertIsInstance(result, HttpResponse) + self.assertEqual( + result['Content-Type'], + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' + ) + self.assertEqual( + result['Content-Disposition'], + 'attachment; filename=repository_logs.xlsx' + )