Skip to content

Commit

Permalink
Merge pull request #909 from weni-ai/feature/store-xlsx-logs-file
Browse files Browse the repository at this point in the history
Add: Temp storage for logs
  • Loading branch information
zMardone authored Feb 16, 2024
2 parents 911d465 + f47b6bd commit 59d534f
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 6 deletions.
17 changes: 17 additions & 0 deletions bothub/common/usecase/repositorylog/dto.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from abc import ABC, abstractmethod
from dataclasses import dataclass


@dataclass
class FileResponseDTO:
status: int = None
file_url: str = None
err: str = None
file_name: str = None


class FileDataBase(ABC):

@abstractmethod
def add_file(file) -> FileResponseDTO:
...
27 changes: 21 additions & 6 deletions bothub/common/usecase/repositorylog/export.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from django.http import HttpResponse
import io

from rest_framework import status
from rest_framework.response import Response

from openpyxl import Workbook
from openpyxl.writer.excel import save_virtual_workbook

from .storage import s3FileDatabase


class ExportRepositoryLogUseCase:

Expand Down Expand Up @@ -42,16 +47,26 @@ def _create_xlsx_workbook(

return wb

def xlsx_storage(
self,
xlsx_file
) -> str:
file_database = s3FileDatabase()
xlsx_file_object = io.BytesIO(xlsx_file)
created_file = file_database.add_file(xlsx_file_object)
temp_url = file_database.create_presigned_url(created_file.file_name)
return temp_url

def create_xlsx_response(
self,
repository_logs
) -> HttpResponse:
) -> Response:

wb = self._create_xlsx_workbook(repository_logs)
response = HttpResponse(
content=save_virtual_workbook(wb),
content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
temp_url = self.xlsx_storage(save_virtual_workbook(wb))
response = Response(
data={"file": temp_url},
status=status.HTTP_200_OK
)
response['Content-Disposition'] = 'attachment; filename=repository_logs.xlsx'

return response
46 changes: 46 additions & 0 deletions bothub/common/usecase/repositorylog/storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import uuid
import boto3
from os.path import basename
from django.conf import settings

from .dto import FileDataBase, FileResponseDTO


class s3FileDatabase(FileDataBase):
def __init__(self) -> None:
self.s3_client = boto3.client(
's3',
aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY,
region_name=settings.AWS_REGION_NAME
)

def add_file(self, file) -> FileResponseDTO:
file_name = basename("export_logs.xlsx")
name, extension = file_name.split(".")
file_name = f"{name}-{uuid.uuid4()}.{extension}"
response = FileResponseDTO()
try:
self.s3_client.upload_fileobj(file, settings.AWS_BUCKET_NAME, file_name)
response.status = 0
response.file_url = f"https://{settings.AWS_BUCKET_NAME}.s3.{settings.AWS_REGION_NAME}.amazonaws.com/{file_name}"
response.file_name = file_name
except Exception as exception:
response.status = 1
response.err = str(exception)
return response

def create_presigned_url(
self,
file_name: str,
expiration: int = 3600
) -> str:

return self.s3_client.generate_presigned_url(
'get_object',
Params={
'Bucket': settings.AWS_BUCKET_NAME,
'Key': file_name
},
ExpiresIn=expiration
)

0 comments on commit 59d534f

Please sign in to comment.