권주희

add get api

1 -from django.contrib.auth.models import User, Group 1 +from django.contrib.auth.models import Group
2 from rest_framework import serializers 2 from rest_framework import serializers
3 -from api.models import Item, SharedItem 3 +from .models import Item, SharedItem,User
4 4
5 5
6 class UserSerializer(serializers.HyperlinkedModelSerializer): 6 class UserSerializer(serializers.HyperlinkedModelSerializer):
7 class Meta: 7 class Meta:
8 model = User 8 model = User
9 - fields = ['url', 'username', 'email', 'groups'] 9 + fields = '__all__'
10 10
11 class GroupSerializer(serializers.HyperlinkedModelSerializer): 11 class GroupSerializer(serializers.HyperlinkedModelSerializer):
12 class Meta: 12 class Meta:
......
...@@ -27,8 +27,11 @@ class UserViewSet(viewsets.ModelViewSet): ...@@ -27,8 +27,11 @@ class UserViewSet(viewsets.ModelViewSet):
27 """ 27 """
28 queryset = User.objects.all().order_by('-date_joined') 28 queryset = User.objects.all().order_by('-date_joined')
29 serializer_class = UserSerializer 29 serializer_class = UserSerializer
30 - permission_classes = [permissions.IsAuthenticated] 30 + permission_classes = [permissions.IsAuthenticatedOrReadOnly, permissions.AllowAny,
31 - 31 + # IsOwnerOrReadOnly
32 + ]
33 + permission_classes_by_action = {'get': [permissions.AllowAny],
34 + 'destroy': [permissions.AllowAny]}
32 @csrf_exempt 35 @csrf_exempt
33 @action(detail=False, methods=['POST'], permission_classes=[permissions.AllowAny], url_path='signup', url_name='singup') 36 @action(detail=False, methods=['POST'], permission_classes=[permissions.AllowAny], url_path='signup', url_name='singup')
34 def signup(self, request): 37 def signup(self, request):
...@@ -93,6 +96,22 @@ class UserViewSet(viewsets.ModelViewSet): ...@@ -93,6 +96,22 @@ class UserViewSet(viewsets.ModelViewSet):
93 ) 96 )
94 return JsonResponse(status=status.HTTP_405_METHOD_NOT_ALLOWED) 97 return JsonResponse(status=status.HTTP_405_METHOD_NOT_ALLOWED)
95 98
99 + def get(self, request, pk):
100 + user = User.objects.filter(int_id=pk)
101 + data = serializers.serialize("json", user)
102 + json_data = json.loads(data)
103 + res = json_data[0]['fields']
104 + res['id']=json_data[0]['pk']
105 + return Response({'data': res}, status=status.HTTP_200_OK)
106 +
107 + def get_permissions(self):
108 + try:
109 + # return permission_classes depending on `action`
110 + return [permission() for permission in self.permission_classes_by_action[self.action]]
111 + except KeyError:
112 + # action is not set return default permission_classes
113 + return [permission() for permission in self.permission_classes]
114 +
96 115
97 class ItemViewSet(viewsets.ViewSet): 116 class ItemViewSet(viewsets.ViewSet):
98 117
......
...@@ -35,5 +35,6 @@ urlpatterns = [ ...@@ -35,5 +35,6 @@ urlpatterns = [
35 url(r'^<int:pk>/copy/$', views.ItemViewSet.copy, name='copy'), 35 url(r'^<int:pk>/copy/$', views.ItemViewSet.copy, name='copy'),
36 url(r'^<int:pk>/children/$', views.ItemViewSet.children, name='copy'), 36 url(r'^<int:pk>/children/$', views.ItemViewSet.children, name='copy'),
37 url(r'^signup/$', views.UserViewSet.signup, name='signup'), 37 url(r'^signup/$', views.UserViewSet.signup, name='signup'),
38 + url(r'^login/$', views.UserViewSet.login, name='login'),
38 39
39 ] 40 ]
......