2025-02-17 18:18:14 +02:00
|
|
|
|
from fastapi import FastAPI, APIRouter, Depends, Request, HTTPException, Form
|
|
|
|
|
from fastapi.templating import Jinja2Templates
|
|
|
|
|
from fastapi.responses import RedirectResponse, HTMLResponse
|
|
|
|
|
from fastapi.responses import JSONResponse
|
|
|
|
|
import jwt
|
2025-02-17 21:53:15 +02:00
|
|
|
|
from pydantic import BaseModel
|
2025-02-17 18:18:14 +02:00
|
|
|
|
from sqlalchemy.future import select
|
2025-02-17 21:01:27 +02:00
|
|
|
|
from sqlalchemy.orm import joinedload
|
2025-02-17 18:18:14 +02:00
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from routers.auth import get_current_user
|
2025-02-17 21:53:15 +02:00
|
|
|
|
from model.database import get_async_session, Job, Client, AppliedJob, User
|
2025-02-17 18:18:14 +02:00
|
|
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
templates = Jinja2Templates(directory="templates")
|
|
|
|
|
|
|
|
|
|
@router.get("/product")
|
|
|
|
|
async def product(request: Request,
|
|
|
|
|
username: str = Depends(get_current_user),
|
|
|
|
|
session: AsyncSession = Depends(get_async_session)):
|
|
|
|
|
size = "Work"
|
|
|
|
|
username = username
|
|
|
|
|
|
2025-02-17 21:01:27 +02:00
|
|
|
|
result = await session.execute(
|
|
|
|
|
select(AppliedJob)
|
|
|
|
|
.options(
|
|
|
|
|
joinedload(AppliedJob.client), # Подгружаем клиента
|
|
|
|
|
joinedload(AppliedJob.job), # Подгружаем работу
|
|
|
|
|
joinedload(AppliedJob.users) # Подгружаем пользователя
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
applied_jobs = result.scalars().all()
|
|
|
|
|
|
|
|
|
|
|
2025-02-17 21:53:15 +02:00
|
|
|
|
result1 = await session.execute(select(User))
|
|
|
|
|
users = result1.scalars().all()
|
2025-02-17 18:18:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return templates.TemplateResponse("product.html", {"request": request, "size": size,
|
2025-02-17 21:01:27 +02:00
|
|
|
|
"jobs": applied_jobs, "role": username["role"],
|
2025-02-17 21:53:15 +02:00
|
|
|
|
"username": username['username'], "users": users,
|
|
|
|
|
"current_path": request.url.path })#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Pydantic модель запроса
|
|
|
|
|
class StatusUpdate(BaseModel):
|
|
|
|
|
job_id: int
|
|
|
|
|
status: str
|
|
|
|
|
|
|
|
|
|
@router.post("/update_status/")
|
|
|
|
|
async def update_status(data: StatusUpdate,
|
|
|
|
|
session: AsyncSession = Depends(get_async_session)):
|
|
|
|
|
job = await session.execute(select(AppliedJob).where(AppliedJob.id == data.job_id))
|
|
|
|
|
job = job.scalars().first()
|
|
|
|
|
|
|
|
|
|
if not job:
|
|
|
|
|
raise HTTPException(status_code=404, detail="Job not found")
|
|
|
|
|
|
|
|
|
|
job.status = data.status
|
|
|
|
|
await session.commit()
|
|
|
|
|
|
|
|
|
|
return {"message": "Статус обновлён", "job_id": job.id, "new_status": job.status}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AssigneeUpdate(BaseModel):
|
|
|
|
|
job_id: int
|
|
|
|
|
assignee_id: int
|
|
|
|
|
|
|
|
|
|
@router.post("/update_assignee/")
|
|
|
|
|
async def update_assignee(data: AssigneeUpdate,
|
|
|
|
|
session: AsyncSession = Depends(get_async_session)):
|
|
|
|
|
job = await session.execute(select(AppliedJob).where(AppliedJob.id == data.job_id))
|
|
|
|
|
job = job.scalars().first()
|
|
|
|
|
|
|
|
|
|
if not job:
|
|
|
|
|
raise HTTPException(status_code=404, detail="Job not found")
|
|
|
|
|
|
|
|
|
|
user = await session.execute(select(User).where(User.id == data.assignee_id))
|
|
|
|
|
user = user.scalars().first()
|
|
|
|
|
|
|
|
|
|
if not user:
|
|
|
|
|
raise HTTPException(status_code=404, detail="User not found")
|
|
|
|
|
|
|
|
|
|
job.assignee = data.assignee_id # 👈 Убрал _id, чтобы совпадало с моделью
|
|
|
|
|
await session.commit()
|
|
|
|
|
|
|
|
|
|
return {"message": "Исполнитель обновлён", "job_id": job.id, "new_assignee": job.assignee}
|