From 02960099fdb4c54d6a0544f4983d958f2cca83f7 Mon Sep 17 00:00:00 2001 From: patrick Date: Mon, 7 Jun 2021 19:03:39 +0000 Subject: [PATCH] fix access to PDF protocol as a non FET user. --- fet2020/posts/models.py | 2 +- fet2020/posts/urls.py | 8 +++- fet2020/posts/utils.py | 8 ++-- fet2020/posts/views.py | 38 +++++++++++++------ fet2020/templates/home.html | 4 +- fet2020/templates/layout.html | 2 +- .../posts/partials/_article_row.html | 2 +- .../templates/posts/partials/_date_box.html | 2 +- .../posts/partials/_meeting_row.html | 2 +- .../templates/posts/partials/_posts_hero.html | 5 ++- .../posts/partials/_posts_hero_compact.html | 5 ++- fet2020/templates/posts/show.html | 6 +-- 12 files changed, 54 insertions(+), 30 deletions(-) diff --git a/fet2020/posts/models.py b/fet2020/posts/models.py index 2751b80b..88264cc6 100644 --- a/fet2020/posts/models.py +++ b/fet2020/posts/models.py @@ -204,7 +204,7 @@ class Post(models.Model): @property def url(self): - return reverse("posts.show", kwargs={"id": self.slug}) + return reverse("posts:posts.show", kwargs={"id": self.slug}) def get_absolute_url(self): return self.url diff --git a/fet2020/posts/urls.py b/fet2020/posts/urls.py index 3b2d7da7..daa8319b 100644 --- a/fet2020/posts/urls.py +++ b/fet2020/posts/urls.py @@ -1,7 +1,10 @@ -from django.urls import path +from django.urls import path, re_path +from . import apps from . import views +app_name = apps.PostsConfig.name + urlpatterns = [ path("func/tag_complete", views.tag_complete), path("func/slug_calc", views.slug_calc), @@ -9,5 +12,6 @@ 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"), + re_path(r"^(?P[-\w]+)/agenda.pdf$", views.show_pdf_agenda, name="show_pdf_agenda"), + re_path(r"^(?P[-\w]+)/protokoll.pdf$", views.show_pdf_protocol, name="show_pdf_protocol"), ] diff --git a/fet2020/posts/utils.py b/fet2020/posts/utils.py index 1ed2ac7e..22385d91 100644 --- a/fet2020/posts/utils.py +++ b/fet2020/posts/utils.py @@ -4,10 +4,12 @@ from xhtml2pdf import pisa def render_to_pdf(html): - result = BytesIO() - pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result) + src = BytesIO(html.encode("ISO-8859-1")) + dest = BytesIO() + + pdf = pisa.pisaDocument(src, dest) if not pdf.err: - return HttpResponse(result.getvalue(), content_type='application/pdf') + return HttpResponse(dest.getvalue(), content_type='application/pdf') return None diff --git a/fet2020/posts/views.py b/fet2020/posts/views.py index 521ebf2f..a76b480d 100644 --- a/fet2020/posts/views.py +++ b/fet2020/posts/views.py @@ -10,6 +10,7 @@ from django.utils import timezone from django.utils.text import slugify from taggit.models import Tag +from authentications.decorators import authenticated_user from documents.api import get_pad_link from documents.etherpadlib import add_ep_cookie from members.models import Member, JobMember @@ -191,16 +192,7 @@ 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 - +def show_pdf(request, html, filename): rendered = render_to_string(settings.BASE_DIR + "/templates/posts/pdf/template.html") # get body-content from pdf template @@ -212,7 +204,31 @@ def show_pdf(self, id, filename=None): html = html[:idx] + rendered + html[idx:] pdf = render_to_pdf(html) - return HttpResponse(pdf, content_type='application/pdf') + + if not pdf: + raise Http404("can't create pdf file") + + response = HttpResponse(pdf, content_type='application/pdf') + + content = "inline; filename=%s" % filename + response['Content-Disposition'] = content + + return response + + +def show_pdf_agenda(request, id): + p = __get_post_object(id) + html = p.agenda_html + + return show_pdf(request, html, p.slug + "-agenda") + + +@authenticated_user +def show_pdf_protocol(request, id): + p = __get_post_object(id) + html = p.protocol_html + + return show_pdf(request, html, p.slug + "-protokoll") ########### diff --git a/fet2020/templates/home.html b/fet2020/templates/home.html index 522061e0..dafb2d37 100644 --- a/fet2020/templates/home.html +++ b/fet2020/templates/home.html @@ -86,7 +86,7 @@ {% include 'posts/partials/_date_box.html' %} {% endfor %} - + - Mehr anzeigen + Mehr anzeigen diff --git a/fet2020/templates/layout.html b/fet2020/templates/layout.html index ac9e030f..70a9cbdd 100644 --- a/fet2020/templates/layout.html +++ b/fet2020/templates/layout.html @@ -55,7 +55,7 @@
  • Tasks
  • Intern
  • {% endif %} -
  • News
  • +
  • News
  • Fachschaft
  • Fotos
  • diff --git a/fet2020/templates/posts/partials/_article_row.html b/fet2020/templates/posts/partials/_article_row.html index c765ca51..9c6fe1a1 100644 --- a/fet2020/templates/posts/partials/_article_row.html +++ b/fet2020/templates/posts/partials/_article_row.html @@ -1,4 +1,4 @@ - +

    {{post.title}}

    diff --git a/fet2020/templates/posts/partials/_date_box.html b/fet2020/templates/posts/partials/_date_box.html index 3f3957bf..21ecc99a 100644 --- a/fet2020/templates/posts/partials/_date_box.html +++ b/fet2020/templates/posts/partials/_date_box.html @@ -1,4 +1,4 @@ -
    +
    diff --git a/fet2020/templates/posts/partials/_meeting_row.html b/fet2020/templates/posts/partials/_meeting_row.html index 7bf9409b..c3f164e4 100644 --- a/fet2020/templates/posts/partials/_meeting_row.html +++ b/fet2020/templates/posts/partials/_meeting_row.html @@ -1,4 +1,4 @@ - +

    {{post.title}}

    diff --git a/fet2020/templates/posts/partials/_posts_hero.html b/fet2020/templates/posts/partials/_posts_hero.html index 07f1a125..6abbb264 100644 --- a/fet2020/templates/posts/partials/_posts_hero.html +++ b/fet2020/templates/posts/partials/_posts_hero.html @@ -1,8 +1,9 @@ -
    +

    {{ post.title | safe }}

    {{ post.subtitle|default_if_none:" " }}

    + {% if post.post_type != 'N' %}

    {{ post.event_start|date:"d. F Y" }}

    {% else %} @@ -10,4 +11,4 @@ {% endif %}
    -
    \ No newline at end of file + diff --git a/fet2020/templates/posts/partials/_posts_hero_compact.html b/fet2020/templates/posts/partials/_posts_hero_compact.html index b543450c..ea73071a 100644 --- a/fet2020/templates/posts/partials/_posts_hero_compact.html +++ b/fet2020/templates/posts/partials/_posts_hero_compact.html @@ -1,8 +1,9 @@ - +

    {{ post.title | safe }}

    +
    {% if post.post_type != 'N' %}

    {{ post.event_start|date:"d. F Y" }}

    @@ -11,4 +12,4 @@ {% endif %}
    -
    \ No newline at end of file + diff --git a/fet2020/templates/posts/show.html b/fet2020/templates/posts/show.html index 6855bafe..e6bf3261 100644 --- a/fet2020/templates/posts/show.html +++ b/fet2020/templates/posts/show.html @@ -78,7 +78,7 @@
    - Nächster Artikel
    + Nächster Artikel
    {% if request.user.is_authenticated %}
    @@ -86,7 +86,7 @@ {% if post.has_agenda %} Agenda {% if filename_agenda %} - + {% endif %} @@ -96,7 +96,7 @@ {% if post.has_protocol %} Protokoll {% if filename_protocol %} - + {% endif %}