ki-backend/ki/routes.py

54 lines
1.3 KiB
Python
Raw Normal View History

2021-06-07 18:52:30 +02:00
import os
from flask import jsonify, make_response, request, send_file
2021-06-07 17:52:14 +02:00
from ki.models import Skill
2021-06-06 22:25:10 +02:00
from app import app
2021-06-07 17:52:14 +02:00
def models_to_list(models):
models_list = []
for model in models:
models_list.append(model.to_dict())
return models_list
2021-06-06 22:25:10 +02:00
@app.route("/")
def hello_world():
return "KI"
2021-06-07 17:52:14 +02:00
@app.route("/skills")
def get_skills():
skills_query = Skill.query
if "search" in request.args:
skills_query = skills_query.filter(Skill.name.startswith(request.args.get("search")))
skills = skills_query.order_by(Skill.name) \
.limit(10) \
.all()
api_skills = models_to_list(skills)
response_data = {"skills": api_skills}
return response_data
2021-06-07 18:52:30 +02:00
@app.route("/skills/<skill_id>/icon")
def get_skill_icon(skill_id):
skill = Skill.query.get(skill_id)
if skill is None:
return make_response(jsonify([]), 404)
icons_base_path = app.config["KI_DATA_PATH"] + "/skill_icons/"
icon_base_path = icons_base_path + str(skill.id)
icon_svg_path = icon_base_path + ".svg"
if os.path.exists(icon_svg_path):
return send_file(icon_svg_path, mimetype="image/svg")
fallback_icon_path = app.config["KI_DATA_PATH"] + "/skill_icons/placeholder.svg"
return send_file(fallback_icon_path, mimetype="image/svg")