권주희

Merge branch 'develop' into 'master'

Develop



See merge request !13
...@@ -15,13 +15,15 @@ from rest_framework.decorators import action ...@@ -15,13 +15,15 @@ from rest_framework.decorators import action
15 from rest_framework.permissions import IsAuthenticated, AllowAny 15 from rest_framework.permissions import IsAuthenticated, AllowAny
16 16
17 from .models import Item, SharedItem, User 17 from .models import Item, SharedItem, User
18 -from .serializers import UserSerializer,GroupSerializer,ItemSerializer 18 +from .serializers import UserSerializer, GroupSerializer, ItemSerializer
19 from rest_framework import status 19 from rest_framework import status
20 from annoying.functions import get_object_or_None 20 from annoying.functions import get_object_or_None
21 from django.conf import settings 21 from django.conf import settings
22 import jwt 22 import jwt
23 from django.http import HttpResponse, JsonResponse 23 from django.http import HttpResponse, JsonResponse
24 -from khudrive.settings import AWS_SESSION_TOKEN, AWS_SECRET_ACCESS_KEY, AWS_ACCESS_KEY_ID, AWS_REGION, AWS_STORAGE_BUCKET_NAME 24 +from khudrive.settings import AWS_SESSION_TOKEN, AWS_SECRET_ACCESS_KEY, AWS_ACCESS_KEY_ID, AWS_REGION, \
25 + AWS_STORAGE_BUCKET_NAME
26 +
25 27
26 class UserViewSet(viewsets.ModelViewSet): 28 class UserViewSet(viewsets.ModelViewSet):
27 """ 29 """
...@@ -34,15 +36,17 @@ class UserViewSet(viewsets.ModelViewSet): ...@@ -34,15 +36,17 @@ class UserViewSet(viewsets.ModelViewSet):
34 ] 36 ]
35 permission_classes_by_action = {'get': [permissions.AllowAny], 37 permission_classes_by_action = {'get': [permissions.AllowAny],
36 'destroy': [permissions.AllowAny]} 38 'destroy': [permissions.AllowAny]}
39 +
37 @csrf_exempt 40 @csrf_exempt
38 - @action(detail=False, methods=['POST'], permission_classes=[permissions.AllowAny], url_path='signup', url_name='singup') 41 + @action(detail=False, methods=['POST'], permission_classes=[permissions.AllowAny], url_path='signup',
42 + url_name='singup')
39 def signup(self, request): 43 def signup(self, request):
40 user_id = request.POST.get('user_id', '') 44 user_id = request.POST.get('user_id', '')
41 name = request.POST.get('name', '') 45 name = request.POST.get('name', '')
42 password = request.POST.get('password', '') 46 password = request.POST.get('password', '')
43 user = get_object_or_None(User, user_id=user_id) 47 user = get_object_or_None(User, user_id=user_id)
44 if user == None: 48 if user == None:
45 - user = User(user_id = user_id, name = name, password = password, total_size=100000, current_size = 0) 49 + user = User(user_id=user_id, name=name, password=password, total_size=100000, current_size=0)
46 user.save() 50 user.save()
47 root = Item(is_folder=True, name="root", file_type="folder", path="", user_id=user.int_id, size=0, 51 root = Item(is_folder=True, name="root", file_type="folder", path="", user_id=user.int_id, size=0,
48 status=True) 52 status=True)
...@@ -52,7 +56,7 @@ class UserViewSet(viewsets.ModelViewSet): ...@@ -52,7 +56,7 @@ class UserViewSet(viewsets.ModelViewSet):
52 'int_id': user.int_id, 56 'int_id': user.int_id,
53 'user_id': user.user_id, 57 'user_id': user.user_id,
54 'name': user.name, 58 'name': user.name,
55 - 'root_folder':root.item_id, 59 + 'root_folder': root.item_id,
56 'total_size': user.total_size, 60 'total_size': user.total_size,
57 'current_size': user.current_size, 61 'current_size': user.current_size,
58 'created_time': user.created_time 62 'created_time': user.created_time
...@@ -107,7 +111,7 @@ class UserViewSet(viewsets.ModelViewSet): ...@@ -107,7 +111,7 @@ class UserViewSet(viewsets.ModelViewSet):
107 data = serializers.serialize("json", user) 111 data = serializers.serialize("json", user)
108 json_data = json.loads(data) 112 json_data = json.loads(data)
109 res = json_data[0]['fields'] 113 res = json_data[0]['fields']
110 - res['id']=json_data[0]['pk'] 114 + res['id'] = json_data[0]['pk']
111 return Response({'data': res}, status=status.HTTP_200_OK) 115 return Response({'data': res}, status=status.HTTP_200_OK)
112 116
113 def get_permissions(self): 117 def get_permissions(self):
...@@ -120,11 +124,10 @@ class UserViewSet(viewsets.ModelViewSet): ...@@ -120,11 +124,10 @@ class UserViewSet(viewsets.ModelViewSet):
120 124
121 125
122 class ItemViewSet(viewsets.ViewSet): 126 class ItemViewSet(viewsets.ViewSet):
123 -
124 queryset = Item.objects.all() 127 queryset = Item.objects.all()
125 serializer_class = ItemSerializer 128 serializer_class = ItemSerializer
126 permission_classes = [permissions.IsAuthenticatedOrReadOnly, permissions.AllowAny, 129 permission_classes = [permissions.IsAuthenticatedOrReadOnly, permissions.AllowAny,
127 - #IsOwnerOrReadOnly 130 + # IsOwnerOrReadOnly
128 ] 131 ]
129 permission_classes_by_action = {'get': [permissions.AllowAny], 132 permission_classes_by_action = {'get': [permissions.AllowAny],
130 'destroy': [permissions.AllowAny]} 133 'destroy': [permissions.AllowAny]}
...@@ -134,8 +137,8 @@ class ItemViewSet(viewsets.ViewSet): ...@@ -134,8 +137,8 @@ class ItemViewSet(viewsets.ViewSet):
134 def search(self, request): 137 def search(self, request):
135 if request.method == 'GET': 138 if request.method == 'GET':
136 keyword = request.GET.get('keyword', '') 139 keyword = request.GET.get('keyword', '')
137 - user_id = request.GET.get('user_id', '') 140 + # user_id = request.GET.get('user_id', '')
138 - item_list = Item.objects.filter(name__icontains = keyword, user_id = user_id ) 141 + item_list = Item.objects.filter(name__icontains=keyword)
139 142
140 data = serializers.serialize("json", item_list) 143 data = serializers.serialize("json", item_list)
141 json_data = json.loads(data) 144 json_data = json.loads(data)
...@@ -144,7 +147,8 @@ class ItemViewSet(viewsets.ViewSet): ...@@ -144,7 +147,8 @@ class ItemViewSet(viewsets.ViewSet):
144 t = i['fields'] 147 t = i['fields']
145 t['id'] = i['pk'] 148 t['id'] = i['pk']
146 res.append(t) 149 res.append(t)
147 - return Response({'data': {'list' : res}}, status=status.HTTP_200_OK) 150 + return Response({'data': {'list': res}}, status=status.HTTP_200_OK)
151 +
148 """ 152 """
149 # url: items/11/ 153 # url: items/11/
150 # 마지막 slash도 써주어야함 154 # 마지막 slash도 써주어야함
...@@ -165,6 +169,7 @@ class ItemViewSet(viewsets.ViewSet): ...@@ -165,6 +169,7 @@ class ItemViewSet(viewsets.ViewSet):
165 169
166 return Response({'message': presigned_url}, status=status.HTTP_200_OK) 170 return Response({'message': presigned_url}, status=status.HTTP_200_OK)
167 """ 171 """
172 +
168 # url: items/11/ 173 # url: items/11/
169 # 마지막 slash도 써주어야함 174 # 마지막 slash도 써주어야함
170 def get(self, request, pk): 175 def get(self, request, pk):
...@@ -184,19 +189,18 @@ class ItemViewSet(viewsets.ViewSet): ...@@ -184,19 +189,18 @@ class ItemViewSet(viewsets.ViewSet):
184 'get_object', 189 'get_object',
185 Params={'Bucket': s3_bucket, 190 Params={'Bucket': s3_bucket,
186 'Key': object_name}, 191 'Key': object_name},
187 - ExpiresIn = 3600 192 + ExpiresIn=3600
188 ) 193 )
189 194
190 res = json_data[0]['fields'] 195 res = json_data[0]['fields']
191 - res['id']=json_data[0]['pk'] 196 + res['id'] = json_data[0]['pk']
192 - res['signed_url']=presigned_url 197 + res['signed_url'] = presigned_url
193 return Response({'data': res}, status=status.HTTP_200_OK) 198 return Response({'data': res}, status=status.HTTP_200_OK)
194 199
195 # url: items/11/ 200 # url: items/11/
196 # 마지막 slash도 써주어야함 201 # 마지막 slash도 써주어야함
197 def destroy(self, request, pk): 202 def destroy(self, request, pk):
198 if request.method == 'DELETE': 203 if request.method == 'DELETE':
199 - print(pk)
200 item = get_object_or_None(Item, item_id=pk) 204 item = get_object_or_None(Item, item_id=pk)
201 if item != None: 205 if item != None:
202 if item.is_folder == True: # 폴더는 삭제 안되도록 처리 206 if item.is_folder == True: # 폴더는 삭제 안되도록 처리
...@@ -204,7 +208,28 @@ class ItemViewSet(viewsets.ViewSet): ...@@ -204,7 +208,28 @@ class ItemViewSet(viewsets.ViewSet):
204 item.is_deleted = True 208 item.is_deleted = True
205 item.save() 209 item.save()
206 # item.delete() 이거 하면 완전 삭제되어버림 is deleted True 면 휴지통에서 리스트 조회할 수 있도록! 210 # item.delete() 이거 하면 완전 삭제되어버림 is deleted True 면 휴지통에서 리스트 조회할 수 있도록!
207 - return Response({'message': 'delete complete'},status=status.HTTP_200_OK) 211 + return Response({'message': 'destroy complete'}, status=status.HTTP_200_OK)
212 + return Response({'message': 'item is not existed.'}, status=status.HTTP_204_NO_CONTENT)
213 +
214 + @action(methods=['POST'], detail=True, permission_classes=[AllowAny], url_path='restore', url_name='restore')
215 + def restore(self, request, pk):
216 + if request.method == 'POST':
217 + item = get_object_or_None(Item, item_id=pk)
218 + if item != None:
219 + item.is_deleted = False
220 + item.save()
221 + return Response({'message': 'restore complete'}, status=status.HTTP_200_OK)
222 + return Response({'message': 'item is not existed.'}, status=status.HTTP_204_NO_CONTENT)
223 +
224 + @action(methods=['DELETE'], detail=True, permission_classes=[AllowAny], url_path='delete', url_name='delete')
225 + def delete(self, request, pk):
226 + if request.method == 'DELETE':
227 + item = get_object_or_None(Item, item_id=pk)
228 + if item != None:
229 + if item.is_folder == True: # 폴더는 삭제 안되도록 처리
230 + return Response({'message': 'This item is folder.'}, status=status.HTTP_200_OK)
231 + item.delete()
232 + return Response({'message': 'delete permanently complete'}, status=status.HTTP_200_OK)
208 return Response({'message': 'item is not existed.'}, status=status.HTTP_204_NO_CONTENT) 233 return Response({'message': 'item is not existed.'}, status=status.HTTP_204_NO_CONTENT)
209 234
210 # url: items/11/move 235 # url: items/11/move
...@@ -213,7 +238,7 @@ class ItemViewSet(viewsets.ViewSet): ...@@ -213,7 +238,7 @@ class ItemViewSet(viewsets.ViewSet):
213 def move(self, request, pk): 238 def move(self, request, pk):
214 if request.method == 'POST': 239 if request.method == 'POST':
215 parent_id = request.POST.get('parent', '') 240 parent_id = request.POST.get('parent', '')
216 - name = request.POST.get('name','') 241 + name = request.POST.get('name', '')
217 parent = get_object_or_None(Item, item_id=parent_id) 242 parent = get_object_or_None(Item, item_id=parent_id)
218 if parent != None and parent.is_folder == True: 243 if parent != None and parent.is_folder == True:
219 child = get_object_or_None(Item, item_id=pk) 244 child = get_object_or_None(Item, item_id=pk)
...@@ -221,12 +246,12 @@ class ItemViewSet(viewsets.ViewSet): ...@@ -221,12 +246,12 @@ class ItemViewSet(viewsets.ViewSet):
221 return Response({'message': 'item is not existed.'}, status=status.HTTP_204_NO_CONTENT) 246 return Response({'message': 'item is not existed.'}, status=status.HTTP_204_NO_CONTENT)
222 child.parent = parent_id 247 child.parent = parent_id
223 child.save() 248 child.save()
224 - child = Item.objects.filter(item_id = pk) 249 + child = Item.objects.filter(item_id=pk)
225 child_data = serializers.serialize("json", child) 250 child_data = serializers.serialize("json", child)
226 json_child = json.loads(child_data) 251 json_child = json.loads(child_data)
227 res = json_child[0]['fields'] 252 res = json_child[0]['fields']
228 res['id'] = pk 253 res['id'] = pk
229 - parent = Item.objects.filter(item_id = parent_id) 254 + parent = Item.objects.filter(item_id=parent_id)
230 parent_data = serializers.serialize("json", parent) 255 parent_data = serializers.serialize("json", parent)
231 json_parent = json.loads(parent_data)[0]['fields'] 256 json_parent = json.loads(parent_data)[0]['fields']
232 res['parentInfo'] = json_parent 257 res['parentInfo'] = json_parent
...@@ -249,15 +274,16 @@ class ItemViewSet(viewsets.ViewSet): ...@@ -249,15 +274,16 @@ class ItemViewSet(viewsets.ViewSet):
249 if child.is_folder == True: 274 if child.is_folder == True:
250 return Response({'message': 'item is folder'}, status=status.HTTP_204_NO_CONTENT) 275 return Response({'message': 'item is folder'}, status=status.HTTP_204_NO_CONTENT)
251 copiedName = child.name + "_복사본_" + str(datetime.now().strftime('%Y-%m-%d %H:%M')) 276 copiedName = child.name + "_복사본_" + str(datetime.now().strftime('%Y-%m-%d %H:%M'))
252 - copiedItem = Item(is_folder = False, name = copiedName, path =child.path, parent = parent_id, user_id= child.user_id, size=child.size, status=child.status) 277 + copiedItem = Item(is_folder=False, name=copiedName, path=child.path, parent=parent_id,
278 + user_id=child.user_id, size=child.size, status=child.status)
253 copiedItem.save() 279 copiedItem.save()
254 280
255 - copiedItem = Item.objects.filter(name = copiedName) 281 + copiedItem = Item.objects.filter(name=copiedName)
256 copied_data = serializers.serialize("json", copiedItem) 282 copied_data = serializers.serialize("json", copiedItem)
257 json_data = json.loads(copied_data) 283 json_data = json.loads(copied_data)
258 res = json_data[0]['fields'] 284 res = json_data[0]['fields']
259 res['id'] = json_data[0]['pk'] 285 res['id'] = json_data[0]['pk']
260 - parent = Item.objects.filter(item_id = parent_id) 286 + parent = Item.objects.filter(item_id=parent_id)
261 parent_data = serializers.serialize("json", parent) 287 parent_data = serializers.serialize("json", parent)
262 json_parent = json.loads(parent_data)[0]['fields'] 288 json_parent = json.loads(parent_data)[0]['fields']
263 res['parentInfo'] = json_parent 289 res['parentInfo'] = json_parent
...@@ -281,10 +307,10 @@ class ItemViewSet(viewsets.ViewSet): ...@@ -281,10 +307,10 @@ class ItemViewSet(viewsets.ViewSet):
281 url_path='children', url_name='children') 307 url_path='children', url_name='children')
282 def children(self, request, pk): 308 def children(self, request, pk):
283 if request.method == 'GET': 309 if request.method == 'GET':
284 - children = Item.objects.filter(parent = pk, is_deleted=False) 310 + children = Item.objects.filter(parent=pk, is_deleted=False)
285 children_data = serializers.serialize("json", children) 311 children_data = serializers.serialize("json", children)
286 json_children = json.loads(children_data) 312 json_children = json.loads(children_data)
287 - parent = Item.objects.filter(item_id=pk) #item 313 + parent = Item.objects.filter(item_id=pk) # item
288 parent_data = serializers.serialize("json", parent) 314 parent_data = serializers.serialize("json", parent)
289 json_parent = json.loads(parent_data)[0]['fields'] 315 json_parent = json.loads(parent_data)[0]['fields']
290 res = json_parent 316 res = json_parent
...@@ -299,13 +325,14 @@ class ItemViewSet(viewsets.ViewSet): ...@@ -299,13 +325,14 @@ class ItemViewSet(viewsets.ViewSet):
299 if request.method == 'POST': 325 if request.method == 'POST':
300 name = request.POST.get('name', '') 326 name = request.POST.get('name', '')
301 user_id = request.GET.get('user_id', '') 327 user_id = request.GET.get('user_id', '')
302 - item = Item(is_folder=True, name=name, file_type="folder", path="", parent=pk, user_id=user_id, size=0, status=True) 328 + item = Item(is_folder=True, name=name, file_type="folder", path="", parent=pk, user_id=user_id, size=0,
329 + status=True)
303 item.save() 330 item.save()
304 - item = Item.objects.filter(item_id = item.item_id) 331 + item = Item.objects.filter(item_id=item.item_id)
305 item_data = serializers.serialize("json", item) 332 item_data = serializers.serialize("json", item)
306 json_item = json.loads(item_data) 333 json_item = json.loads(item_data)
307 res = json_item[0]['fields'] 334 res = json_item[0]['fields']
308 - res['id']=json_item[0]['pk'] 335 + res['id'] = json_item[0]['pk']
309 res['inside_folder_list'] = [] 336 res['inside_folder_list'] = []
310 res['inside_file_list'] = [] 337 res['inside_file_list'] = []
311 return Response({'data': res}, status=status.HTTP_200_OK) 338 return Response({'data': res}, status=status.HTTP_200_OK)
...@@ -318,7 +345,7 @@ class ItemViewSet(viewsets.ViewSet): ...@@ -318,7 +345,7 @@ class ItemViewSet(viewsets.ViewSet):
318 s3 = boto3.client('s3') 345 s3 = boto3.client('s3')
319 s3_bucket = AWS_STORAGE_BUCKET_NAME 346 s3_bucket = AWS_STORAGE_BUCKET_NAME
320 347
321 - #파일 객체 생성 348 + # 파일 객체 생성
322 file_name = request.POST.get('name', '') 349 file_name = request.POST.get('name', '')
323 file_size = request.POST.get('size', '') 350 file_size = request.POST.get('size', '')
324 file_parent = pk 351 file_parent = pk
...@@ -352,7 +379,7 @@ class ItemViewSet(viewsets.ViewSet): ...@@ -352,7 +379,7 @@ class ItemViewSet(viewsets.ViewSet):
352 'url': 'https://%s.s3.amazonaws.com/%s' % (s3_bucket, file_name) 379 'url': 'https://%s.s3.amazonaws.com/%s' % (s3_bucket, file_name)
353 } 380 }
354 381
355 - return Response({'presigned_post':presigned_post, 'proc_data':data}, status=status.HTTP_200_OK) 382 + return Response({'presigned_post': presigned_post, 'proc_data': data}, status=status.HTTP_200_OK)
356 383
357 # url: /status/ 384 # url: /status/
358 @action(methods=['POST'], detail=True, permission_classes=[AllowAny], 385 @action(methods=['POST'], detail=True, permission_classes=[AllowAny],
...@@ -360,7 +387,7 @@ class ItemViewSet(viewsets.ViewSet): ...@@ -360,7 +387,7 @@ class ItemViewSet(viewsets.ViewSet):
360 def status(self, request, *args, **kwargs): 387 def status(self, request, *args, **kwargs):
361 if request.method == 'POST': 388 if request.method == 'POST':
362 pk = request.POST.get('item_id', '') 389 pk = request.POST.get('item_id', '')
363 - queryset = Item.objects.filter(item_id = pk) 390 + queryset = Item.objects.filter(item_id=pk)
364 for cand in queryset: 391 for cand in queryset:
365 cand.status = True 392 cand.status = True
366 cand.save() 393 cand.save()
...@@ -369,12 +396,12 @@ class ItemViewSet(viewsets.ViewSet): ...@@ -369,12 +396,12 @@ class ItemViewSet(viewsets.ViewSet):
369 396
370 397
371 class SharedItemViewSet(viewsets.ModelViewSet): 398 class SharedItemViewSet(viewsets.ModelViewSet):
372 -
373 queryset = SharedItem.objects.all() 399 queryset = SharedItem.objects.all()
374 # serializer_class = SharedItemSerializer 400 # serializer_class = SharedItemSerializer
375 permission_classes = [permissions.IsAuthenticatedOrReadOnly, permissions.AllowAny, 401 permission_classes = [permissions.IsAuthenticatedOrReadOnly, permissions.AllowAny,
376 # IsOwnerOrReadOnly 402 # IsOwnerOrReadOnly
377 ] 403 ]
404 +
378 # url: http://localhost:8000/items/1/share/ 405 # url: http://localhost:8000/items/1/share/
379 # 마지막 slash도 써주어야함 406 # 마지막 slash도 써주어야함
380 @csrf_exempt 407 @csrf_exempt
...@@ -388,19 +415,20 @@ class SharedItemViewSet(viewsets.ModelViewSet): ...@@ -388,19 +415,20 @@ class SharedItemViewSet(viewsets.ModelViewSet):
388 if sharedfile != None: 415 if sharedfile != None:
389 # 서버는 정상이나 이미 공유객체로 등록된 파일임 416 # 서버는 정상이나 이미 공유객체로 등록된 파일임
390 return Response({'message': 'This file is already shared'}, status=status.HTTP_200_OK) 417 return Response({'message': 'This file is already shared'}, status=status.HTTP_200_OK)
391 - sharedfile = SharedItem(item_id =pk, password=password, expires = expires) 418 + sharedfile = SharedItem(item_id=pk, password=password, expires=expires)
392 sharedfile.save() 419 sharedfile.save()
393 - sharedfile = SharedItem.objects.get(item_id = pk) 420 + sharedfile = SharedItem.objects.get(item_id=pk)
394 421
395 # sf = serializers.serialize("json", sharedfile) 422 # sf = serializers.serialize("json", sharedfile)
396 - item = Item.objects.filter(item_id = pk) 423 + item = Item.objects.filter(item_id=pk)
397 item_json = serializers.serialize("json", item) 424 item_json = serializers.serialize("json", item)
398 425
399 json_data = json.loads(item_json) 426 json_data = json.loads(item_json)
400 print(json_data) 427 print(json_data)
401 res = json_data[0]['fields'] 428 res = json_data[0]['fields']
402 res['id'] = json_data[0]['pk'] 429 res['id'] = json_data[0]['pk']
403 - return Response({"shared": sharedfile.created_time , 'data': res}, status=status.HTTP_200_OK) 430 + return Response({"shared": sharedfile.created_time, 'data': res}, status=status.HTTP_200_OK)
431 +
404 432
405 item = ItemViewSet.as_view({ 433 item = ItemViewSet.as_view({
406 'delete': 'destroy', 434 'delete': 'destroy',
......
...@@ -30,6 +30,8 @@ urlpatterns = [ ...@@ -30,6 +30,8 @@ urlpatterns = [
30 path('admin/', admin.site.urls), 30 path('admin/', admin.site.urls),
31 path('', include(router.urls)), 31 path('', include(router.urls)),
32 url(r'^search/$', views.ItemViewSet.search, name='search'), 32 url(r'^search/$', views.ItemViewSet.search, name='search'),
33 + url(r'^<int:pk>/delete/$', views.ItemViewSet.delete, name='delete'),
34 + url(r'^<int:pk>/restore/$', views.ItemViewSet.restore, name='restore'),
33 url(r'^<int:pk>/share/$', views.SharedItemViewSet.share, name='share'), 35 url(r'^<int:pk>/share/$', views.SharedItemViewSet.share, name='share'),
34 url(r'^<int:pk>/move/$', views.ItemViewSet.move, name='move'), 36 url(r'^<int:pk>/move/$', views.ItemViewSet.move, name='move'),
35 url(r'^<int:pk>/copy/$', views.ItemViewSet.copy, name='copy'), 37 url(r'^<int:pk>/copy/$', views.ItemViewSet.copy, name='copy'),
......