changeset 30:9d65de2ebe22

Merge TemplateConvertView into ConvertView Also improve temporary file handling.
author Björn Ricks <bjoern.ricks@intevation.de>
date Thu, 16 Oct 2014 10:34:43 +0200
parents 1dadc59c4b9a
children 83bca7dc9bfe
files main.py odfcast/convert.py templates/convert.html templates/template_convert.html
diffstat 4 files changed, 27 insertions(+), 63 deletions(-) [+]
line wrap: on
line diff
--- a/main.py	Thu Oct 16 10:03:32 2014 +0200
+++ b/main.py	Thu Oct 16 10:34:43 2014 +0200
@@ -10,8 +10,7 @@
 if app.config["DEBUG"]:
     logging.basicConfig(level=logging.DEBUG)
 
-from odfcast.convert import ConvertView, TemplateConvertView, MergeView, \
-    TemplateView
+from odfcast.convert import ConvertView, MergeView, TemplateView
 
 
 app.add_url_rule("/convert/",
@@ -21,13 +20,6 @@
                      app.config["PY3O_UNO_SERVER_HOSTNAME"],
                      app.config["PY3O_UNO_SERVER_PORT"],
                  ))
-app.add_url_rule("/template/",
-                 view_func=TemplateConvertView.as_view(
-                     "template",
-                     app.config["PY3O_UNO_DRIVER"],
-                     app.config["PY3O_UNO_SERVER_HOSTNAME"],
-                     app.config["PY3O_UNO_SERVER_PORT"],
-                 ))
 app.add_url_rule("/merge/",
                  view_func=MergeView.as_view("merge"),
                  )
--- a/odfcast/convert.py	Thu Oct 16 10:03:32 2014 +0200
+++ b/odfcast/convert.py	Thu Oct 16 10:34:43 2014 +0200
@@ -49,14 +49,25 @@
         if not self.is_format_supported(fformat):
             return "Format %s not allowed" % fformat, 401
 
+        datadict = self.get_datadict()
+
         mimetype = self.get_mimetype_for_format(fformat)
 
-        infile = self.save_form_file(ffile)
-        try:
-            outfile = self.convert(infile, fformat)
-        except:
-            log.exception("Conversion error")
-            return "Conversion error", 500
+        outfile = self.save_form_file(ffile)
+
+        if datadict:
+            tfile = tempfile.NamedTemporaryFile()
+            t = Template(outfile, tfile)
+            t.render(datadict)
+            outfile.close()
+            outfile = tfile
+
+        if format != "odt":
+            try:
+                outfile = self.convert(outfile, fformat)
+            except:
+                log.exception("Conversion error")
+                return "Conversion error", 500
 
         return Response(outfile, mimetype=mimetype)
 
@@ -64,8 +75,9 @@
         return render_template("convert.html")
 
     def save_form_file(self, infile):
-        outfile = tempfile.NamedTemporaryFile()
-        infile.save(outfile.name)
+        outfile = tempfile.TemporaryFile()
+        infile.save(outfile)
+        infile.close()
         return outfile
 
     def convert(self, infile, fformat):
@@ -79,35 +91,12 @@
     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']
+        if not vars:
+            return None
         return json.loads(vars)
 
-    def get(self):
-        return render_template("template_convert.html")
-
 
 class MergeView(MethodView):
 
--- a/templates/convert.html	Thu Oct 16 10:03:32 2014 +0200
+++ b/templates/convert.html	Thu Oct 16 10:34:43 2014 +0200
@@ -9,6 +9,10 @@
         <input id="format" type="text" placeholder="Format" name="format" class="form-control">
     </div>
     <div class="form-group">
+        <label for="datadict">Data in JSON Format</label>
+        <textarea id="datadict" name="datadict" class="form-control"></textarea>
+    </div>
+    <div class="form-group">
         <label for="file" >File</label>
         <input id="file" type="file" name="file">
     </div>
--- a/templates/template_convert.html	Thu Oct 16 10:03:32 2014 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,21 +0,0 @@
-{% extends 'base.html' %}
-
-{% block content %}
-<h1>Template Convert</h1>
-
-<form role="form" action="" method="POST" enctype="multipart/form-data">
-    <div class="form-group">
-        <label for="format">Format</label>
-        <input id="format" type="text" placeholder="Format" name="format" class="form-control">
-    </div>
-    <div class="form-group">
-        <label for="datadict">Data in JSON Format</label>
-        <textarea id="datadict" name="datadict" class="form-control"></textarea>
-    </div>
-    <div class="form-group">
-        <label for="file" >File</label>
-        <input id="file" type="file" name="file">
-    </div>
-    <button class="btn btn-default" type="submit">Convert</button>
-</form>
-{% endblock %}
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)