changeset 4:46f31348fe01

Implement a TemplateConvertView to allow convert odt templates with variable substitution
author Björn Ricks <bjoern.ricks@intevation.de>
date Fri, 26 Sep 2014 15:35:21 +0200 (2014-09-26)
parents 15807d87930c
children f804af69b783
files odfcast/convert.py
diffstat 1 files changed, 29 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/odfcast/convert.py	Fri Sep 26 15:34:46 2014 +0200
+++ b/odfcast/convert.py	Fri Sep 26 15:35:21 2014 +0200
@@ -2,9 +2,11 @@
 
 import tempfile
 
-from flask import request, Response
+from flask import request, Response, json
 from flask.views import MethodView
 
+from py3o.template import Template
+
 ALLOWED_FORMATS = ["pdf", "doc", "docx", "odt"]
 
 MIMETYPES = {
@@ -58,3 +60,29 @@
 
     def get_mimetype_for_format(self, fformat):
         return MIMETYPES.get(fformat, DEFAULT_MIMETYPE)
+
+
+class TemplateConvertView(ConvertView):
+
+    def post(self):
+        ffile = request.files['file']
+        fformat = request.form['format']
+        datadict = self.get_datadict()
+
+        if not self.is_format_supported(fformat):
+            return "Format %s not allowed" % fformat, 401
+
+        tfile = self.save_form_file(ffile)
+        outfile = tempfile.NamedTemporaryFile()
+        t = Template(tfile, outfile.name)
+        t.render(datadict)
+
+        if fformat != "odt":
+            outfile = self.convert(outfile, fformat)
+
+        mimetype = self.get_mimetype_for_format(fformat)
+        return Response(outfile, mimetype=mimetype)
+
+    def get_datadict(self):
+        vars = request.form['datadict']
+        return json.loads(vars)
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)