From 3352f862195ca00494f2aed3ec7d548cb1dd1b5c Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Thu, 28 Jun 2018 11:25:14 +0200 Subject: Migrate from xhtml2odt to pandoc for ODT generation (refs #4178) --- ishtar_common/menus.py | 1 + .../templates/ishtar/blocks/window_image.html | 84 +++------------------- .../ishtar/blocks/window_image_detail.html | 74 +++++++++++++++++++ .../templates/ishtar/blocks/window_image_odt.html | 9 +++ ishtar_common/templates/ishtar/sheet.html | 26 ++++--- ishtar_common/views_item.py | 59 +++++++-------- 6 files changed, 132 insertions(+), 121 deletions(-) create mode 100644 ishtar_common/templates/ishtar/blocks/window_image_detail.html create mode 100644 ishtar_common/templates/ishtar/blocks/window_image_odt.html (limited to 'ishtar_common') diff --git a/ishtar_common/menus.py b/ishtar_common/menus.py index 6d51323ab..43415793f 100644 --- a/ishtar_common/menus.py +++ b/ishtar_common/menus.py @@ -62,6 +62,7 @@ class Menu: self.user = user self.initialized = False self.items = {} + self.childs = [] self.current_action = current_action self.current_section = None self.current_url = None diff --git a/ishtar_common/templates/ishtar/blocks/window_image.html b/ishtar_common/templates/ishtar/blocks/window_image.html index aa43e1ca5..ab1013df9 100644 --- a/ishtar_common/templates/ishtar/blocks/window_image.html +++ b/ishtar_common/templates/ishtar/blocks/window_image.html @@ -1,81 +1,12 @@ {% load i18n %}{% if item.images.count %} +{% if output == "ODT" or output == "PDF"%} +{% include "ishtar/blocks/window_image_odt.html" %} +{% else %}
{% for image in item.images.all %}
× - {% if image.title %}

{{image.title}}

{% endif %} - {% if image.description %}

- {{image.description}} -

{% endif %} - {% if image.licences.count %} -
-
- {% trans "Licenses" %} -
-
- {% for license in image.licenses.all %} - {% if not forloop.first %} ; {% endif %}{% if license.url %}{% endif %} - {{license}} - {% if license.url %}{% endif %} - {% endfor %} -
-
- {% endif %} - {% if image.authors.count or image.authors_raw %} -
-
- {% trans "Authors" %} -
-
- {% for author in image.authors.all %} - {% if not forloop.first %} ; {% endif %}{{author}}{% endfor %} - {% if image.authors_raw %} - {% if image.authors.count %} ; {% endif %} - {{image.authors_raw}} - {% endif %} -
-
- {% endif %} - {% if image.image_type %} -
-
- {% trans "Type" %} -
-
- {{image.image_type}} -
-
- {% endif %} - {% if image.reference %} -
-
- {% trans "Ref." %} -
-
- {{image.reference}} -
-
- {% endif %} - {% if image.internal_reference %} -
-
- {% trans "Internal ref." %} -
-
- {{image.internal_reference}} -
-
- {% endif %} - {% if image.creation_date %} -
-
- {% trans "Creation date" %} -
-
- {{image.creation_date}} -
-
- {% endif %} + {% include "ishtar/blocks/window_image_detail.html" %} {% trans "Modify" %}   @@ -84,10 +15,11 @@
{% for image in item.images.all %} - {% if output != "ODT" %}{% endif %} + - {% if output != "ODT" %}{% endif %} + {% endfor %}
{% endif%} +{% endif%} diff --git a/ishtar_common/templates/ishtar/blocks/window_image_detail.html b/ishtar_common/templates/ishtar/blocks/window_image_detail.html new file mode 100644 index 000000000..c64d72ac2 --- /dev/null +++ b/ishtar_common/templates/ishtar/blocks/window_image_detail.html @@ -0,0 +1,74 @@ +{% load i18n %} + {% if image.title %}

{{image.title}}

{% endif %} + {% if image.description %}

+ {{image.description}} +

{% endif %} + {% if image.licences.count %} +
+
+ {% trans "Licenses" %} +
+
+ {% for license in image.licenses.all %} + {% if not forloop.first %} ; {% endif %}{% if license.url %}{% endif %} + {{license}} + {% if license.url %}{% endif %} + {% endfor %} +
+
+ {% endif %} + {% if image.authors.count or image.authors_raw %} +
+
+ {% trans "Authors" %} +
+
+ {% for author in image.authors.all %} + {% if not forloop.first %} ; {% endif %}{{author}}{% endfor %} + {% if image.authors_raw %} + {% if image.authors.count %} ; {% endif %} + {{image.authors_raw}} + {% endif %} +
+
+ {% endif %} + {% if image.image_type %} +
+
+ {% trans "Type" %} +
+
+ {{image.image_type}} +
+
+ {% endif %} + {% if image.reference %} +
+
+ {% trans "Ref." %} +
+
+ {{image.reference}} +
+
+ {% endif %} + {% if image.internal_reference %} +
+
+ {% trans "Internal ref." %} +
+
+ {{image.internal_reference}} +
+
+ {% endif %} + {% if image.creation_date %} +
+
+ {% trans "Creation date" %} +
+
+ {{image.creation_date}} +
+
+ {% endif %} diff --git a/ishtar_common/templates/ishtar/blocks/window_image_odt.html b/ishtar_common/templates/ishtar/blocks/window_image_odt.html new file mode 100644 index 000000000..9c9383cdd --- /dev/null +++ b/ishtar_common/templates/ishtar/blocks/window_image_odt.html @@ -0,0 +1,9 @@ +{% load i18n %} +
+ {% for image in item.images.all %} +
+ + {% include "ishtar/blocks/window_image_detail.html" %} +
+ {% endfor %} +
diff --git a/ishtar_common/templates/ishtar/sheet.html b/ishtar_common/templates/ishtar/sheet.html index 54a2f767d..e937f1474 100644 --- a/ishtar_common/templates/ishtar/sheet.html +++ b/ishtar_common/templates/ishtar/sheet.html @@ -13,13 +13,23 @@
+ {% if output != "ODT" and output != "PDF"%} {% block toolbar %}{% endblock %} + {% endif %}
+ {% if output != "ODT" and output != "PDF"%} {% block head_sheet %} {% endblock %} + {% endif %}
{% block content %} {% endblock %} diff --git a/ishtar_common/views_item.py b/ishtar_common/views_item.py index b286ab3bf..513035903 100644 --- a/ishtar_common/views_item.py +++ b/ishtar_common/views_item.py @@ -1,13 +1,13 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +from copy import copy, deepcopy import csv import datetime import json import logging -import optparse import re -from copy import copy, deepcopy +import subprocess from tempfile import NamedTemporaryFile from django.conf import settings @@ -26,7 +26,6 @@ from tidylib import tidy_document as tidy from unidecode import unidecode from weasyprint import HTML, CSS from weasyprint.fonts import FontConfiguration -from xhtml2odt import xhtml2odt from ishtar_common.utils import check_model_access_control, CSV_OPTIONS, \ get_all_field_names @@ -180,41 +179,31 @@ def show_item(model, name, extra_dct=None): tpl = loader.get_template('ishtar/sheet_%s.html' % name) context_instance['output'] = 'ODT' content = tpl.render(context_instance, request) + tidy_options = {'output-xhtml': 1, 'indent': 1, + 'tidy-mark': 0, 'doctype': 'auto', + 'add-xml-decl': 1, 'wrap': 1} + html, errors = tidy(content, options=tidy_options) + html = html.encode('utf-8').replace(" ", " ") + html = re.sub(']*)>\n', '', html) + + odt = NamedTemporaryFile() + html_source = NamedTemporaryFile() + with open(html_source.name, 'w') as html_file: + html_file.write(html) + + pandoc_args = ["pandoc", "-f", "html", "-t", "odt", + "-o", odt.name, html_source.name] try: - tidy_options = {'output-xhtml': 1, 'indent': 1, - 'tidy-mark': 0, 'doctype': 'auto', - 'add-xml-decl': 1, 'wrap': 1} - html, errors = tidy(content, options=tidy_options) - html = html.encode('utf-8').replace(" ", " ") - html = re.sub(']*)>\n', '', html) - - odt = NamedTemporaryFile() - options = optparse.Values() - options.with_network = True - for k, v in (('input', ''), - ('output', odt.name), - ('template', settings.ODT_TEMPLATE), - ('with_network', True), - ('top_header_level', 1), - ('img_width', '8cm'), - ('img_height', '6cm'), - ('verbose', False), - ('replace_keyword', 'ODT-INSERT'), - ('cut_start', 'ODT-CUT-START'), - ('htmlid', None), - ('url', "#")): - setattr(options, k, v) - odtfile = xhtml2odt.ODTFile(options) - odtfile.open() - odtfile.import_xhtml(html) - odtfile = odtfile.save() - except xhtml2odt.ODTExportError: - return HttpResponse(content, content_type="application/xhtml") + subprocess.check_call(pandoc_args) + except subprocess.CalledProcessError: + return HttpResponse(content, + content_type="application/xhtml") response = HttpResponse( content_type='application/vnd.oasis.opendocument.text') - response['Content-Disposition'] = 'attachment; filename=%s.odt' % \ - filename - response.write(odtfile) + response['Content-Disposition'] = \ + 'attachment; filename={}.odt'.format(filename) + with open(odt.name, 'r') as odt_file: + response.write(odt_file.read()) return response elif doc_type == 'pdf': tpl = loader.get_template('ishtar/sheet_%s_pdf.html' % name) -- cgit v1.2.3