From b4fbeea21e20d017255d3a1236dbbafda29dd3b6 Mon Sep 17 00:00:00 2001 From: Kara Engelhardt Date: Tue, 12 Nov 2024 20:40:42 +0100 Subject: [PATCH] Add simple pdf export --- .../froide_evidencecollection/list.html | 3 + .../froide_evidencecollection/pdf_export.html | 69 +++++++++++++++++++ froide_evidencecollection/views.py | 16 ++++- 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 froide_evidencecollection/templates/froide_evidencecollection/pdf_export.html diff --git a/froide_evidencecollection/templates/froide_evidencecollection/list.html b/froide_evidencecollection/templates/froide_evidencecollection/list.html index ff2893a..b239c06 100644 --- a/froide_evidencecollection/templates/froide_evidencecollection/list.html +++ b/froide_evidencecollection/templates/froide_evidencecollection/list.html @@ -50,6 +50,9 @@

{% translate 'Refine your query' %}

  • {% trans "Export search as XLSX" %}
  • +
  • + {% trans "Export search as PDF" %} +
  • {% else %} diff --git a/froide_evidencecollection/templates/froide_evidencecollection/pdf_export.html b/froide_evidencecollection/templates/froide_evidencecollection/pdf_export.html new file mode 100644 index 0000000..b839a56 --- /dev/null +++ b/froide_evidencecollection/templates/froide_evidencecollection/pdf_export.html @@ -0,0 +1,69 @@ +{% load i18n %} + + + Export + + + + {% for row in rows %} +
    +

    {{ row.title }}

    + + + + + + + + + + + + + + + + + + + + + + + + + +
    {% trans "Date" %}{{ row.date }}
    {% trans "Source" %} + {{ row.source__url }} +
    {% trans "Evidence Type" %}{{ row.type__name }}
    {% trans "Evidence Area" %}{{ row.area__name }}
    {% trans "Person" %}{{ row.person__name }}
    {% trans "Evidence Quality" %}{{ row.quality__name }}
    +
    {{ row.description }}
    +
    + {% endfor %} + + diff --git a/froide_evidencecollection/views.py b/froide_evidencecollection/views.py index 9dc6729..e7c60e3 100644 --- a/froide_evidencecollection/views.py +++ b/froide_evidencecollection/views.py @@ -1,12 +1,15 @@ import csv import io +from django.conf import settings from django.http import HttpResponse +from django.template.loader import render_to_string from django.utils.translation import gettext as _ from django.views.generic import DetailView import openpyxl +from froide.foirequest.pdf_generator import get_wp from froide.helper.breadcrumbs import BreadcrumbView from froide.helper.search.views import BaseSearchView from froide_evidencecollection.documents import EvidenceDocument @@ -67,7 +70,7 @@ class EvidenceExportView(EvidenceListView): "title", "description", ] - FORMATS = ["csv", "xlsx"] + FORMATS = ["csv", "xlsx", "pdf"] def get_rows(self): self.object_list = self.get_queryset() @@ -112,3 +115,14 @@ def generate_xlsx(self, rows): f.getvalue(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", ) + + def generate_pdf(self, rows): + html = render_to_string( + "froide_evidencecollection/pdf_export.html", + context={"rows": rows, "SITE_NAME": settings.SITE_NAME}, + ) + wp = get_wp() + if not wp: + raise Exception("WeasyPrint needs to be installed") + doc = wp.HTML(string=html) + return doc.write_pdf(), "application/pdf"