X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/357027375ff8867f42ca34bcbfb5a78b5b185fc3..7bbeecb5dd5c5f20120f9e40e41568a3bff1caa4:/src/reporting/utils.py?ds=inline diff --git a/src/reporting/utils.py b/src/reporting/utils.py old mode 100755 new mode 100644 index 8ecb9b045..ec4ff791c --- a/src/reporting/utils.py +++ b/src/reporting/utils.py @@ -1,6 +1,5 @@ -# -*- coding: utf-8 -*- -# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. -# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. +# This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Wolne Lektury. See NOTICE for more information. # from errno import ENOENT import os @@ -8,6 +7,7 @@ import os.path from django.conf import settings import logging from django.http import HttpResponse +from wolnelektury.utils import makedirs logger = logging.getLogger(__name__) @@ -21,7 +21,7 @@ def render_to_pdf(output_path, template, context=None, add_files=None): :param dict add_files: a dictionary of additional files XeTeX will need """ - from StringIO import StringIO + from io import BytesIO import shutil from tempfile import mkdtemp import subprocess @@ -29,10 +29,10 @@ def render_to_pdf(output_path, template, context=None, add_files=None): from django.template.loader import render_to_string rendered = render_to_string(template, context) - texml = StringIO(rendered.encode('utf-8')) + texml = BytesIO(rendered.encode('utf-8')) tempdir = mkdtemp(prefix="render_to_pdf-") tex_path = os.path.join(tempdir, "doc.tex") - with open(tex_path, 'w') as tex_file: + with open(tex_path, 'wb') as tex_file: Texml.processor.process(texml, tex_file, encoding="utf-8") if add_files: @@ -47,12 +47,10 @@ def render_to_pdf(output_path, template, context=None, add_files=None): cwd = os.getcwd() os.chdir(tempdir) try: - subprocess.check_call(['xelatex', '-interaction=batchmode', tex_path], + subprocess.check_call( + ['xelatex', '-interaction=batchmode', tex_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE) - try: - os.makedirs(os.path.dirname(output_path)) - except: - pass + makedirs(os.path.dirname(output_path)) shutil.move(os.path.join(tempdir, "doc.pdf"), output_path) finally: os.chdir(cwd) @@ -70,13 +68,10 @@ def render_to_csv(output_path, template, context=None, add_files=None): from django.template.loader import render_to_string - try: - os.makedirs(os.path.dirname(output_path)) - except: - pass + makedirs(os.path.dirname(output_path)) rendered = render_to_string(template, context) - with open(output_path, 'w') as csv_file: + with open(output_path, 'wb') as csv_file: csv_file.write(rendered.encode('utf-8')) @@ -115,7 +110,7 @@ def generated_file_view(file_name, mime_type, send_name=None, signals=None): response = HttpResponse(content_type=mime_type) response['Content-Disposition'] = 'attachment; filename=%s' % name - with open(file_path) as f: + with open(file_path, 'rb') as f: for chunk in read_chunks(f): response.write(chunk) return response