ki-backend/ki/actions/seed.py

168 lines
6.0 KiB
Python
Raw Permalink Normal View History

2021-07-05 19:37:05 +02:00
# SPDX-FileCopyrightText: WTF Kooperative eG <https://wtf-eg.de/>
#
# SPDX-License-Identifier: AGPL-3.0-or-later
2021-06-26 10:51:39 +02:00
import csv
import logging
from app import app, db
2021-07-05 20:39:49 +02:00
from ki.models import Address, Contact, ContactType, Language, Skill, Profile, ProfileLanguage, ProfileSearchtopic, \
ProfileSkill, User
2021-06-26 10:51:39 +02:00
2021-07-02 16:49:14 +02:00
def seed_contacttypes():
contacttypes_seed_file_path = app.config["KI_DATA_DIR"] + "/seed_data/contacttypes.csv"
logging.info("importing contacttypes")
with open(contacttypes_seed_file_path) as file:
csv_reader = csv.DictReader(file)
for contacttype in csv_reader:
id = int(contacttype["id"])
db_contacttype = ContactType.query.get(id)
if db_contacttype is None:
db.session.add(ContactType(id=int(contacttype["id"]), name=contacttype["name"]))
2021-10-04 18:10:31 +02:00
def seed_user(nickname,
visible=False,
skills=[],
languages=[],
volunteerwork="",
availability_status=False,
2021-10-04 18:10:31 +02:00
freetext="",
availability_text="",
availability_hours_per_week=42):
2021-08-16 22:41:03 +02:00
app.logger.info(f"seeding {nickname} \\o/")
user = User(auth_id=nickname)
db.session.add(user)
profile = Profile(nickname=nickname,
pronouns="",
volunteerwork=volunteerwork,
availability_status=availability_status,
availability_text=availability_text,
availability_hours_per_week=availability_hours_per_week,
2021-08-16 22:41:03 +02:00
freetext=freetext,
visible=visible,
user=user)
for skill_data in skills:
skill = ProfileSkill(profile=profile, skill_id=skill_data[0], level=skill_data[1])
db.session.add(skill)
for language_data in languages:
language = ProfileLanguage(profile=profile, language_id=language_data[0], level=language_data[1])
db.session.add(language)
db.session.add(profile)
2021-06-26 10:51:39 +02:00
def seed(dev: bool):
2021-07-02 16:49:14 +02:00
seed_contacttypes()
2021-06-26 10:51:39 +02:00
skill_seed_file_path = app.config["KI_DATA_DIR"] + "/seed_data/skills.csv"
app.logger.info("importing skills")
2021-06-26 10:51:39 +02:00
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"]))
app.logger.info("importing languages")
2021-06-26 10:51:39 +02:00
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"]))
if dev:
app.logger.info("seeding peter :)")
2021-06-26 10:51:39 +02:00
peter = User(auth_id="peter")
db.session.add(peter)
peters_profile = Profile(nickname="peternichtlustig",
pronouns="Herr Dr. Dr.",
volunteerwork="Gartenverein",
availability_status=True,
availability_hours_per_week=42,
availability_text="Immer",
2021-06-26 10:51:39 +02:00
freetext="Ich mag Kaffee",
user=peter)
db.session.add(peters_profile)
2021-07-02 16:49:14 +02:00
matrix_contact = Contact(profile=peters_profile, contacttype_id=4, content="@peter:wtf-eg.de")
2021-06-26 10:51:39 +02:00
db.session.add(matrix_contact)
2021-07-02 16:49:14 +02:00
email_contact = Contact(profile=peters_profile, contacttype_id=1, content="peter@wtf-eg.de")
2021-06-27 13:07:54 +02:00
db.session.add(email_contact)
2021-06-26 10:51:39 +02:00
peters_address = Address(name="Peter Nichtlustig",
street="Waldweg",
house_number="23i",
additional="Hinterhaus",
postcode="13337",
city="Bielefeld",
country="Deutschland",
profile=peters_profile)
db.session.add(peters_address)
2021-06-27 13:38:16 +02:00
peters_python_skill = ProfileSkill(profile=peters_profile, skill_id=3, level=3)
2021-06-26 10:51:39 +02:00
db.session.add(peters_python_skill)
2021-06-27 13:38:16 +02:00
peters_php_skill = ProfileSkill(profile=peters_profile, skill_id=1, level=5)
db.session.add(peters_php_skill)
2021-07-05 20:39:49 +02:00
peters_python_searchtopic = ProfileSearchtopic(profile=peters_profile, skill_id=3)
db.session.add(peters_python_searchtopic)
peters_php_searchtopic = ProfileSearchtopic(profile=peters_profile, skill_id=1)
db.session.add(peters_php_searchtopic)
2021-06-27 13:38:16 +02:00
peter_de = ProfileLanguage(profile=peters_profile, language_id="de", level=5)
2021-06-26 10:51:39 +02:00
db.session.add(peter_de)
2021-06-27 13:38:16 +02:00
peter_fr = ProfileLanguage(profile=peters_profile, language_id="fr", level=3)
2021-06-26 10:51:39 +02:00
db.session.add(peter_fr)
2021-08-16 22:41:03 +02:00
seed_user("klaus")
seed_user("dirtydieter",
visible=True,
volunteerwork="Müll sammeln",
availability_status=True,
availability_hours_per_week=24,
availability_text="Nur Nachts!",
2021-08-16 22:41:03 +02:00
freetext="1001010010111!!!",
skills=[(Skill.skill_id_php, 5)])
2021-09-20 21:07:48 +02:00
all_skills = Skill.query.all()
all_profile_skills = []
for skill in all_skills:
all_profile_skills.append((skill.id, 3))
seed_user("jutta", visible=True, languages=[("fr", 5)], skills=all_profile_skills)
2021-08-16 22:41:03 +02:00
seed_user("giesela", visible=True, skills=[(Skill.skill_id_mysql, 3), (Skill.skill_id_postgresql, 5)])
seed_user("bertha", visible=False, skills=[(Skill.skill_id_sqlite, 3), (Skill.skill_id_postgresql, 5)])
2021-09-20 21:07:48 +02:00
2021-08-16 22:41:03 +02:00
seed_user("monique", visible=True, languages=[("fr", 4)])
2021-06-26 10:51:39 +02:00
db.session.commit()