Skip to content

Commit

Permalink
backend: support for csv export
Browse files Browse the repository at this point in the history
  • Loading branch information
div72 committed Jul 27, 2022
1 parent 4d2afe1 commit 48ef993
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion formie/forms.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
)

0 comments on commit 48ef993

Please sign in to comment.