From f525f570da6f3e782e817c66cd7cac7997abd89a Mon Sep 17 00:00:00 2001 From: kovacspe Date: Sat, 11 Nov 2023 21:51:15 +0100 Subject: [PATCH 1/3] Change attachment to file response --- competition/views.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/competition/views.py b/competition/views.py index 9420ba66..6bbb1ce0 100644 --- a/competition/views.py +++ b/competition/views.py @@ -290,7 +290,8 @@ def my_solution(self, request, pk=None): raise exceptions.NotFound( detail='Zatiaľ nebolo nahraté žiadne riešenie') return FileResponse( - file, content_type='application/pdf') + file, content_type='application/pdf', + ) @action(detail=True, url_path='corrected-solution') def corrected_solution(self, request, pk=None): @@ -492,19 +493,25 @@ def remove_vote(self, request, pk=None): def download_solution(self, request, pk=None): """Stiahne riešenie""" solution = self.get_object() - response = HttpResponse( - solution.solution, content_type='application/pdf') - response['Content-Disposition'] = f'attachment; filename="{solution.solution}"' - return response + file = solution.solution + if not file: + raise exceptions.NotFound( + detail='Zatiaľ nebolo nahraté žiadne riešenie') + return FileResponse( + file, content_type='application/pdf', + ) @action(methods=['get'], detail=True, url_path='download-corrected') def download_corrected(self, request, pk=None): """Stiahne opravenú verziu riešenia""" solution = self.get_object() - response = HttpResponse( - solution.solution, content_type='application/pdf') - response['Content-Disposition'] = f'attachment; filename="{solution.corrected_solution}"' - return response + file = solution.corrected_solution + if not file: + raise exceptions.NotFound( + detail='Zatiaľ nebolo nahraté žiadne riešenie') + return FileResponse( + file, content_type='application/pdf', + ) @action(methods=['post'], detail=True, url_path='upload-solution-file', From 9a53d58cf8979845f5933489c194245bac8fbbda Mon Sep 17 00:00:00 2001 From: kovacspe Date: Sat, 11 Nov 2023 21:52:31 +0100 Subject: [PATCH 2/3] format --- competition/views.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/competition/views.py b/competition/views.py index 6bbb1ce0..a5aa2371 100644 --- a/competition/views.py +++ b/competition/views.py @@ -290,8 +290,7 @@ def my_solution(self, request, pk=None): raise exceptions.NotFound( detail='Zatiaľ nebolo nahraté žiadne riešenie') return FileResponse( - file, content_type='application/pdf', - ) + file, content_type='application/pdf') @action(detail=True, url_path='corrected-solution') def corrected_solution(self, request, pk=None): From e4275d241ce3855e519d490945326d19441a9743 Mon Sep 17 00:00:00 2001 From: kovacspe Date: Sat, 11 Nov 2023 21:59:23 +0100 Subject: [PATCH 3/3] Rename paths --- competition/views.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/competition/views.py b/competition/views.py index a5aa2371..67faa286 100644 --- a/competition/views.py +++ b/competition/views.py @@ -488,8 +488,8 @@ def remove_vote(self, request, pk=None): self.get_object().set_vote(Vote.NONE) return Response('Hlas Odobraný.', status=status.HTTP_200_OK) - @action(methods=['get'], detail=True, url_path='download-solution') - def download_solution(self, request, pk=None): + @action(methods=['get'], detail=True, url_path='file-solution') + def file_solution(self, request, pk=None): """Stiahne riešenie""" solution = self.get_object() file = solution.solution @@ -500,8 +500,8 @@ def download_solution(self, request, pk=None): file, content_type='application/pdf', ) - @action(methods=['get'], detail=True, url_path='download-corrected') - def download_corrected(self, request, pk=None): + @action(methods=['get'], detail=True, url_path='file-corrected') + def file_corrected(self, request, pk=None): """Stiahne opravenú verziu riešenia""" solution = self.get_object() file = solution.corrected_solution