retrieve document with label as the default

This commit is contained in:
Tomáš Mládek 2020-04-01 01:10:26 +02:00
parent 5168ee6840
commit 13b17710c8
3 changed files with 33 additions and 11 deletions

View file

@ -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 = [

View file

@ -36,14 +36,21 @@
</div>
{% endif %}
<div class="doc-link">
<span class="doc-link-intro">Get {% if document.is_pdf %}document with {% endif %}print label: </span>
<a href="{% url "pile:label" document.id %}">{% url "pile:label" document.id %}</a></div>
{% if document.is_local_pdf %}
<div class="doc-link">
<span class="doc-link-intro">Access file as:</span>
<a href="{{ document.url }}">Entry #{{ document.id }} of /-\ pile</a>
</div>
{% if document.url %}
<div class="doc-link">
<span class="doc-link-intro">Get original document at: </span>
<a href="/docs/{{ document.file.url }}">{{ document.file.url }}</a>
</div>
{% else %}
<div class="doc-link">
<span class="doc-link-intro">Access file at:</span>
<a href="{{ document.url }}">{{ document.url }}</a></div>
<a href="{{ document.url }}">{{ document.url }}</a>
</div>
{% endif %}
</div>
{% endblock %}

View file

@ -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,