From 48ef99341f6bab0f4eb6e09b5d5394da9280ea92 Mon Sep 17 00:00:00 2001 From: div72 <60045611+div72@users.noreply.github.com> Date: Wed, 27 Jul 2022 17:53:56 +0300 Subject: [PATCH] backend: support for csv export --- formie/forms.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/formie/forms.py b/formie/forms.py index 486c6bc..c212665 100644 --- a/formie/forms.py +++ b/formie/forms.py @@ -1,9 +1,11 @@ +import csv import datetime +import io import json from dataclasses import dataclass from typing import List -from flask import abort, g, redirect, render_template, request, url_for, Blueprint +from flask import abort, g, redirect, render_template, request, url_for, Blueprint, Response from formie import auth from formie.models import db, Field, ChoiceField, Form, TextField, RangeField @@ -130,6 +132,12 @@ def view_results(form_id: int): cols.append(getattr(res, col)) results.append(cols) + if request.args.get('format', default = None, type=str) == "csv": + buf = io.StringIO() + csv.writer(buf).writerows(results) + buf.seek(0) + return Response(buf.read(), mimetype='text/csv') + return render_template( "forms/results.html", schema=schema, results=results )