54 lines
1.8 KiB
Python
Raw Normal View History

2025-11-09 23:29:07 +01:00
from fastapi import APIRouter, Depends
from ..auth import get_current_user
2025-11-13 11:47:50 +01:00
from ..models import User_Pydantic, User, File, Folder
2025-11-13 23:22:05 +01:00
from typing import Dict, Any
2025-11-09 23:29:07 +01:00
router = APIRouter(
prefix="/users",
tags=["users"],
)
2025-11-13 23:22:05 +01:00
2025-11-09 23:29:07 +01:00
@router.get("/me", response_model=User_Pydantic)
async def read_users_me(current_user: User = Depends(get_current_user)):
return await User_Pydantic.from_tortoise_orm(current_user)
2025-11-13 11:47:50 +01:00
2025-11-13 23:22:05 +01:00
2025-11-13 11:47:50 +01:00
@router.get("/me/export", response_model=Dict[str, Any])
async def export_my_data(current_user: User = Depends(get_current_user)):
"""
Exports all personal data associated with the current user.
Includes user profile, and metadata for all owned files and folders.
"""
user_data = await User_Pydantic.from_tortoise_orm(current_user)
files = await File.filter(owner=current_user).values(
"id", "name", "size", "created_at", "modified_at", "file_type", "parent_id"
)
folders = await Folder.filter(owner=current_user).values(
"id", "name", "created_at", "modified_at", "parent_id"
)
return {
"user_profile": user_data.dict(),
"files_metadata": files,
"folders_metadata": folders,
# In a more complete implementation, other data like activity logs,
# share information, etc., would also be included.
}
2025-11-13 23:22:05 +01:00
2025-11-13 11:47:50 +01:00
@router.delete("/me", status_code=204)
async def delete_my_account(current_user: User = Depends(get_current_user)):
"""
Deletes the current user's account and all associated data.
This includes all files and folders owned by the user.
"""
# Delete all files and folders owned by the user
await File.filter(owner=current_user).delete()
await Folder.filter(owner=current_user).delete()
# Finally, delete the user account
await current_user.delete()
return {}