Browse Source

Add proper JSON headers

master
Pete Shadbolt 7 years ago
parent
commit
1342037093
2 changed files with 13 additions and 5 deletions
  1. +12
    -5
      app.py
  2. +1
    -0
      templates/index.html

+ 12
- 5
app.py View File

@@ -1,4 +1,4 @@
from flask import Flask, request, redirect, url_for, make_response, render_template, Markup, send_from_directory, send_file
from flask import Flask, request, redirect, url_for, make_response, render_template, Markup, send_from_directory, send_file, jsonify, Response
from flask_redis import FlaskRedis from flask_redis import FlaskRedis
import json, abp, markdown import json, abp, markdown
from pprint import pprint from pprint import pprint
@@ -35,12 +35,12 @@ def graph(uuid):
return "Posted {} bytes OK".format(len(data)) return "Posted {} bytes OK".format(len(data))


elif redis.exists(uuid): elif redis.exists(uuid):
return redis.get(uuid)
return jsonify(json.loads(redis.get(uuid)))
else: else:
g = abp.GraphState() g = abp.GraphState()
data = json.dumps(g.to_json(stringify=True))
redis.setex(uuid, DAY, data)
return data
data = g.to_json(stringify=True)
redis.setex(uuid, DAY, json.dumps(data))
return jsonify(data)


@app.route("/<uuid>/edit", methods=["POST"]) @app.route("/<uuid>/edit", methods=["POST"])
def edit(uuid): def edit(uuid):
@@ -85,3 +85,10 @@ def doc():
body = Markup(markdown.markdown(render_template("doc.md"), extensions=["markdown.extensions.codehilite"])) body = Markup(markdown.markdown(render_template("doc.md"), extensions=["markdown.extensions.codehilite"]))
return render_template("boilerplate.html", body=body) return render_template("boilerplate.html", body=body)



@app.route("/<uuid>/download")
def download(uuid):
return Response(redis.get(uuid),
mimetype='application/json',
headers={'Content-Disposition':'attachment;filename=graph.json'})


+ 1
- 0
templates/index.html View File

@@ -45,6 +45,7 @@
<ul> <ul>
<li><a onclick="editor.clear()">Clear</a></li> <li><a onclick="editor.clear()">Clear</a></li>
<li><a onclick="editor.raussendorf()">Raussendorf</a></li> <li><a onclick="editor.raussendorf()">Raussendorf</a></li>
<li><a href="/{{uuid}}/download">Download</a></li>
<li><a href="/doc">Help</a></li> <li><a href="/doc">Help</a></li>
</ul> </ul>
</div> </div>


Loading…
Cancel
Save