Mercurial > odfcast > odfcast
comparison odfcast/convert.py @ 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 | 4a34f72f036b |
children | 46f31348fe01 |
comparison
equal
deleted
inserted
replaced
2:745a53c8b4f2 | 3:15807d87930c |
---|---|
26 | 26 |
27 def _load_driver_module(self, pyuno_driver_name): | 27 def _load_driver_module(self, pyuno_driver_name): |
28 return __import__(pyuno_driver_name, globals(), locals(), | 28 return __import__(pyuno_driver_name, globals(), locals(), |
29 ["Convertor"]) | 29 ["Convertor"]) |
30 | 30 |
31 def is_format_supported(self, fformat): | |
32 return fformat and fformat.lower() in ALLOWED_FORMATS | |
33 | |
31 def post(self): | 34 def post(self): |
32 file = request.files['file'] | 35 ffile = request.files['file'] |
33 format = request.form['format'] | 36 fformat = request.form['format'] |
34 | 37 |
35 if format not in ALLOWED_FORMATS: | 38 if not self.is_format_supported(fformat): |
36 return "Format %s not allowed" % format, 401 | 39 return "Format %s not allowed" % fformat, 401 |
37 | 40 |
38 outfile = self.convert(file, format) | 41 infile = self.save_form_file(ffile) |
39 mimetype = self.get_mimetype_for_format(format) | 42 outfile = self.convert(infile, fformat) |
43 mimetype = self.get_mimetype_for_format(fformat) | |
40 return Response(outfile, mimetype=mimetype) | 44 return Response(outfile, mimetype=mimetype) |
41 | 45 |
42 def convert(self, file, format): | 46 def save_form_file(self, infile): |
43 infile = tempfile.NamedTemporaryFile() | 47 outfile = tempfile.NamedTemporaryFile() |
44 file.save(infile.name) | 48 infile.save(outfile.name) |
49 return outfile | |
50 | |
51 def convert(self, infile, fformat): | |
45 outfile = tempfile.NamedTemporaryFile() | 52 outfile = tempfile.NamedTemporaryFile() |
46 | 53 |
47 self.convertor.convert(infile.name, outfile.name, format) | 54 self.convertor.convert(infile.name, outfile.name, fformat) |
48 | 55 |
49 infile.close() | 56 infile.close() |
50 return outfile | 57 return outfile |
51 | 58 |
52 def get_mimetype_for_format(self, format): | 59 def get_mimetype_for_format(self, fformat): |
53 return MIMETYPES.get(format, DEFAULT_MIMETYPE) | 60 return MIMETYPES.get(fformat, DEFAULT_MIMETYPE) |