implement skills and languages endpoints

This commit is contained in:
2021-06-07 22:04:03 +02:00
parent b161254363
commit 6bedeef48a
255 changed files with 296 additions and 35 deletions

View File

@ -1,9 +1,34 @@
from ki.models import Skill
import csv
from ki.models import Language, Skill
from app import app, db
@app.cli.command("seed")
def seed():
db.session.add(Skill(id=1, name="PHP"))
db.session.add(Skill(id=2, name="Vue.js"))
skill_seed_file_path = app.config["KI_DATA_DIR"] + "/seed_data/skills.csv"
with open(skill_seed_file_path) as skills_file:
skills_csv_reader = csv.DictReader(skills_file)
for skill in skills_csv_reader:
id = int(skill["id"])
db_skill = Skill.query.get(id)
if db_skill is None:
db.session.add(Skill(id=int(skill["id"]), name=skill["name"]))
iso_seed_file_path = app.config["KI_DATA_DIR"] + "/seed_data/iso_639_1.csv"
with open(iso_seed_file_path) as iso_file:
iso_csv_reader = csv.DictReader(iso_file)
for iso in iso_csv_reader:
id = iso["639-1"]
db_language = Language.query.get(id)
if db_language is None:
db.session.add(Language(id=iso["639-1"], name=iso["Sprache"]))
db.session.commit()

View File

@ -12,7 +12,7 @@ user_skill_table = Table("user_skill", db.Model.metadata,
user_language_table = Table(
"user_language", db.Model.metadata,
Column("user_id", Integer, ForeignKey("user.id")),
Column("language_id", Integer, ForeignKey("language.id")))
Column("language_id", String(2), ForeignKey("language.id")))
class User(db.Model):
@ -86,9 +86,12 @@ class Skill(db.Model):
class Language(db.Model):
__tablename__ = "language"
id = Column(Integer, primary_key=True)
id = Column(String(2), primary_key=True)
name = Column(String(25), nullable=False)
users = relationship("User",
secondary=user_language_table,
back_populates="languages")
def to_dict(self):
return {"id": self.id, "name": self.name}

View File

@ -1,7 +1,7 @@
import os
from flask import jsonify, make_response, request, send_file
from flask import make_response, request, send_file
from ki.models import Skill
from ki.models import Language, Skill
from app import app
@ -14,6 +14,52 @@ def models_to_list(models):
return models_list
def handle_completion_request(model, key):
query = model.query
if "search" in request.args:
query = query.filter(model.name.startswith(request.args.get("search")))
results = query.order_by(model.name) \
.limit(10) \
.all()
api_results = models_to_list(results)
response_data = {}
response_data[key] = api_results
return response_data
def handle_icon_request(model, id, path):
object = model.query.get(id)
if object is None:
return make_response({}, 404)
icon_base_path = path + str(id)
icon_svg_path = icon_base_path + ".svg"
if os.path.exists(icon_svg_path):
return send_file(icon_svg_path, mimetype="image/svg")
icon_png_path = icon_base_path + ".png"
if os.path.exists(icon_png_path):
return send_file(icon_png_path, mimetype="image/png")
unknown_svg_path = path + "unknown.svg"
if os.path.exists(unknown_svg_path):
return send_file(unknown_svg_path, mimetype="image/svg")
unknown_png_path = path + "unknown.png"
if os.path.exists(unknown_png_path):
return send_file(unknown_png_path, mimetype="image/png")
return make_response({"error": "icon not found"}, 404)
@app.route("/")
def hello_world():
return "KI"
@ -21,33 +67,21 @@ def hello_world():
@app.route("/skills")
def get_skills():
skills_query = Skill.query
return handle_completion_request(Skill, "skills")
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
@app.route("/skills/<skill_id>/icon")
def get_skill_icon(skill_id):
skill = Skill.query.get(skill_id)
skill_icons_path = app.config["KI_DATA_DIR"] + "/imgs/skill_icons/"
return handle_icon_request(Skill, skill_id, skill_icons_path)
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"
@app.route("/languages")
def get_languages():
return handle_completion_request(Language, "languages")
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")
@app.route("/languages/<language_id>/icon")
def get_language_icon(language_id):
language_flags_path = app.config["KI_DATA_DIR"] + "/imgs/flags/"
return handle_icon_request(Language, language_id, language_flags_path)