diff --git a/fet2020/posts/urls.py b/fet2020/posts/urls.py index 003a7516..3b2d7da7 100644 --- a/fet2020/posts/urls.py +++ b/fet2020/posts/urls.py @@ -9,4 +9,5 @@ urlpatterns = [ path("", views.index, name="posts.index"), path("fet_calendar.ics", views.calendar, name="posts.calendar"), path("", views.show, name="posts.show"), + path("/", views.show_pdf, name="posts.show_pdf"), ] diff --git a/fet2020/posts/utils.py b/fet2020/posts/utils.py new file mode 100644 index 00000000..1ed2ac7e --- /dev/null +++ b/fet2020/posts/utils.py @@ -0,0 +1,13 @@ +from django.http import HttpResponse +from io import BytesIO +from xhtml2pdf import pisa + + +def render_to_pdf(html): + result = BytesIO() + pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result) + + if not pdf.err: + return HttpResponse(result.getvalue(), content_type='application/pdf') + + return None diff --git a/fet2020/posts/views.py b/fet2020/posts/views.py index e3880739..c6b6332a 100644 --- a/fet2020/posts/views.py +++ b/fet2020/posts/views.py @@ -1,17 +1,20 @@ -from collections import deque import logging -from taggit.models import Tag -from django.shortcuts import render +from collections import deque +from django.conf import settings from django.http import HttpResponse, JsonResponse, HttpResponseServerError -from django.utils.text import slugify +from django.shortcuts import render +from django.template.loader import render_to_string from django.utils import timezone +from django.utils.text import slugify +from taggit.models import Tag from documents.api import get_pad_link from documents.etherpadlib import add_ep_cookie from members.models import Member, JobMember from .models import Post, FetMeeting, FileUpload +from .utils import render_to_pdf logger = logging.getLogger(__name__) @@ -67,11 +70,17 @@ def tags(request, tag=""): return render(request, "posts/tag.html", context) -def show(request, id=None): +def __get_post_object(id=None): if id.isdigit() or id is int: - p = Post.objects.get(id=int(id)) + return Post.objects.get(id=int(id)) elif id != "" and id is not None: - p = Post.objects.get(slug=(id)) + return Post.objects.get(slug=id) + + return None + + +def show(request, id=None): + p = __get_post_object(id) files = deque(FileUpload.objects.filter(post=p)) @@ -85,10 +94,14 @@ def show(request, id=None): ep_agenda_link = "#" ep_protocol_link = "#" + # set filename for pdf, not a nice solution + filename_agenda = None + filename_protocol = None if p.has_agenda: # and p.agenda_key: try: ep_agenda_link = get_pad_link(p.agenda_key) + filename_agenda = p.slug + "-agenda.pdf" except Exception as e: logger.error( "Can't get the agenda link from '%s'. Error: %s", p.agenda_key, e @@ -98,6 +111,7 @@ def show(request, id=None): if p.has_protocol: # and p.protocol_key: try: ep_protocol_link = get_pad_link(p.protocol_key) + filename_protocol = p.slug + "-protokoll.pdf" except Exception as e: logger.error( "Can't get the protocol link from '%s. Error: %s", p.protocol_key, e @@ -120,6 +134,8 @@ def show(request, id=None): "related_posts": related_posts[0:6], "ep_agenda_link": ep_agenda_link, "ep_protocol_link": ep_protocol_link, + "filename_agenda": filename_agenda, + "filename_protocol": filename_protocol, } response = render(request, "posts/show.html", context) @@ -134,6 +150,30 @@ def show(request, id=None): return response +def show_pdf(self, id, filename=None): + p = __get_post_object(id) + + # it works but not a nice solution + html = "" + if "agenda" in filename: + html = p.agenda_html + elif "protokoll" in filename: + html = p.protocol_html + + rendered = render_to_string(settings.BASE_DIR + "/templates/posts/pdf/template.html") + + # get body-content from pdf template + rendered = rendered.split("")[1] + rendered = rendered.split("")[0] + + # set body-content in html + idx = html.index("") + html = html[:idx] + rendered + html[idx:] + + pdf = render_to_pdf(html) + return HttpResponse(pdf, content_type='application/pdf') + + ########### # HELPERS # ########### diff --git a/fet2020/templates/posts/pdf/template.html b/fet2020/templates/posts/pdf/template.html new file mode 100644 index 00000000..f8f747a8 --- /dev/null +++ b/fet2020/templates/posts/pdf/template.html @@ -0,0 +1,13 @@ + + + + + + + + diff --git a/fet2020/templates/posts/show.html b/fet2020/templates/posts/show.html index 4b18e972..91114978 100644 --- a/fet2020/templates/posts/show.html +++ b/fet2020/templates/posts/show.html @@ -84,12 +84,23 @@
{% if post.has_agenda %} - Agenda
+ Agenda + {% if filename_agenda %} + + + + {% endif %} +
{% endif %} {% if post.has_protocol %} - Protokoll
- + Protokoll + {% if filename_protocol %} + + + + {% endif %} +

{% endif %}