1
0
mirror of https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git synced 2025-08-14 00:25:46 +02:00

Починка админки полностью22223322f2ва

This commit is contained in:
MoonTestUse1
2025-01-01 23:50:15 +06:00
parent b2395ad252
commit d3a6440844
3 changed files with 55 additions and 20 deletions

View File

@@ -1,32 +1,42 @@
"""Database initialization script that runs on container startup"""
"""Database initialization script"""
import sys
from pathlib import Path
# Add the parent directory to sys.path
sys.path.append(str(Path(__file__).resolve().parents[1]))
from app.database import SessionLocal
from app.crud import employees
from app.models.employee import EmployeeCreate
from app.schemas.employee import EmployeeCreate
from app.utils.auth import get_password_hash
def init_db():
"""Initialize database with default data"""
db = SessionLocal()
try:
# Create default employee with valid password (min 6 chars)
# Create default admin employee
employee = EmployeeCreate(
first_name="Сотрудник",
last_name="Лесников",
department="general",
first_name="Admin",
last_name="Admin",
department="IT",
office="101",
password="111111" # Changed to meet 6 character minimum
password="admin66"
)
existing = employees.get_employee_by_lastname(db, employee.last_name)
if not existing:
employees.create_employee(db, employee)
print("Default employee created successfully")
else:
print("Default employee already exists")
# Check if admin already exists
existing_employee = employees.get_employee_by_lastname(db, employee.last_name)
if existing_employee:
print("Admin already exists")
return
# Hash password before saving
employee_dict = employee.model_dump()
employee_dict["password"] = get_password_hash(employee_dict["password"])
# Create admin
db_employee = employees.create_employee(db, employee_dict)
print(f"Created admin employee with ID: {db_employee.id}")
except Exception as e:
print(f"Error initializing database: {e}")
raise