Skip to content

Commit dcd8a1f

Browse files
C4ptainCrunchclaude
andcommitted
Fix PDF download error for documents without PDF files
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 <noreply@anthropic.com>
1 parent e95ffcc commit dcd8a1f

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

catalog/templates/catalog/course.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ <h1 class="d-flex align-items-center gap-2">
216216
</svg>
217217
</a>
218218
{% endif %}
219-
<a class="btn" title="Télécharger" href="{% url 'document_pdf' document.pk %}"
219+
<a class="btn" title="Télécharger"
220+
href="{% if document.pdf and document.pdf.name %}{% url 'document_pdf' document.pk %}{% else %}{% url 'document_original' document.pk %}{% endif %}"
220221
data-turbo="false">
221222
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor"
222223
class="bi bi-download" viewBox="0 0 16 16">

documents/views.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from django.contrib import messages
66
from django.contrib.auth.decorators import login_required
77
from django.db.models import F
8-
from django.http import HttpResponse, HttpResponseRedirect
8+
from django.http import Http404, HttpResponse, HttpResponseRedirect
99
from django.shortcuts import get_object_or_404, redirect, render
1010
from django.urls import reverse
1111
from django.views.decorators.clickjacking import xframe_options_sameorigin
@@ -254,6 +254,10 @@ def document_vote(request, pk):
254254
def document_original_file(request, pk):
255255
document = get_object_or_404(Document, pk=pk)
256256

257+
# Check if original file exists
258+
if not document.original or not document.original.name:
259+
raise Http404("Le fichier original n'existe pas pour ce document")
260+
257261
body = document.original.read()
258262

259263
response = HttpResponse(body, content_type="application/octet-stream")
@@ -274,6 +278,11 @@ def document_original_file(request, pk):
274278
@login_required
275279
def document_pdf_file(request, pk):
276280
document = get_object_or_404(Document, pk=pk)
281+
282+
# Check if PDF file exists
283+
if not document.pdf or not document.pdf.name:
284+
raise Http404("Le fichier PDF n'existe pas pour ce document")
285+
277286
body = document.pdf.read()
278287

279288
response = HttpResponse(body, content_type="application/pdf")

0 commit comments

Comments
 (0)