initial commit

This commit is contained in:
kiaro37
2026-01-26 13:33:54 +03:00
commit 21cb493267
21 changed files with 1028 additions and 0 deletions

70
app/api/admin.py Normal file
View File

@@ -0,0 +1,70 @@
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
import os
import shutil
from ..db import get_db
from ..models import Grave, GraveStatus, User, GraveHistory
from ..schemas import GraveOut, RejectRequest
from ..dependencies import require_admin, get_current_user
from ..config import settings
from ..notifications import send_push_notification
router = APIRouter(dependencies=[Depends(require_admin)])
@router.post("/graves/{grave_id}/approve", response_model=GraveOut)
def approve_grave(grave_id: int, db: Session = Depends(get_db), user=Depends(get_current_user)):
grave = db.query(Grave).get(grave_id)
if not grave:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Not found")
grave.status = GraveStatus.APPROVED
# Move file from temp to permanent location
if grave.temp_photo_path and os.path.exists(grave.temp_photo_path):
os.makedirs(settings.uploads_graves_dir, exist_ok=True)
filename = os.path.basename(grave.temp_photo_path)
final_path = os.path.join(settings.uploads_graves_dir, filename)
shutil.move(grave.temp_photo_path, final_path)
grave.temp_photo_path = None
grave.photo_url = f"/static/graves/{filename}"
db.commit()
db.refresh(grave)
db.add(GraveHistory(grave_id=grave.id, action="approved", actor_id=user.id))
db.commit()
# Notify submitter if FCM token is present
if grave.created_by:
user = db.query(User).get(grave.created_by)
if user and user.fcm_token:
send_push_notification(
user.fcm_token,
title="Метка утверждена",
body=f"'{grave.full_name}' утверждена модератором",
data={"grave_id": grave.id, "status": grave.status.value},
)
return grave
@router.post("/graves/{grave_id}/reject", response_model=GraveOut)
def reject_grave(grave_id: int, payload: RejectRequest, db: Session = Depends(get_db), user=Depends(get_current_user)):
grave = db.query(Grave).get(grave_id)
if not grave:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Not found")
grave.status = GraveStatus.REJECTED
db.add(GraveHistory(grave_id=grave.id, action="rejected", changes={"comment": payload.comment}, actor_id=user.id))
# store comment in history
# actor id is not available via dependency here; rely on require_admin protecting route
# We can extend later to include actor id if needed
# delete temp file if any
if grave.temp_photo_path and os.path.exists(grave.temp_photo_path):
os.remove(grave.temp_photo_path)
grave.temp_photo_path = None
db.commit()
db.refresh(grave)
return grave