forked from LondheShubham153/react_django_demo_app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
64 lines (45 loc) · 1.37 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from django.shortcuts import render
from django.http import JsonResponse
from rest_framework.decorators import api_view
from rest_framework.response import Response
from .serializers import TaskSerializer
from .models import Task
# Create your views here.
@api_view(['GET'])
def apiOverview(request):
api_urls = {
'List':'/task-list/',
'Detail View':'/task-detail/<str:pk>/',
'Create':'/task-create/',
'Update':'/task-update/<str:pk>/',
'Delete':'/task-delete/<str:pk>/',
}
return Response(api_urls)
@api_view(['GET'])
def taskList(request):
tasks = Task.objects.all().order_by('-id')
serializer = TaskSerializer(tasks, many=True)
return Response(serializer.data)
@api_view(['GET'])
def taskDetail(request, pk):
tasks = Task.objects.get(id=pk)
serializer = TaskSerializer(tasks, many=False)
return Response(serializer.data)
@api_view(['POST'])
def taskCreate(request):
serializer = TaskSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
@api_view(['POST'])
def taskUpdate(request, pk):
task = Task.objects.get(id=pk)
serializer = TaskSerializer(instance=task, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
@api_view(['DELETE'])
def taskDelete(request, pk):
task = Task.objects.get(id=pk)
task.delete()
return Response('Item succsesfully delete!')