This repository has been archived by the owner on Jul 2, 2024. It is now read-only.
generated from edu-python-course/django-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Tasks API] Implement base API views (#106)
- Loading branch information
Showing
9 changed files
with
218 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
""" | ||
Tasks application API resources | ||
""" | ||
|
||
import uuid | ||
|
||
from rest_framework import status | ||
from rest_framework.decorators import api_view | ||
from rest_framework.request import Request | ||
from rest_framework.response import Response | ||
|
||
from tasks.models import TaskModel | ||
from tasks.serializers import TaskModelReadSerializer, TaskModelWriteSerializer | ||
|
||
|
||
@api_view(["GET", "POST"]) | ||
def tasks_list(request: Request) -> Response: | ||
""" | ||
Handle requests to task list endpoint | ||
""" | ||
|
||
if request.method == "GET": | ||
qs = TaskModel.objects.all() | ||
serializer = TaskModelReadSerializer(qs, many=True) | ||
|
||
return Response(serializer.data) | ||
|
||
serializer = TaskModelWriteSerializer(data=request.data) | ||
if serializer.is_valid(): | ||
serializer.save() | ||
|
||
return Response(serializer.data, status=status.HTTP_201_CREATED) | ||
|
||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | ||
|
||
|
||
@api_view(["GET", "PUT", "PATCH", "DELETE"]) | ||
def tasks_detail(request: Request, pk: uuid.UUID) -> Response: | ||
""" | ||
Handle requests to task detail endpoint | ||
""" | ||
|
||
try: | ||
instance = TaskModel.objects.get(pk=pk) | ||
except TaskModel.DoesNotExist: | ||
return Response(status=status.HTTP_404_NOT_FOUND) | ||
|
||
if request.method == "GET": | ||
serializer = TaskModelReadSerializer(instance) | ||
|
||
return Response(serializer.data) # HTTP_200_OK | ||
|
||
if request.method == "DELETE": | ||
instance.delete() | ||
|
||
return Response(status=status.HTTP_204_NO_CONTENT) | ||
|
||
if request.method == "PATCH": | ||
serializer = TaskModelWriteSerializer(instance, request.data, | ||
partial=True) | ||
else: # PUT | ||
serializer = TaskModelWriteSerializer(instance, request.data) | ||
|
||
if serializer.is_valid(): | ||
serializer.save() | ||
|
||
return Response(serializer.data, status=status.HTTP_202_ACCEPTED) | ||
|
||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) |
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,14 @@ | ||
""" | ||
Tasks application API routes | ||
""" | ||
|
||
from django.urls import path | ||
|
||
from tasks import resources | ||
|
||
app_name = "tasks" | ||
urlpatterns = [ | ||
path("tasks/", resources.tasks_list, name="tasks-list"), | ||
path("tasks/<uuid:pk>/", resources.tasks_detail, name="tasks-detail") | ||
] |
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
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,63 @@ | ||
from http import HTTPStatus | ||
|
||
from django.contrib.auth import get_user_model | ||
from rest_framework import test | ||
from rest_framework.reverse import reverse_lazy | ||
|
||
from tasks.models import TaskModel | ||
|
||
UserModel = get_user_model() | ||
|
||
|
||
class TestTasksListAPI(test.APITestCase): | ||
fixtures = ["users"] | ||
reporter: UserModel = None | ||
assignee: UserModel = None | ||
|
||
@classmethod | ||
def setUpTestData(cls) -> None: | ||
cls.reporter = UserModel.objects.get(pk=2) | ||
cls.assignee = UserModel.objects.get(pk=3) | ||
cls.data = { | ||
"summary": "Test tasks list API", | ||
"reporter": cls.reporter.pk, | ||
} | ||
|
||
def setUp(self) -> None: | ||
self.client = test.APIClient() | ||
self.instance = TaskModel.objects.create( | ||
summary="Existing task", | ||
reporter=self.reporter, | ||
) | ||
self.url_path = reverse_lazy( | ||
"api:tasks-detail", | ||
args=(self.instance.pk,) | ||
) | ||
self.url_404 = reverse_lazy( | ||
"api:tasks-detail", | ||
args=("dc9dcb0a-e20b-404e-a4e6-9627f7bc118d",) | ||
) | ||
|
||
def test_get(self): | ||
response = self.client.get(self.url_path) | ||
self.assertEqual(response.status_code, HTTPStatus.OK) | ||
|
||
def test_put(self): | ||
response = self.client.put(self.url_path, self.data) | ||
self.assertEqual(response.status_code, HTTPStatus.ACCEPTED) | ||
|
||
def test_patch(self): | ||
response = self.client.patch(self.url_path, {"completed": False}) | ||
self.assertEqual(response.status_code, HTTPStatus.ACCEPTED) | ||
|
||
def test_delete(self): | ||
response = self.client.delete(self.url_path) | ||
self.assertEqual(response.status_code, HTTPStatus.NO_CONTENT) | ||
|
||
def test_not_found(self): | ||
response = self.client.get(self.url_404) | ||
self.assertEqual(response.status_code, HTTPStatus.NOT_FOUND) | ||
|
||
def test_bad_request(self): | ||
response = self.client.put(self.url_path, {}) | ||
self.assertEqual(response.status_code, HTTPStatus.BAD_REQUEST) |
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,43 @@ | ||
from http import HTTPStatus | ||
|
||
from django.contrib.auth import get_user_model | ||
from rest_framework import test | ||
from rest_framework.reverse import reverse_lazy | ||
|
||
from tasks.models import TaskModel | ||
|
||
UserModel = get_user_model() | ||
|
||
|
||
class TestTasksListAPI(test.APITestCase): | ||
fixtures = ["users", "tasks"] | ||
reporter: UserModel = None | ||
|
||
@classmethod | ||
def setUpTestData(cls) -> None: | ||
cls.url_path = reverse_lazy("api:tasks-list") | ||
cls.reporter = UserModel.objects.get(pk=2) | ||
cls.data = { | ||
"summary": "Test tasks list API", | ||
"reporter": cls.reporter.pk, | ||
} | ||
|
||
def setUp(self) -> None: | ||
self.client = test.APIClient() | ||
|
||
def test_get(self): | ||
response = self.client.get(self.url_path) | ||
|
||
self.assertEqual(response.status_code, HTTPStatus.OK) | ||
|
||
def test_post(self): | ||
response = self.client.post(self.url_path, self.data) | ||
|
||
qs = TaskModel.objects.filter(summary=self.data["summary"]) | ||
self.assertEqual(response.status_code, HTTPStatus.CREATED) | ||
self.assertTrue(qs.exists()) | ||
|
||
def test_bad_request(self): | ||
response = self.client.post(self.url_path, {}) | ||
|
||
self.assertEqual(response.status_code, HTTPStatus.BAD_REQUEST) |
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