endorsement/api.py
2018-11-28 16:04:07 +01:00

36 lines
1 KiB
Python

from app import app, db
from models import User, Achievement
import json
from graphviz import Digraph
from flask import escape
@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()])
@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")