-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
56 lines (41 loc) · 1.82 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
from rest_framework.permissions import AllowAny
from rest_framework.generics import GenericAPIView
from rest_framework.exceptions import NotFound
from rest_framework.response import Response
from rest_framework.status import HTTP_200_OK
from .serializer import CategorySerializer
from .services import ctg_one, ctg_list
from recipe.models import Category
class CategoryView(GenericAPIView):
permission_classes = (AllowAny,)
serializer_class = CategorySerializer
def get_object(self, pk):
try:
root = Category.objects.get(pk=pk)
except:
raise NotFound('Narmalni id kirit bunaqa it yoq sanda')
return root
def get(self, requests, *args, **kwargs):
if 'pk' in kwargs and kwargs['pk']:
result = ctg_one(kwargs['pk'])
else:
result = ctg_list(requests)
return Response(result, status=HTTP_200_OK)
def post(self, requests, *args, **kwargs):
serializer = self.get_serializer(data=requests.data)
serializer.is_valid(raise_exception=True)
root = serializer.create(serializer.data)
response = ctg_one(root.pk)
return Response(response, status=HTTP_200_OK)
def put(self, requests, *args, **kwargs):
root = self.get_object(kwargs['pk'])
print(root, "keldi")
serializer = self.get_serializer(data=requests.data, instance=root)
print(serializer, "boshogriq keldi")
serializer.is_valid(raise_exception=True)
root = serializer.save()
response = ctg_one(root.pk)
return Response(response, status=HTTP_200_OK)
def delete(self, requests, *args, **kwargs):
self.get_object(kwargs['pk']).delete()
return Response({"success": "bu narsa o'chirildi"}, status=HTTP_200_OK)