ki-backend/ki/commands.py

40 lines
1.0 KiB
Python
Raw Normal View History

import csv
from ki.models import Language, Skill
2021-06-07 17:52:14 +02:00
from app import app, db
def seed():
skill_seed_file_path = app.config["KI_DATA_DIR"] + "/seed_data/skills.csv"
2021-06-13 21:41:01 +02:00
print("importing skills")
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"]))
2021-06-07 17:52:14 +02:00
db.session.commit()
2021-06-13 21:41:01 +02:00
@app.cli.command("seed")
def seed_command():
seed()