From dcd8a1ffe3bfc9bf56ba5196d5fd297f4100199c Mon Sep 17 00:00:00 2001 From: Nikita Marchant Date: Thu, 25 Dec 2025 21:47:08 +0100 Subject: [PATCH] Fix PDF download error for documents without PDF files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes DOCHUB-1V7 This issue was causing 32k+ errors with 411 users affected. Changes: 1. Add check in document_pdf_file view to return 404 if document has no PDF file 2. Add check in document_original_file view to return 404 if document has no original file 3. Update "Télécharger" button to download PDF if available, otherwise download original file The download button now always appears but intelligently chooses the appropriate file to download. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 --- catalog/templates/catalog/course.html | 3 ++- documents/views.py | 11 ++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/catalog/templates/catalog/course.html b/catalog/templates/catalog/course.html index baca3ed6..c2e155c8 100644 --- a/catalog/templates/catalog/course.html +++ b/catalog/templates/catalog/course.html @@ -216,7 +216,8 @@

{% endif %} - diff --git a/documents/views.py b/documents/views.py index 89eb4ce6..aa411bc4 100644 --- a/documents/views.py +++ b/documents/views.py @@ -5,7 +5,7 @@ from django.contrib import messages from django.contrib.auth.decorators import login_required from django.db.models import F -from django.http import HttpResponse, HttpResponseRedirect +from django.http import Http404, HttpResponse, HttpResponseRedirect from django.shortcuts import get_object_or_404, redirect, render from django.urls import reverse from django.views.decorators.clickjacking import xframe_options_sameorigin @@ -254,6 +254,10 @@ def document_vote(request, pk): def document_original_file(request, pk): document = get_object_or_404(Document, pk=pk) + # Check if original file exists + if not document.original or not document.original.name: + raise Http404("Le fichier original n'existe pas pour ce document") + body = document.original.read() response = HttpResponse(body, content_type="application/octet-stream") @@ -274,6 +278,11 @@ def document_original_file(request, pk): @login_required def document_pdf_file(request, pk): document = get_object_or_404(Document, pk=pk) + + # Check if PDF file exists + if not document.pdf or not document.pdf.name: + raise Http404("Le fichier PDF n'existe pas pour ce document") + body = document.pdf.read() response = HttpResponse(body, content_type="application/pdf")