Skip to content
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

todo-ViewSet #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/todo_core/to_do/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from to_do.models import ToDo


class ToDoListSerializer(ModelSerializer):
class ToDoSerializer(ModelSerializer):
class Meta:
model = ToDo
fields = ['id', 'title', 'choice']
13 changes: 7 additions & 6 deletions src/todo_core/to_do/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from django.urls import path
from rest_framework.routers import DefaultRouter

from to_do.views import ToDoListCreateAPI, ToDoDetailAPI
from to_do.views import ToDoViewSet

urlpatterns = [
path('', ToDoListCreateAPI.as_view()),
path('/<int:todo_id>', ToDoDetailAPI.as_view())
]
router = DefaultRouter(trailing_slash=False)

router.register("", ToDoViewSet, basename='user')

urlpatterns = router.urls
49 changes: 15 additions & 34 deletions src/todo_core/to_do/views.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,21 @@
from rest_framework.generics import get_object_or_404
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet
from to_do.models import ToDo
from to_do.serializer import ToDoListSerializer
from to_do.serializer import ToDoSerializer


class ToDoListCreateAPI(APIView):
def get(self, request):
todo = ToDo.objects.all()
return_data = ToDoListSerializer(todo, many=True).data
return Response(return_data)
class ToDoViewSet(ModelViewSet):
queryset = ToDo.objects.all()
serializer_class = ToDoSerializer

def post(self, request):
serializer = ToDoListSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
todo = ToDo.objects.create(
title=serializer.validated_data['title'],
)

return_data = ToDoListSerializer(todo).data
return Response(return_data, status=201)
todolist = ToDoViewSet.as_view({
'get': 'list',
'post': 'create',
})


class ToDoDetailAPI(APIView):
def get(self, request, todo_id):
todo = get_object_or_404(ToDo, id=todo_id)
return_data = ToDoListSerializer(todo).data
return Response(return_data, status=200)

def patch(self, request, todo_id):
todo = get_object_or_404(ToDo, id=todo_id)
serializer = ToDoListSerializer(instance=todo, data=request.data, partial=True)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(data=serializer.data)

def delete(self, request, todo_id):
get_object_or_404(ToDo, id=todo_id).delete()
return Response(status=204)
todo_detail = ToDoViewSet.as_view({
'get': 'retrieve',
'put': 'update',
'patch': 'partial_update',
'delete': 'destroy',
})
6 changes: 4 additions & 2 deletions src/todo_core/to_do_server/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
from django.urls import path
from to_do import views

urlpatterns = [
path('admin/', admin.site.urls),
path('todo', include('to_do.urls'))
path('todo/', views.todolist),
path('todo/<int:pk>', views.todo_detail),
]