Major overhaul to the Notes/Issues. Minor tweaks to the UI. Added Profile photos
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from fastapi import APIRouter, Depends, Query, UploadFile, File
|
||||
from typing import Optional, List
|
||||
from auth.models import TokenPayload
|
||||
from auth.dependencies import require_permission
|
||||
@@ -93,3 +93,15 @@ async def unassign_device(
|
||||
_user: TokenPayload = Depends(require_permission("app_users", "edit")),
|
||||
):
|
||||
return service.unassign_device(user_id, device_id)
|
||||
|
||||
|
||||
@router.post("/{user_id}/photo")
|
||||
async def upload_photo(
|
||||
user_id: str,
|
||||
file: UploadFile = File(...),
|
||||
_user: TokenPayload = Depends(require_permission("app_users", "edit")),
|
||||
):
|
||||
contents = await file.read()
|
||||
content_type = file.content_type or "image/jpeg"
|
||||
url = service.upload_photo(user_id, contents, file.filename, content_type)
|
||||
return {"photo_url": url}
|
||||
|
||||
@@ -2,7 +2,7 @@ from datetime import datetime
|
||||
|
||||
from google.cloud.firestore_v1 import DocumentReference
|
||||
|
||||
from shared.firebase import get_db
|
||||
from shared.firebase import get_db, get_bucket
|
||||
from shared.exceptions import NotFoundError
|
||||
from users.models import UserCreate, UserUpdate, UserInDB
|
||||
|
||||
@@ -250,3 +250,29 @@ def get_user_devices(user_doc_id: str) -> list[dict]:
|
||||
break
|
||||
|
||||
return devices
|
||||
|
||||
|
||||
def upload_photo(user_doc_id: str, file_bytes: bytes, filename: str, content_type: str) -> str:
|
||||
"""Upload a profile photo to Firebase Storage and update the user's photo_url."""
|
||||
db = get_db()
|
||||
bucket = get_bucket()
|
||||
if not bucket:
|
||||
raise RuntimeError("Firebase Storage not initialized")
|
||||
|
||||
# Verify user exists
|
||||
doc_ref = db.collection(COLLECTION).document(user_doc_id)
|
||||
doc = doc_ref.get()
|
||||
if not doc.exists:
|
||||
raise NotFoundError("User")
|
||||
|
||||
ext = filename.rsplit(".", 1)[-1] if "." in filename else "jpg"
|
||||
storage_path = f"users/{user_doc_id}/uploads/profile.{ext}"
|
||||
|
||||
blob = bucket.blob(storage_path)
|
||||
blob.upload_from_string(file_bytes, content_type=content_type)
|
||||
blob.make_public()
|
||||
|
||||
photo_url = blob.public_url
|
||||
doc_ref.update({"photo_url": photo_url})
|
||||
|
||||
return photo_url
|
||||
|
||||
Reference in New Issue
Block a user