endorsement/api.py

37 lines
1 KiB
Python
Raw Normal View History

2018-11-28 15:02:38 +01:00
from app import app, db
from models import User, Achievement
import json
2018-11-28 15:34:45 +01:00
from graphviz import Digraph
from flask import escape
2018-11-28 15:02:38 +01:00
@app.route("/api")
@app.route("/api/all")
def api_all():
return json.dumps([user.to_obj() for user in User.query.all()])
@app.route("/api/users")
def api_all_users():
return json.dumps([user.id for user in User.query.all()])
@app.route("/api/achievements")
def api_all_achievements():
return json.dumps([achievement.to_obj() for achievement in Achievement.query.all()])
2018-11-28 15:34:45 +01:00
@app.route("/api/svg")
def api_svg():
return dot_graph("svg")
@app.route("/api/dot")
def api_dot():
return dot_graph("dot")
def dot_graph(format):
dot = Digraph(format=format)
for user in User.query.all():
dot.node(escape(user.id), escape(user.id))
for achievement in Achievement.query.all():
for endorser in achievement.endorsed_by:
dot.edge(escape(endorser.id), escape(achievement.achieved_by), label=escape(achievement.name))
return dot.pipe().decode("utf-8")