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"])
|
2024-01-11 20:48:13 +01:00
|
|
|
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"]))
|
|
|
|
|
|
|
|
|
2023-10-20 12:24:16 +02:00
|
|
|
def seed_user(auth_id,
|
|
|
|
nickname=None,
|
|
|
|
pronouns="",
|
|
|
|
visible=True,
|
2021-10-04 18:10:31 +02:00
|
|
|
volunteerwork="",
|
2021-10-04 19:25:24 +02:00
|
|
|
availability_status=False,
|
2021-10-04 18:10:31 +02:00
|
|
|
freetext="",
|
2021-10-04 19:25:24 +02:00
|
|
|
availability_text="",
|
2023-10-20 12:24:16 +02:00
|
|
|
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/")
|
|
|
|
|
2023-10-20 12:24:16 +02:00
|
|
|
user = User(auth_id=auth_id)
|
2021-08-16 22:41:03 +02:00
|
|
|
db.session.add(user)
|
|
|
|
|
|
|
|
profile = Profile(nickname=nickname,
|
2023-10-20 12:24:16 +02:00
|
|
|
pronouns=pronouns,
|
2021-08-16 22:41:03 +02:00
|
|
|
volunteerwork=volunteerwork,
|
2021-10-04 19:25:24 +02:00
|
|
|
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)
|
|
|
|
|
2023-10-20 12:24:16 +02:00
|
|
|
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)
|
|
|
|
|
2023-10-20 12:24:16 +02:00
|
|
|
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
|
|
|
|
2023-10-20 12:24:16 +02:00
|
|
|
db.session.add(profile)
|
2023-04-20 18:07:01 +02:00
|
|
|
|
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"
|
|
|
|
|
2021-07-12 21:13:12 +02:00
|
|
|
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"])
|
2024-01-11 20:48:13 +01:00
|
|
|
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"]))
|
|
|
|
|
2021-07-12 21:13:12 +02:00
|
|
|
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"]
|
2024-01-11 20:48:13 +01:00
|
|
|
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:
|
2023-10-20 12:24:16 +02:00
|
|
|
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):
|
2023-10-20 12:24:16 +02:00
|
|
|
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)
|
2023-10-20 12:24:16 +02:00
|
|
|
|
|
|
|
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")
|