
- Setup project structure and FastAPI application - Configure SQLite database with SQLAlchemy ORM - Setup Alembic for database migrations - Implement user authentication with JWT - Create task models and CRUD operations - Implement task assignment functionality - Add detailed API documentation - Create comprehensive README with usage instructions - Lint code with Ruff
150 lines
3.8 KiB
Python
150 lines
3.8 KiB
Python
from typing import Any, List
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy import and_
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.deps import get_current_user, get_db
|
|
from app.models.task import Task
|
|
from app.models.user import User
|
|
from app.schemas.user import User as UserSchema
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/{task_id}/assignees", response_model=List[UserSchema])
|
|
def read_task_assignees(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
task_id: int,
|
|
current_user: User = Depends(get_current_user),
|
|
) -> Any:
|
|
"""
|
|
Get all assignees for a specific task.
|
|
"""
|
|
task = db.query(Task).filter(
|
|
and_(
|
|
Task.id == task_id,
|
|
not Task.is_deleted
|
|
)
|
|
).first()
|
|
|
|
if not task:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Task not found"
|
|
)
|
|
|
|
# Check if user has access to task
|
|
if task.owner_id != current_user.id and current_user not in task.assignees:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="You don't have access to this task"
|
|
)
|
|
|
|
return task.assignees
|
|
|
|
|
|
@router.post("/{task_id}/assignees/{user_id}", status_code=status.HTTP_201_CREATED)
|
|
def assign_user_to_task(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
task_id: int,
|
|
user_id: int,
|
|
current_user: User = Depends(get_current_user),
|
|
) -> Any:
|
|
"""
|
|
Assign a user to a task.
|
|
"""
|
|
# Get task
|
|
task = db.query(Task).filter(
|
|
and_(
|
|
Task.id == task_id,
|
|
not Task.is_deleted
|
|
)
|
|
).first()
|
|
|
|
if not task:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Task not found"
|
|
)
|
|
|
|
# Only task owner can assign users
|
|
if task.owner_id != current_user.id:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Only the task owner can assign users"
|
|
)
|
|
|
|
# Get user to assign
|
|
user = db.query(User).filter(User.id == user_id).first()
|
|
|
|
if not user:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="User not found"
|
|
)
|
|
|
|
# Check if user is already assigned
|
|
if user in task.assignees:
|
|
return {"message": "User is already assigned to this task"}
|
|
|
|
# Assign user to task
|
|
task.assignees.append(user)
|
|
db.commit()
|
|
|
|
return {"message": "User assigned to task successfully"}
|
|
|
|
|
|
@router.delete("/{task_id}/assignees/{user_id}", status_code=status.HTTP_200_OK)
|
|
def remove_user_from_task(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
task_id: int,
|
|
user_id: int,
|
|
current_user: User = Depends(get_current_user),
|
|
) -> Any:
|
|
"""
|
|
Remove a user from a task.
|
|
"""
|
|
# Get task
|
|
task = db.query(Task).filter(
|
|
and_(
|
|
Task.id == task_id,
|
|
not Task.is_deleted
|
|
)
|
|
).first()
|
|
|
|
if not task:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Task not found"
|
|
)
|
|
|
|
# Only task owner can remove assignments
|
|
if task.owner_id != current_user.id:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Only the task owner can remove assignments"
|
|
)
|
|
|
|
# Get user to remove
|
|
user = db.query(User).filter(User.id == user_id).first()
|
|
|
|
if not user:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="User not found"
|
|
)
|
|
|
|
# Check if user is assigned
|
|
if user not in task.assignees:
|
|
return {"message": "User is not assigned to this task"}
|
|
|
|
# Remove user from task
|
|
task.assignees.remove(user)
|
|
db.commit()
|
|
|
|
return {"message": "User removed from task successfully"}
|