diff --git a/sdbs_pile/pile/models.py b/sdbs_pile/pile/models.py index 26da648..78d1ffd 100644 --- a/sdbs_pile/pile/models.py +++ b/sdbs_pile/pile/models.py @@ -2,6 +2,7 @@ from django.core.exceptions import ValidationError from django.core.files.storage import FileSystemStorage from django.db import models from django.db.models import Count +from django.urls import reverse from model_utils.models import SoftDeletableModel @@ -56,10 +57,14 @@ class Document(SoftDeletableModel): @property def url(self): - if self.file: - return f"/docs/{self.file.url}" + if self.is_local_pdf: + return reverse("pile:label", args=[self.id]) + "?fallback=1" return self.external_url + @property + def is_local_pdf(self): + return self.file.name is not None and self.file.name.endswith(".pdf") + class Meta: ordering = ['-id'] permissions = [ diff --git a/sdbs_pile/pile/templates/front_doc_detail.html b/sdbs_pile/pile/templates/front_doc_detail.html index 4876750..58eacfd 100644 --- a/sdbs_pile/pile/templates/front_doc_detail.html +++ b/sdbs_pile/pile/templates/front_doc_detail.html @@ -36,14 +36,21 @@ {% endif %} - + {% if document.is_local_pdf %} + - {% if document.url %} + + {% else %} + {{ document.url }} + {% endif %} {% endblock %} diff --git a/sdbs_pile/pile/views.py b/sdbs_pile/pile/views.py index e012b82..36838d6 100644 --- a/sdbs_pile/pile/views.py +++ b/sdbs_pile/pile/views.py @@ -8,6 +8,7 @@ from PyPDF2 import PdfFileWriter, PdfFileReader from django.contrib.syndication.views import Feed from django.core.exceptions import ObjectDoesNotExist from django.http import Http404, FileResponse +from django.shortcuts import redirect from django.template.loader import render_to_string from django.utils.text import slugify from django.views import View @@ -93,8 +94,6 @@ class DocumentView(BasePileView): except ObjectDoesNotExist: raise Http404 - document.is_pdf = document.file.name is not None and document.file.name.endswith(".pdf") - return { 'document': document, **base_context_data @@ -114,8 +113,8 @@ class DocumentWithLabel(BasePileViewMixin): weasyprint.HTML(base_url=request.build_absolute_uri(), string=label_html).write_pdf(label_stream) label_stream.seek(0) - final_stream = label_stream - + final_stream = None + concat_succeeded = False if document.file: try: with open(document.file.path, 'rb') as document_fp: @@ -127,8 +126,19 @@ class DocumentWithLabel(BasePileViewMixin): final_stream = io.BytesIO() writer.write(final_stream) final_stream.seek(0) + concat_succeeded = True except Exception as exc: logging.exception(exc) + + if not concat_succeeded: + if self.request.GET.get("fallback"): + if document.is_local_pdf: + return FileResponse(document.file, + filename=f"pile_{document.id}__{slugify(document.title)}.pdf", + content_type="application/pdf") + else: + return redirect(document.external_url) + else: final_stream = label_stream return FileResponse(final_stream,