From 347a069fcd2274f5ae2c31dfde35b65fa492257a Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Tue, 8 Oct 2024 10:53:50 +0200 Subject: ✨ document templates: manage export in PDF, HTML, xlsx, docx using LO unoconv MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ishtar_common/views.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'ishtar_common/views.py') diff --git a/ishtar_common/views.py b/ishtar_common/views.py index 5b675eb52..a6c3b373d 100644 --- a/ishtar_common/views.py +++ b/ishtar_common/views.py @@ -91,6 +91,10 @@ from ishtar_common.utils import ( from ishtar_common.widgets import JQueryAutoComplete from ishtar_common import tasks +convert_document = None +if settings.USE_LIBREOFFICE: + from ishtar_common.libreoffice import convert_document + from .views_item import ( CURRENT_ITEM_KEYS, CURRENT_ITEM_KEYS_DICT, @@ -1535,10 +1539,19 @@ class GenerateView(IshtarMixin, LoginRequiredMixin, View): document = self.publish(tpl, objects) if not document: return HttpResponse(content_type="text/plain") + base_extension = tpl.template.name.split(".")[-1].lower() + extension = tpl.export_format + if not extension: + extension = base_extension + if not settings.USE_LIBREOFFICE and extension not in ("ods", "odt"): + return HttpResponseBadRequest("Invalid format type - need LibreOffice daemon") # bad configuration + content_type = models.EXPORT_FORMATS_CONTENT_TYPE.get( + extension, "application/vnd.oasis.opendocument.text" + ) + if tpl.export_format and convert_document: + document = convert_document(document, tpl.export_format) with open(document, "rb") as f: - response = HttpResponse( - f.read(), content_type="application/vnd.oasis.opendocument.text" - ) + response = HttpResponse(f.read(), content_type=content_type) response["Content-Disposition"] = "attachment; filename={}".format( document.split(os.sep)[-1] ) -- cgit v1.2.3