update
This commit is contained in:
109
AzA march 2026/workforce_planner/seed.py
Normal file
109
AzA march 2026/workforce_planner/seed.py
Normal file
@@ -0,0 +1,109 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Seed-Script: erstellt Datenbank-Tabellen und fügt Test-Mitarbeiter ein.
|
||||
|
||||
python -m workforce_planner.seed
|
||||
"""
|
||||
|
||||
from .database import init_db, SessionLocal
|
||||
from .core.models import Employee, Practice, BalanceAccount
|
||||
from .core.enums import EmployeeRole
|
||||
from .api.auth import hash_password
|
||||
|
||||
|
||||
def seed():
|
||||
print("Erstelle Tabellen ...")
|
||||
init_db()
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
if db.query(Practice).count() > 0:
|
||||
print("Datenbank enthält bereits Daten – überspringe Seed.")
|
||||
return
|
||||
|
||||
praxis = Practice(
|
||||
id="praxis_1",
|
||||
name="Praxis Muster",
|
||||
address="Musterstrasse 1, 8000 Zürich",
|
||||
phone="+41 44 123 45 67",
|
||||
)
|
||||
db.add(praxis)
|
||||
db.flush()
|
||||
|
||||
employees = [
|
||||
Employee(
|
||||
id="admin_1",
|
||||
practice_id=praxis.id,
|
||||
name="Dr. Admin",
|
||||
email="admin@praxis.ch",
|
||||
password_hash=hash_password("admin123"),
|
||||
role=EmployeeRole.ADMIN,
|
||||
vacation_days_per_year=25,
|
||||
),
|
||||
Employee(
|
||||
id="manager_1",
|
||||
practice_id=praxis.id,
|
||||
name="Anna Meier",
|
||||
email="anna@praxis.ch",
|
||||
password_hash=hash_password("test123"),
|
||||
role=EmployeeRole.MANAGER,
|
||||
department="Leitung",
|
||||
vacation_days_per_year=25,
|
||||
),
|
||||
Employee(
|
||||
id="emp_1",
|
||||
practice_id=praxis.id,
|
||||
name="Stefan Müller",
|
||||
email="stefan@praxis.ch",
|
||||
password_hash=hash_password("test123"),
|
||||
role=EmployeeRole.EMPLOYEE,
|
||||
department="MPA",
|
||||
vacation_days_per_year=25,
|
||||
),
|
||||
Employee(
|
||||
id="emp_2",
|
||||
practice_id=praxis.id,
|
||||
name="Lisa Weber",
|
||||
email="lisa@praxis.ch",
|
||||
password_hash=hash_password("test123"),
|
||||
role=EmployeeRole.EMPLOYEE,
|
||||
department="MPA",
|
||||
vacation_days_per_year=20,
|
||||
),
|
||||
Employee(
|
||||
id="emp_3",
|
||||
practice_id=praxis.id,
|
||||
name="Marco Brunner",
|
||||
email="marco@praxis.ch",
|
||||
password_hash=hash_password("test123"),
|
||||
role=EmployeeRole.EMPLOYEE,
|
||||
department="Labor",
|
||||
vacation_days_per_year=25,
|
||||
),
|
||||
]
|
||||
|
||||
for emp in employees:
|
||||
db.add(emp)
|
||||
db.flush()
|
||||
|
||||
import datetime
|
||||
year = datetime.date.today().year
|
||||
for emp in employees:
|
||||
ba = BalanceAccount(
|
||||
employee_id=emp.id,
|
||||
year=year,
|
||||
yearly_quota=emp.vacation_days_per_year,
|
||||
)
|
||||
db.add(ba)
|
||||
|
||||
db.commit()
|
||||
print(f"Seed erfolgreich: 1 Praxis, {len(employees)} Mitarbeiter, Jahr {year}")
|
||||
for emp in employees:
|
||||
print(f" {emp.role.value:10s} {emp.name:20s} {emp.email} (Passwort: {'admin123' if emp.role == EmployeeRole.ADMIN else 'test123'})")
|
||||
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
seed()
|
||||
Reference in New Issue
Block a user