ki-backend/ki/actions/seed.py

159 lines
5.5 KiB
Python
Raw 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
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"
2022-01-26 21:43:31 +01:00
app.logger.info("importing contacttypes")
2021-07-02 16:49:14 +02:00
with open(contacttypes_seed_file_path) as file:
csv_reader = csv.DictReader(file)
for contacttype in csv_reader:
id = int(contacttype["id"])
db_contacttype = db.session.get(ContactType, id)
2021-07-02 16:49:14 +02:00
if db_contacttype is None:
db.session.add(ContactType(id=int(contacttype["id"]), name=contacttype["name"]))
def seed_user(auth_id,
nickname=None,
pronouns="",
visible=True,
2021-10-04 18:10:31 +02:00
volunteerwork="",
availability_status=False,
2021-10-04 18:10:31 +02:00
freetext="",
availability_text="",
availability_hours_per_week=42,
skills=[],
searchtopics=[],
languages=[],
address=None,
contacts=[]):
if not nickname:
nickname = auth_id
2021-08-16 22:41:03 +02:00
app.logger.info(f"seeding {nickname} \\o/")
user = User(auth_id=auth_id)
2021-08-16 22:41:03 +02:00
db.session.add(user)
profile = Profile(nickname=nickname,
pronouns=pronouns,
2021-08-16 22:41:03 +02:00
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 skill_id in searchtopics:
searchtopic = ProfileSearchtopic(profile=profile, skill_id=skill_id)
db.session.add(searchtopic)
2021-08-16 22:41:03 +02:00
for language_data in languages:
language = ProfileLanguage(profile=profile, language_id=language_data[0], level=language_data[1])
db.session.add(language)
if address:
_address = Address(name=address[0],
street=address[1],
house_number=address[2],
additional=address[3],
postcode=address[4],
city=address[5],
country=address[6],
profile=profile)
db.session.add(_address)
for contact_data in contacts:
contact = Contact(profile=profile, contacttype_id=contact_data[0], content=contact_data[1])
db.session.add(contact)
2021-08-16 22:41:03 +02:00
db.session.add(profile)
2021-08-16 22:41:03 +02:00
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 = db.session.get(Skill, id)
2021-06-26 10:51:39 +02:00
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 = db.session.get(Language, id)
2021-06-26 10:51:39 +02:00
if db_language is None:
db.session.add(Language(id=iso["639-1"], name=iso["Sprache"]))
if dev:
seed_user("klaus", visible=False)
2021-06-26 10:51:39 +02:00
2022-01-16 16:35:23 +01:00
for i in range(1, 20):
seed_user(f"babsi{i}")
seed_user("peter",
nickname="peternichtlustig",
visible=False,
pronouns="Herr Dr. Dr.",
volunteerwork="Gartenverein",
availability_status=True,
availability_hours_per_week=42,
availability_text="Immer",
freetext="Ich mag Kaffee",
skills=[(3, 3), (1, 5)],
searchtopics=[3, 1],
languages=[("de", 5), ("fr", 3)],
address=("Peter Nichtlustig", "Waldweg", "23i", "Hinterhaus", "13337", "Bielefeld", "Deutschland"),
contacts=[(4, "@peter:wtf-eg.de"), (1, "peter@wtf-eg.de")])
seed_user("dirtydieter",
volunteerwork="Müll sammeln",
availability_status=True,
availability_hours_per_week=24,
availability_text="Nur Nachts!",
freetext="1001010010111!!!",
skills=[(1, 5)],
address=("Friedrich Witzig", "", "", "", "", "", ""))
2024-03-19 20:44:49 +01:00
# all_skills = [(skill.id, 3) for skill in Skill.query.all()] # query causes problems
# seed_user("jutta", languages=[("fr", 5)], skills=all_skills)
seed_user("giesela", skills=[(9, 3), (10, 5)])
seed_user("bertha", visible=False, skills=[(11, 3), (10, 5)])
seed_user("monique", languages=[("fr", 4)])
2024-01-23 20:11:35 +01:00
print("seeding done")
with app.app_context():
2024-03-19 20:44:49 +01:00
db.session.commit() # also problematic
2024-01-23 20:11:35 +01:00
print("commit done")