changeset 3:15807d87930c

Refactor ConvertView to be easier reusable
author Björn Ricks <bjoern.ricks@intevation.de>
date Fri, 26 Sep 2014 15:34:46 +0200
parents 745a53c8b4f2
children 46f31348fe01
files odfcast/convert.py
diffstat 1 files changed, 20 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/odfcast/convert.py	Fri Sep 26 14:26:58 2014 +0200
+++ b/odfcast/convert.py	Fri Sep 26 15:34:46 2014 +0200
@@ -28,26 +28,33 @@
         return __import__(pyuno_driver_name, globals(), locals(),
                           ["Convertor"])
 
-    def post(self):
-        file = request.files['file']
-        format = request.form['format']
+    def is_format_supported(self, fformat):
+        return fformat and fformat.lower() in ALLOWED_FORMATS
 
-        if format not in ALLOWED_FORMATS:
-            return "Format %s not allowed" % format, 401
+    def post(self):
+        ffile = request.files['file']
+        fformat = request.form['format']
 
-        outfile = self.convert(file, format)
-        mimetype = self.get_mimetype_for_format(format)
+        if not self.is_format_supported(fformat):
+            return "Format %s not allowed" % fformat, 401
+
+        infile = self.save_form_file(ffile)
+        outfile = self.convert(infile, fformat)
+        mimetype = self.get_mimetype_for_format(fformat)
         return Response(outfile, mimetype=mimetype)
 
-    def convert(self, file, format):
-        infile = tempfile.NamedTemporaryFile()
-        file.save(infile.name)
+    def save_form_file(self, infile):
+        outfile = tempfile.NamedTemporaryFile()
+        infile.save(outfile.name)
+        return outfile
+
+    def convert(self, infile, fformat):
         outfile = tempfile.NamedTemporaryFile()
 
-        self.convertor.convert(infile.name, outfile.name, format)
+        self.convertor.convert(infile.name, outfile.name, fformat)
 
         infile.close()
         return outfile
 
-    def get_mimetype_for_format(self, format):
-        return MIMETYPES.get(format, DEFAULT_MIMETYPE)
+    def get_mimetype_for_format(self, fformat):
+        return MIMETYPES.get(fformat, DEFAULT_MIMETYPE)
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)