Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion catalog/templates/catalog/course.html
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ <h1 class="d-flex align-items-center gap-2">
</svg>
</a>
{% endif %}
<a class="btn" title="Télécharger" href="{% url 'document_pdf' document.pk %}"
<a class="btn" title="Télécharger"
href="{% if document.pdf and document.pdf.name %}{% url 'document_pdf' document.pk %}{% else %}{% url 'document_original' document.pk %}{% endif %}"
data-turbo="false">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor"
class="bi bi-download" viewBox="0 0 16 16">
Expand Down
11 changes: 10 additions & 1 deletion documents/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down