mirror of
https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git
synced 2025-08-14 00:25:46 +02:00
Fix init tables
This commit is contained in:
22
backend/app/db/init_db.py
Normal file
22
backend/app/db/init_db.py
Normal file
@@ -0,0 +1,22 @@
|
||||
"""Database initialization script"""
|
||||
from sqlalchemy.orm import Session
|
||||
from app.core.config import settings
|
||||
from app.models.employee import Employee
|
||||
from app.utils.auth import get_password_hash
|
||||
|
||||
def init_db(db: Session) -> None:
|
||||
"""Initialize database with default data"""
|
||||
# Создаем администратора по умолчанию
|
||||
admin = db.query(Employee).filter(Employee.email == settings.ADMIN_USERNAME).first()
|
||||
if not admin:
|
||||
admin = Employee(
|
||||
email=settings.ADMIN_USERNAME,
|
||||
full_name="System Administrator",
|
||||
hashed_password=get_password_hash(settings.ADMIN_PASSWORD),
|
||||
is_active=True,
|
||||
is_admin=True,
|
||||
department="Administration"
|
||||
)
|
||||
db.add(admin)
|
||||
db.commit()
|
||||
db.refresh(admin)
|
@@ -1,8 +1,21 @@
|
||||
"""Main application module"""
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from sqlalchemy.orm import Session
|
||||
from . import models
|
||||
from .routers import admin, employees, requests, auth, statistics
|
||||
from .database import engine, SessionLocal
|
||||
from .db.init_db import init_db
|
||||
|
||||
# Создаем таблицы
|
||||
models.Base.metadata.create_all(bind=engine)
|
||||
|
||||
# Инициализируем базу данных
|
||||
db = SessionLocal()
|
||||
try:
|
||||
init_db(db)
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
app = FastAPI(
|
||||
# Включаем автоматическое перенаправление со слэшем
|
||||
|
@@ -13,8 +13,8 @@ class Employee(Base):
|
||||
hashed_password = Column(String, nullable=False)
|
||||
is_active = Column(Boolean, default=True)
|
||||
is_admin = Column(Boolean, default=False)
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||
department = Column(String, nullable=True)
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||
|
||||
# Определяем отношение к Request
|
||||
requests = relationship("Request", back_populates="employee", cascade="all, delete-orphan")
|
@@ -1,12 +1,12 @@
|
||||
"""Employee schemas"""
|
||||
from pydantic import BaseModel, ConfigDict
|
||||
from pydantic import BaseModel, ConfigDict, EmailStr
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
class EmployeeBase(BaseModel):
|
||||
email: str
|
||||
email: EmailStr
|
||||
full_name: str
|
||||
department: str
|
||||
department: Optional[str] = None
|
||||
is_active: bool = True
|
||||
is_admin: bool = False
|
||||
|
||||
@@ -16,10 +16,9 @@ class EmployeeCreate(EmployeeBase):
|
||||
password: str
|
||||
|
||||
class EmployeeUpdate(BaseModel):
|
||||
email: Optional[str] = None
|
||||
email: Optional[EmailStr] = None
|
||||
full_name: Optional[str] = None
|
||||
department: Optional[str] = None
|
||||
password: Optional[str] = None
|
||||
is_active: Optional[bool] = None
|
||||
is_admin: Optional[bool] = None
|
||||
|
||||
|
@@ -3,7 +3,7 @@
|
||||
<div class="max-w-md w-full bg-white rounded-xl shadow-2xl p-8">
|
||||
<div class="text-center mb-8">
|
||||
<h2 class="text-3xl font-bold text-gray-900">
|
||||
Панель администратора 55
|
||||
Панель администратора
|
||||
</h2>
|
||||
<p class="mt-2 text-gray-600">
|
||||
Вход в систему управления
|
||||
|
Reference in New Issue
Block a user