-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #903 from weni-ai/feature/nlp-log-export
Feature/nlp log export
- Loading branch information
Showing
8 changed files
with
173 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from django.http import HttpResponse | ||
|
||
from openpyxl import Workbook | ||
from openpyxl.writer.excel import save_virtual_workbook | ||
|
||
|
||
class ExportRepositoryLogUseCase: | ||
|
||
def _create_xlsx_workbook( | ||
self, | ||
repository_logs | ||
) -> Workbook: | ||
|
||
wb = Workbook() | ||
ws = wb.active | ||
|
||
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.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 | ||
|
||
def create_xlsx_response( | ||
self, | ||
repository_logs | ||
) -> HttpResponse: | ||
|
||
wb = self._create_xlsx_workbook(repository_logs) | ||
response = HttpResponse( | ||
content=save_virtual_workbook(wb), | ||
content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", | ||
) | ||
response['Content-Disposition'] = 'attachment; filename=repository_logs.xlsx' | ||
|
||
return response |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' | ||
) |