권주희

setting the default function

...@@ -18,7 +18,3 @@ class ItemSerializer(serializers.ModelSerializer): ...@@ -18,7 +18,3 @@ class ItemSerializer(serializers.ModelSerializer):
18 model = Item 18 model = Item
19 fields = '__all__' 19 fields = '__all__'
20 20
21 -class SharedItemSerializer(serializers.ModelSerializer):
22 - class Meta:
23 - model = Item
24 - fields = '__all__'
......
...@@ -13,7 +13,7 @@ from rest_framework.decorators import action ...@@ -13,7 +13,7 @@ from rest_framework.decorators import action
13 from rest_framework.permissions import IsAuthenticated, AllowAny 13 from rest_framework.permissions import IsAuthenticated, AllowAny
14 14
15 from api.models import Item, SharedItem 15 from api.models import Item, SharedItem
16 -from api.serializers import UserSerializer,GroupSerializer,ItemSerializer,SharedItemSerializer 16 +from api.serializers import UserSerializer,GroupSerializer,ItemSerializer
17 from rest_framework import status 17 from rest_framework import status
18 from annoying.functions import get_object_or_None 18 from annoying.functions import get_object_or_None
19 19
...@@ -26,13 +26,16 @@ class UserViewSet(viewsets.ModelViewSet): ...@@ -26,13 +26,16 @@ class UserViewSet(viewsets.ModelViewSet):
26 permission_classes = [permissions.IsAuthenticated] 26 permission_classes = [permissions.IsAuthenticated]
27 27
28 28
29 -class ItemViewSet(viewsets.ModelViewSet): 29 +class ItemViewSet(viewsets.ViewSet):
30 30
31 queryset = Item.objects.all() 31 queryset = Item.objects.all()
32 serializer_class = ItemSerializer 32 serializer_class = ItemSerializer
33 permission_classes = [permissions.IsAuthenticatedOrReadOnly, permissions.AllowAny, 33 permission_classes = [permissions.IsAuthenticatedOrReadOnly, permissions.AllowAny,
34 #IsOwnerOrReadOnly 34 #IsOwnerOrReadOnly
35 ] 35 ]
36 + permission_classes_by_action = {'get': [permissions.AllowAny],
37 + 'destroy': [permissions.AllowAny]}
38 +
36 # url: items/search 39 # url: items/search
37 @action(methods=['GET'], detail=False, permission_classes=[AllowAny], url_path='search', url_name='search') 40 @action(methods=['GET'], detail=False, permission_classes=[AllowAny], url_path='search', url_name='search')
38 def search(self, request): 41 def search(self, request):
...@@ -44,12 +47,38 @@ class ItemViewSet(viewsets.ModelViewSet): ...@@ -44,12 +47,38 @@ class ItemViewSet(viewsets.ModelViewSet):
44 json_data = json.loads(data) 47 json_data = json.loads(data)
45 return Response({'data': {'list' : json_data}}, status=status.HTTP_200_OK) 48 return Response({'data': {'list' : json_data}}, status=status.HTTP_200_OK)
46 49
50 + # url: items/11/
51 + # 마지막 slash도 써주어야함
52 + def get(self, request, pk):
53 + print(pk)
54 + return Response({'message': "info complete"}, status=status.HTTP_200_OK)
55 +
56 + # url: items/11/
57 + # 마지막 slash도 써주어야함
58 + def destroy(self, request, pk):
59 + if request.method == 'DELETE':
60 + print(pk)
61 + # keyword = request.GET.get('keyword', '')
62 + # item_list = Item.objects.filter(name__icontains=keyword)
63 + #
64 + # data = serializers.serialize("json", item_list)
65 + # json_data = json.loads(data)
66 + # return Response({'message': "delete complete"}, status=status.HTTP_200_OK)
67 + return Response(status=status.HTTP_204_NO_CONTENT)
68 +
69 + def get_permissions(self):
70 + try:
71 + # return permission_classes depending on `action`
72 + return [permission() for permission in self.permission_classes_by_action[self.action]]
73 + except KeyError:
74 + # action is not set return default permission_classes
75 + return [permission() for permission in self.permission_classes]
47 76
48 77
49 class SharedItemViewSet(viewsets.ModelViewSet): 78 class SharedItemViewSet(viewsets.ModelViewSet):
50 79
51 queryset = SharedItem.objects.all() 80 queryset = SharedItem.objects.all()
52 - serializer_class = SharedItemSerializer 81 + # serializer_class = SharedItemSerializer
53 permission_classes = [permissions.IsAuthenticatedOrReadOnly, permissions.AllowAny, 82 permission_classes = [permissions.IsAuthenticatedOrReadOnly, permissions.AllowAny,
54 # IsOwnerOrReadOnly 83 # IsOwnerOrReadOnly
55 ] 84 ]
...@@ -76,3 +105,7 @@ class SharedItemViewSet(viewsets.ModelViewSet): ...@@ -76,3 +105,7 @@ class SharedItemViewSet(viewsets.ModelViewSet):
76 105
77 json_data = json.loads(item_json) 106 json_data = json.loads(item_json)
78 return Response({"shared": sharedfile.created_time , 'data': json_data}, status=status.HTTP_200_OK) 107 return Response({"shared": sharedfile.created_time , 'data': json_data}, status=status.HTTP_200_OK)
108 +
109 +item = ItemViewSet.as_view({
110 + 'delete': 'destroy',
111 +})
...\ No newline at end of file ...\ No newline at end of file
......