-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate to django-ninja from DRF #1
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
from django.contrib.admin.views.decorators import staff_member_required | ||
from ninja import NinjaAPI | ||
|
||
from cookiecutter_django_ninja_example.users.api.views import users_router | ||
|
||
# Overrides to maintain consistency with the DRF url namespace in cookiecutter-django. | ||
api = NinjaAPI(urls_namespace="api") | ||
api = NinjaAPI(urls_namespace="api", docs_decorator=staff_member_required) | ||
|
||
api.add_router("/users/", users_router) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from django.urls import reverse | ||
from ninja import ModelSchema | ||
|
||
from cookiecutter_django_ninja_example.users.models import User | ||
|
||
|
||
class UserUpdateSchema(ModelSchema): | ||
class Meta: | ||
model = User | ||
fields = ["username", "name"] | ||
|
||
|
||
class UserSchema(ModelSchema): | ||
url: str | ||
|
||
@staticmethod | ||
def resolve_url(obj: User, context): | ||
request = context["request"] | ||
return request.build_absolute_uri( | ||
reverse("api:user-detail", kwargs={"username": obj.username}), | ||
) | ||
|
||
class Meta: | ||
model = User | ||
fields = ["username", "name"] |
13 changes: 0 additions & 13 deletions
13
cookiecutter_django_ninja_example/users/api/serializers.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,58 @@ | ||
from rest_framework import status | ||
from rest_framework.decorators import action | ||
from rest_framework.mixins import ListModelMixin | ||
from rest_framework.mixins import RetrieveModelMixin | ||
from rest_framework.mixins import UpdateModelMixin | ||
from rest_framework.response import Response | ||
from rest_framework.viewsets import GenericViewSet | ||
from ninja import Router | ||
from ninja.errors import HttpError | ||
|
||
from cookiecutter_django_ninja_example.users.models import User | ||
|
||
from .serializers import UserSerializer | ||
from .schemas import UserSchema | ||
from .schemas import UserUpdateSchema | ||
|
||
users_router = Router(tags=["users"]) | ||
|
||
class UserViewSet(RetrieveModelMixin, ListModelMixin, UpdateModelMixin, GenericViewSet): | ||
serializer_class = UserSerializer | ||
queryset = User.objects.all() | ||
lookup_field = "username" | ||
|
||
def get_queryset(self, *args, **kwargs): | ||
assert isinstance(self.request.user.id, int) | ||
return self.queryset.filter(id=self.request.user.id) | ||
# error handling in django-ninja is different from DRF | ||
# see https://django-ninja.dev/guides/errors/ | ||
# do we need to add a custom error handler function in template? | ||
|
||
@action(detail=False) | ||
def me(self, request): | ||
serializer = UserSerializer(request.user, context={"request": request}) | ||
return Response(status=status.HTTP_200_OK, data=serializer.data) | ||
|
||
@users_router.get("me/", response=UserSchema, url_name="user-me") | ||
def user_me(request): | ||
return request.user | ||
|
||
|
||
@users_router.get("/", response=list[UserSchema], url_name="user-list") | ||
def user_list(request): | ||
return User.objects.all() | ||
|
||
|
||
@users_router.get("{username}/", response=UserSchema, url_name="user-detail") | ||
def user_detail(request, username: str): | ||
try: | ||
return User.objects.get(username=username) | ||
except User.DoesNotExist as exc: | ||
raise HttpError(404, "User not found") from exc | ||
|
||
|
||
@users_router.put("{username}/", response=UserSchema, url_name="user-update") | ||
def update_user(request, username: str, payload: UserUpdateSchema): | ||
try: | ||
user = User.objects.get(username=username) | ||
user.username = payload.username | ||
user.name = payload.name | ||
user.save() | ||
except User.DoesNotExist as exc: | ||
raise HttpError(404, "User not found") from exc | ||
|
||
return user | ||
|
||
|
||
@users_router.patch("{username}/", response=UserSchema, url_name="user-partial-update") | ||
def partial_update_user(request, username: str, payload: UserUpdateSchema): | ||
try: | ||
user = User.objects.get(username=username) | ||
user.username = payload.username | ||
user.name = payload.name | ||
user.save() | ||
except User.DoesNotExist as exc: | ||
raise HttpError(404, "User not found") from exc | ||
|
||
return user |
File renamed without changes.
22 changes: 22 additions & 0 deletions
22
cookiecutter_django_ninja_example/users/tests/test_api_views.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from ninja.testing import TestClient | ||
|
||
from cookiecutter_django_ninja_example.users.api.views import users_router | ||
from cookiecutter_django_ninja_example.users.models import User | ||
|
||
|
||
class TestUserAPI: | ||
def test_user_me(self, user: User): | ||
client = TestClient(users_router) | ||
response = client.get("/me/", user=user) | ||
|
||
# there is no `status` in django-ninja, | ||
# do we need to add enum for status codes? | ||
assert response.status_code == 200 # noqa: PLR2004 | ||
|
||
assert response.json() == { | ||
"username": user.username, | ||
# note that drf creates urls with `testserver`, | ||
# but django-ninja uses `testlocation` | ||
"url": f"http://testlocation/api/users/{user.username}/", | ||
"name": user.name, | ||
} |
35 changes: 0 additions & 35 deletions
35
cookiecutter_django_ninja_example/users/tests/test_drf_views.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be simplified with
get_object_or_404(User, username=username)