diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-06-28 11:25:14 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-06-28 11:25:14 +0200 |
commit | 3352f862195ca00494f2aed3ec7d548cb1dd1b5c (patch) | |
tree | 9c76d7cc39a0919e3a3a172157a555817bd62f4b /ishtar_common/views_item.py | |
parent | 51517b852e4548257525462b6c5184c6e006d3a4 (diff) | |
download | Ishtar-3352f862195ca00494f2aed3ec7d548cb1dd1b5c.tar.bz2 Ishtar-3352f862195ca00494f2aed3ec7d548cb1dd1b5c.zip |
Migrate from xhtml2odt to pandoc for ODT generation (refs #4178)
Diffstat (limited to 'ishtar_common/views_item.py')
-rw-r--r-- | ishtar_common/views_item.py | 59 |
1 files changed, 24 insertions, 35 deletions
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('<pre([^>]*)>\n', '<pre\\1>', 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('<pre([^>]*)>\n', '<pre\\1>', 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) |