pronounscc/api_v2/views/users.py
2023-09-22 01:37:57 +02:00

38 lines
878 B
Python

from ninja import Router, Field, Schema
from ninja.errors import HttpError
from pronounscc.models import User
router = Router()
class UserOut(Schema):
id: str = Field(..., alias="uid")
username: str
display_name: str = Field(..., alias="userprofile.display_name")
bio: str = Field(..., alias="userprofile.bio")
@router.get("/@me")
def get_me(request):
return {"data": "me endpoint, eventually"}
@router.get("/{user_ref}", response=UserOut)
def get_user(request, user_ref: str):
try:
user_id = int(user_ref)
try:
user = User.objects.get(pk=user_id)
return user
except User.DoesNotExist:
pass
except ValueError:
pass
try:
user = User.objects.get(username=user_ref)
return user
except User.DoesNotExist:
raise HttpError(404, "User not found")