add skill icon endpoint

This commit is contained in:
weeman 2021-06-07 18:52:30 +02:00
parent 5321144dc0
commit b5aa267090
Signed by: weeman
GPG Key ID: 34F0524D4DA694A1
2 changed files with 21 additions and 1 deletions

1
app.py
View File

@ -10,6 +10,7 @@ load_dotenv(find_dotenv())
app = Flask(__name__) app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv("SQLALCHEMY_DATABASE_URI") app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv("SQLALCHEMY_DATABASE_URI")
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config["KI_DATA_PATH"] = os.path.dirname(__file__) + "/data"
db = SQLAlchemy(app) db = SQLAlchemy(app)
migrate = Migrate(app, db) migrate = Migrate(app, db)

View File

@ -1,4 +1,5 @@
from flask import request import os
from flask import jsonify, make_response, request, send_file
from ki.models import Skill from ki.models import Skill
from app import app from app import app
@ -32,3 +33,21 @@ def get_skills():
api_skills = models_to_list(skills) api_skills = models_to_list(skills)
response_data = {"skills": api_skills} response_data = {"skills": api_skills}
return response_data return response_data
@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")