Creating admin interface
This commit is contained in:
@@ -4,8 +4,10 @@ from sqlalchemy.orm import Session
|
||||
from datetime import date
|
||||
from app.api.deps import get_db, get_current_active_user
|
||||
from app.crud.crud_ppr import ppr as crud_ppr
|
||||
from app.schemas.ppr import PPR, PPRCreate, PPRUpdate, PPRStatus, PPRStatusUpdate
|
||||
from app.crud.crud_journal import journal as crud_journal
|
||||
from app.schemas.ppr import PPR, PPRCreate, PPRUpdate, PPRStatus, PPRStatusUpdate, Journal
|
||||
from app.models.ppr import User
|
||||
from app.core.utils import get_client_ip
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -37,7 +39,8 @@ async def create_ppr(
|
||||
current_user: User = Depends(get_current_active_user)
|
||||
):
|
||||
"""Create a new PPR record"""
|
||||
ppr = crud_ppr.create(db, obj_in=ppr_in, created_by=current_user.username)
|
||||
client_ip = get_client_ip(request)
|
||||
ppr = crud_ppr.create(db, obj_in=ppr_in, created_by=current_user.username, user_ip=client_ip)
|
||||
|
||||
# Send real-time update via WebSocket
|
||||
if hasattr(request.app.state, 'connection_manager'):
|
||||
@@ -85,7 +88,8 @@ async def update_ppr(
|
||||
detail="PPR record not found"
|
||||
)
|
||||
|
||||
ppr = crud_ppr.update(db, db_obj=db_ppr, obj_in=ppr_in)
|
||||
client_ip = get_client_ip(request)
|
||||
ppr = crud_ppr.update(db, db_obj=db_ppr, obj_in=ppr_in, user=current_user.username, user_ip=client_ip)
|
||||
|
||||
# Send real-time update
|
||||
if hasattr(request.app.state, 'connection_manager'):
|
||||
@@ -117,8 +121,8 @@ async def patch_ppr(
|
||||
detail="PPR record not found"
|
||||
)
|
||||
|
||||
# For PATCH, we only update fields that are explicitly provided (not None)
|
||||
ppr = crud_ppr.update(db, db_obj=db_ppr, obj_in=ppr_in)
|
||||
client_ip = get_client_ip(request)
|
||||
ppr = crud_ppr.update(db, db_obj=db_ppr, obj_in=ppr_in, user=current_user.username, user_ip=client_ip)
|
||||
|
||||
# Send real-time update
|
||||
if hasattr(request.app.state, 'connection_manager'):
|
||||
@@ -143,15 +147,20 @@ async def update_ppr_status(
|
||||
current_user: User = Depends(get_current_active_user)
|
||||
):
|
||||
"""Update PPR status (LANDED, DEPARTED, etc.)"""
|
||||
ppr = crud_ppr.update_status(db, ppr_id=ppr_id, status=status_update.status)
|
||||
client_ip = get_client_ip(request)
|
||||
ppr = crud_ppr.update_status(
|
||||
db,
|
||||
ppr_id=ppr_id,
|
||||
status=status_update.status,
|
||||
user=current_user.username,
|
||||
user_ip=client_ip
|
||||
)
|
||||
if not ppr:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="PPR record not found"
|
||||
)
|
||||
|
||||
# Log the status change (you might want to create a journal entry here)
|
||||
|
||||
# Send real-time update
|
||||
if hasattr(request.app.state, 'connection_manager'):
|
||||
await request.app.state.connection_manager.broadcast({
|
||||
@@ -175,7 +184,8 @@ async def delete_ppr(
|
||||
current_user: User = Depends(get_current_active_user)
|
||||
):
|
||||
"""Delete (soft delete) a PPR record"""
|
||||
ppr = crud_ppr.delete(db, ppr_id=ppr_id)
|
||||
client_ip = get_client_ip(request)
|
||||
ppr = crud_ppr.delete(db, ppr_id=ppr_id, user=current_user.username, user_ip=client_ip)
|
||||
if not ppr:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
@@ -192,4 +202,22 @@ async def delete_ppr(
|
||||
}
|
||||
})
|
||||
|
||||
return ppr
|
||||
return ppr
|
||||
|
||||
|
||||
@router.get("/{ppr_id}/journal", response_model=List[Journal])
|
||||
async def get_ppr_journal(
|
||||
ppr_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_active_user)
|
||||
):
|
||||
"""Get journal entries for a specific PPR"""
|
||||
# Verify PPR exists
|
||||
ppr = crud_ppr.get(db, ppr_id=ppr_id)
|
||||
if not ppr:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="PPR record not found"
|
||||
)
|
||||
|
||||
return crud_journal.get_by_ppr_id(db, ppr_id=ppr_id)
|
||||
Reference in New Issue
Block a user