# HG changeset patch # User Björn Ricks # Date 1413448483 -7200 # Node ID 9d65de2ebe22bf96ceb8bf65a14136c50cb7d337 # Parent 1dadc59c4b9a645ab2e56f31120999864e972628 Merge TemplateConvertView into ConvertView Also improve temporary file handling. diff -r 1dadc59c4b9a -r 9d65de2ebe22 main.py --- 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"), ) diff -r 1dadc59c4b9a -r 9d65de2ebe22 odfcast/convert.py --- 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): diff -r 1dadc59c4b9a -r 9d65de2ebe22 templates/convert.html --- 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 @@
+ + +
+
diff -r 1dadc59c4b9a -r 9d65de2ebe22 templates/template_convert.html --- 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 %} -

Template Convert

- -
-
- - -
-
- - -
-
- - -
- -
-{% endblock %}