summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Williams <andy@andywilliams.me>2016-08-06 09:06:07 +0100
committerAndy Williams <andy@andywilliams.me>2016-08-06 09:06:07 +0100
commit0391668d1eff852c0081c66abe7d3fc7b2ac4fdd (patch)
tree55daa824a0a42a28e88cab19ad0c20aaf4723a92
parent86d1dfa3dbd1e0b39aad9424ee43bbcbbfaec03b (diff)
Add a simple CSV output for our non-json friends
-rw-r--r--app/__init__.py18
1 files changed, 17 insertions, 1 deletions
diff --git a/app/__init__.py b/app/__init__.py
index 7968ecc..308de87 100644
--- a/app/__init__.py
+++ b/app/__init__.py
@@ -1,9 +1,12 @@
1from flask import Flask 1from flask import Flask, Response
2from flask import abort, render_template, send_from_directory 2from flask import abort, render_template, send_from_directory
3from flask_restful import Resource, Api, abort as api_abort 3from flask_restful import Resource, Api, abort as api_abort
4 4
5from app.data import THEMES, BACKGROUNDS 5from app.data import THEMES, BACKGROUNDS
6 6
7import csv
8import io
9
7app = Flask(__name__) 10app = Flask(__name__)
8api = Api(app) 11api = Api(app)
9 12
@@ -44,6 +47,19 @@ def themes(theme_id=None):
44 else: 47 else:
45 return render_template('themes.html', themes=THEMES) 48 return render_template('themes.html', themes=THEMES)
46 49
50@api.representation('text/csv')
51def csvformat(data, code, headers=None):
52 dw = io.StringIO()
53 out = csv.writer(dw, quoting=csv.QUOTE_NONNUMERIC)
54 for key, value in data.items():
55 if isinstance(value, dict):
56 out.writerow([key])
57 else:
58 out.writerow([key, value])
59 resp = Response(dw.getvalue(), 'text/csv', headers=headers)
60 resp.status_code = code
61 return resp
62
47api.add_resource(ThemeList, '/v1/themes/') 63api.add_resource(ThemeList, '/v1/themes/')
48api.add_resource(Theme, '/v1/themes/<string:theme_id>') 64api.add_resource(Theme, '/v1/themes/<string:theme_id>')
49 65