linkedin2/routers/product.py

95 lines
3.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
from pydantic import BaseModel
from sqlalchemy.future import select
from sqlalchemy.orm import joinedload
from sqlalchemy.ext.asyncio import AsyncSession
from routers.auth import get_current_user
from model.database import get_async_session, Job, Client, AppliedJob, User
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
result = await session.execute(
select(AppliedJob)
.options(
joinedload(AppliedJob.client), # Подгружаем клиента
joinedload(AppliedJob.job), # Подгружаем работу
joinedload(AppliedJob.users) # Подгружаем пользователя
)
)
applied_jobs = result.scalars().all()
result1 = await session.execute(select(User))
users = result1.scalars().all()
return templates.TemplateResponse("product.html", {"request": request, "size": size,
"jobs": applied_jobs, "role": username["role"],
"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}