From f525f570da6f3e782e817c66cd7cac7997abd89a Mon Sep 17 00:00:00 2001 From: kovacspe Date: Sat, 11 Nov 2023 21:51:15 +0100 Subject: [PATCH] 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',