init
This commit is contained in:
commit
48703ea9fc
36 changed files with 936 additions and 0 deletions
0
api_v2/__init__.py
Normal file
0
api_v2/__init__.py
Normal file
3
api_v2/admin.py
Normal file
3
api_v2/admin.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
8
api_v2/api.py
Normal file
8
api_v2/api.py
Normal 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
6
api_v2/apps.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ApiV2Config(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "api_v2"
|
0
api_v2/migrations/__init__.py
Normal file
0
api_v2/migrations/__init__.py
Normal file
3
api_v2/models.py
Normal file
3
api_v2/models.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.db import models
|
||||
|
||||
# Create your models here.
|
3
api_v2/tests.py
Normal file
3
api_v2/tests.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
5
api_v2/urls.py
Normal file
5
api_v2/urls.py
Normal 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
3
api_v2/views.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
0
api_v2/views/__init__.py
Normal file
0
api_v2/views/__init__.py
Normal file
38
api_v2/views/users.py
Normal file
38
api_v2/views/users.py
Normal 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")
|
Loading…
Add table
Add a link
Reference in a new issue