Skip to content

Commit

Permalink
refactor: change get to post method for download file view, create se…
Browse files Browse the repository at this point in the history
…rializer for file name, removed spaces in the name of the created archive
  • Loading branch information
dev-lymar committed Jun 22, 2024
1 parent 780d066 commit 7ebdff0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
4 changes: 4 additions & 0 deletions makedoc/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ class DataDocSerializer(serializers.Serializer[Any]):
factory_id = serializers.IntegerField()
destination = serializers.CharField()
delivery_cost = serializers.IntegerField(min_value=0)


class FileNameSerializer(serializers.Serializer[Any]):
file_name = serializers.CharField(required=False, allow_blank=True)
5 changes: 3 additions & 2 deletions makedoc/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
get_formatted_date_agreement,
get_formatted_date_shipment,
)

from .data_service import DataService


Expand Down Expand Up @@ -194,8 +193,10 @@ def add_workbook_to_archive_xlsx(self, wb, user, client):
tempdir = os.path.join("makedoc", "tempdoc", str(user.id))
os.makedirs(tempdir, exist_ok=True)

client_name = client.client_name.replace(' ', '_')
date_today = datetime.today().strftime('%d.%m.%Y_%H:%M:%S')
self.archive_name = (
f"{client.client_name} {datetime.today().strftime('%d.%m.%Y %H:%M:%S')}.zip"
f"{client_name}_{date_today}.zip"
)
archive_path = f"{tempdir}/{self.archive_name}"

Expand Down
4 changes: 2 additions & 2 deletions makedoc/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from django.urls import path

from .views import DataDocView, CreateDocsView, DownloadDocView
from .views import DataDocView, CreateDocsView, DownloadDocAPIView

urlpatterns = [
path("filemake/", CreateDocsView.as_view(), name="file"),
path("downloadfile/", DownloadDocView.as_view(), name="downloadfile"),
path("downloadfile/", DownloadDocAPIView.as_view(), name="downloadfile"),
path("data/", DataDocView.as_view(), name="data"),
]
24 changes: 15 additions & 9 deletions makedoc/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from rest_framework.views import APIView

from makedoc.services import Documents
from .serializers import DataDocSerializer, DocumentsSimpleSerializer
from .serializers import DataDocSerializer, DocumentsSimpleSerializer, FileNameSerializer


class CreateDocsView(APIView):
Expand Down Expand Up @@ -49,15 +49,24 @@ def get(self, request, *args, **kwargs):
return Response({"message": "Documents saved"}, status=status.HTTP_200_OK)


class DownloadDocView(APIView):
class DownloadDocAPIView(APIView):
permission_classes = [IsAuthenticated]

def get(self, request, *args, **kwargs):
cache_key = f"last_created_file_user:{request.user.id}"
file_name = cache.get(cache_key)
def post(self, request, *args, **kwargs):
serializer = FileNameSerializer(data=request.data)
if not serializer.is_valid():
return Response({"error": "Incorrect file name"}, status=status.HTTP_404_NOT_FOUND)

file_name = serializer.validated_data.get("file_name")

if not file_name:
return Response({"error": "No file found"}, status=status.HTTP_404_NOT_FOUND)
cache_key = f"last_created_file_user:{request.user.id}"
file_name = cache.get(cache_key)

if not file_name:
return Response({"error": "No file found"}, status=status.HTTP_404_NOT_FOUND)

cache.delete(cache_key)

file_path = os.path.join("makedoc", "tempdoc", str(request.user.id), file_name)

Expand All @@ -66,9 +75,6 @@ def get(self, request, *args, **kwargs):

response = FileResponse(open(file_path, "rb"))
response["Content-Disposition"] = f'attachment; filename="{os.path.basename(file_name)}"'

cache.delete(cache_key)

response["message"] = "File successfully downloaded"
response.status_code = status.HTTP_200_OK

Expand Down

0 comments on commit 7ebdff0

Please sign in to comment.