This commit is contained in:
sam 2023-09-22 01:37:57 +02:00
commit 48703ea9fc
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
36 changed files with 936 additions and 0 deletions

0
api_v2/__init__.py Normal file
View file

3
api_v2/admin.py Normal file
View file

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

8
api_v2/api.py Normal file
View file

@ -0,0 +1,8 @@
from ninja import NinjaAPI
from .views.users import router as users_router
api = NinjaAPI()
api.add_router("/users/", users_router)

6
api_v2/apps.py Normal file
View file

@ -0,0 +1,6 @@
from django.apps import AppConfig
class ApiV2Config(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "api_v2"

View file

3
api_v2/models.py Normal file
View file

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

3
api_v2/tests.py Normal file
View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

5
api_v2/urls.py Normal file
View file

@ -0,0 +1,5 @@
from django.urls import path
from .api import api
urlpatterns = [path("/", api.urls)]

3
api_v2/views.py Normal file
View file

@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.

0
api_v2/views/__init__.py Normal file
View file

38
api_v2/views/users.py Normal file
View file

@ -0,0 +1,38 @@
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")