mirror of
https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git
synced 2025-08-14 00:25:46 +02:00
Починка админки полностью22223322f2ва
This commit is contained in:
19
backend/app/schemas/employee.py
Normal file
19
backend/app/schemas/employee.py
Normal file
@@ -0,0 +1,19 @@
|
||||
"""Employee schemas"""
|
||||
from pydantic import BaseModel
|
||||
from datetime import datetime
|
||||
|
||||
class EmployeeBase(BaseModel):
|
||||
first_name: str
|
||||
last_name: str
|
||||
department: str
|
||||
office: str
|
||||
|
||||
class EmployeeCreate(EmployeeBase):
|
||||
password: str
|
||||
|
||||
class Employee(EmployeeBase):
|
||||
id: int
|
||||
created_at: datetime
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
@@ -7,18 +7,20 @@ 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 add_employee():
|
||||
"""Add a new employee to the database"""
|
||||
db = SessionLocal()
|
||||
try:
|
||||
# Create employee data
|
||||
employee = EmployeeCreate(
|
||||
first_name="", # Имя не указано в требованиях
|
||||
first_name="Сотрудник",
|
||||
last_name="Лесников",
|
||||
department="general", # Общий отдел
|
||||
department="general",
|
||||
office="101",
|
||||
password="1111"
|
||||
password="111111"
|
||||
)
|
||||
|
||||
# Check if employee already exists
|
||||
@@ -27,13 +29,17 @@ def add_employee():
|
||||
print(f"Сотрудник {employee.last_name} уже существует")
|
||||
return
|
||||
|
||||
# Hash password before saving
|
||||
employee_dict = employee.model_dump()
|
||||
employee_dict["password"] = get_password_hash(employee_dict["password"])
|
||||
|
||||
# Create new employee
|
||||
db_employee = employees.create_employee(db, employee)
|
||||
db_employee = employees.create_employee(db, employee_dict)
|
||||
print(f"Создан сотрудник: {db_employee.last_name} (ID: {db_employee.id})")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Ошибка при создании сотрудника: {e}")
|
||||
raise e
|
||||
raise
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user