권주희

Merge branch 'feature/item_api' into 'develop'

Feature/item api



See merge request !12
......@@ -15,13 +15,15 @@ from rest_framework.decorators import action
from rest_framework.permissions import IsAuthenticated, AllowAny
from .models import Item, SharedItem, User
from .serializers import UserSerializer,GroupSerializer,ItemSerializer
from .serializers import UserSerializer, GroupSerializer, ItemSerializer
from rest_framework import status
from annoying.functions import get_object_or_None
from django.conf import settings
import jwt
from django.http import HttpResponse, JsonResponse
from khudrive.settings import AWS_SESSION_TOKEN, AWS_SECRET_ACCESS_KEY, AWS_ACCESS_KEY_ID, AWS_REGION, AWS_STORAGE_BUCKET_NAME
from khudrive.settings import AWS_SESSION_TOKEN, AWS_SECRET_ACCESS_KEY, AWS_ACCESS_KEY_ID, AWS_REGION, \
AWS_STORAGE_BUCKET_NAME
class UserViewSet(viewsets.ModelViewSet):
"""
......@@ -34,15 +36,17 @@ class UserViewSet(viewsets.ModelViewSet):
]
permission_classes_by_action = {'get': [permissions.AllowAny],
'destroy': [permissions.AllowAny]}
@csrf_exempt
@action(detail=False, methods=['POST'], permission_classes=[permissions.AllowAny], url_path='signup', url_name='singup')
@action(detail=False, methods=['POST'], permission_classes=[permissions.AllowAny], url_path='signup',
url_name='singup')
def signup(self, request):
user_id = request.POST.get('user_id', '')
name = request.POST.get('name', '')
password = request.POST.get('password', '')
user = get_object_or_None(User, user_id=user_id)
if user == None:
user = User(user_id = user_id, name = name, password = password, total_size=100000, current_size = 0)
user = User(user_id=user_id, name=name, password=password, total_size=100000, current_size=0)
user.save()
root = Item(is_folder=True, name="root", file_type="folder", path="", user_id=user.int_id, size=0,
status=True)
......@@ -52,7 +56,7 @@ class UserViewSet(viewsets.ModelViewSet):
'int_id': user.int_id,
'user_id': user.user_id,
'name': user.name,
'root_folder':root.item_id,
'root_folder': root.item_id,
'total_size': user.total_size,
'current_size': user.current_size,
'created_time': user.created_time
......@@ -107,7 +111,7 @@ class UserViewSet(viewsets.ModelViewSet):
data = serializers.serialize("json", user)
json_data = json.loads(data)
res = json_data[0]['fields']
res['id']=json_data[0]['pk']
res['id'] = json_data[0]['pk']
return Response({'data': res}, status=status.HTTP_200_OK)
def get_permissions(self):
......@@ -120,11 +124,10 @@ class UserViewSet(viewsets.ModelViewSet):
class ItemViewSet(viewsets.ViewSet):
queryset = Item.objects.all()
serializer_class = ItemSerializer
permission_classes = [permissions.IsAuthenticatedOrReadOnly, permissions.AllowAny,
#IsOwnerOrReadOnly
# IsOwnerOrReadOnly
]
permission_classes_by_action = {'get': [permissions.AllowAny],
'destroy': [permissions.AllowAny]}
......@@ -134,8 +137,8 @@ class ItemViewSet(viewsets.ViewSet):
def search(self, request):
if request.method == 'GET':
keyword = request.GET.get('keyword', '')
user_id = request.GET.get('user_id', '')
item_list = Item.objects.filter(name__icontains = keyword, user_id = user_id )
# user_id = request.GET.get('user_id', '')
item_list = Item.objects.filter(name__icontains=keyword)
data = serializers.serialize("json", item_list)
json_data = json.loads(data)
......@@ -144,7 +147,8 @@ class ItemViewSet(viewsets.ViewSet):
t = i['fields']
t['id'] = i['pk']
res.append(t)
return Response({'data': {'list' : res}}, status=status.HTTP_200_OK)
return Response({'data': {'list': res}}, status=status.HTTP_200_OK)
"""
# url: items/11/
# 마지막 slash도 써주어야함
......@@ -165,6 +169,7 @@ class ItemViewSet(viewsets.ViewSet):
return Response({'message': presigned_url}, status=status.HTTP_200_OK)
"""
# url: items/11/
# 마지막 slash도 써주어야함
def get(self, request, pk):
......@@ -184,19 +189,18 @@ class ItemViewSet(viewsets.ViewSet):
'get_object',
Params={'Bucket': s3_bucket,
'Key': object_name},
ExpiresIn = 3600
ExpiresIn=3600
)
res = json_data[0]['fields']
res['id']=json_data[0]['pk']
res['signed_url']=presigned_url
res['id'] = json_data[0]['pk']
res['signed_url'] = presigned_url
return Response({'data': res}, status=status.HTTP_200_OK)
# url: items/11/
# 마지막 slash도 써주어야함
def destroy(self, request, pk):
if request.method == 'DELETE':
print(pk)
item = get_object_or_None(Item, item_id=pk)
if item != None:
if item.is_folder == True: # 폴더는 삭제 안되도록 처리
......@@ -204,7 +208,28 @@ class ItemViewSet(viewsets.ViewSet):
item.is_deleted = True
item.save()
# item.delete() 이거 하면 완전 삭제되어버림 is deleted True 면 휴지통에서 리스트 조회할 수 있도록!
return Response({'message': 'delete complete'},status=status.HTTP_200_OK)
return Response({'message': 'destroy complete'}, status=status.HTTP_200_OK)
return Response({'message': 'item is not existed.'}, status=status.HTTP_204_NO_CONTENT)
@action(methods=['POST'], detail=True, permission_classes=[AllowAny], url_path='restore', url_name='restore')
def restore(self, request, pk):
if request.method == 'POST':
item = get_object_or_None(Item, item_id=pk)
if item != None:
item.is_deleted = False
item.save()
return Response({'message': 'restore complete'}, status=status.HTTP_200_OK)
return Response({'message': 'item is not existed.'}, status=status.HTTP_204_NO_CONTENT)
@action(methods=['DELETE'], detail=True, permission_classes=[AllowAny], url_path='delete', url_name='delete')
def delete(self, request, pk):
if request.method == 'DELETE':
item = get_object_or_None(Item, item_id=pk)
if item != None:
if item.is_folder == True: # 폴더는 삭제 안되도록 처리
return Response({'message': 'This item is folder.'}, status=status.HTTP_200_OK)
item.delete()
return Response({'message': 'delete permanently complete'}, status=status.HTTP_200_OK)
return Response({'message': 'item is not existed.'}, status=status.HTTP_204_NO_CONTENT)
# url: items/11/move
......@@ -213,7 +238,7 @@ class ItemViewSet(viewsets.ViewSet):
def move(self, request, pk):
if request.method == 'POST':
parent_id = request.POST.get('parent', '')
name = request.POST.get('name','')
name = request.POST.get('name', '')
parent = get_object_or_None(Item, item_id=parent_id)
if parent != None and parent.is_folder == True:
child = get_object_or_None(Item, item_id=pk)
......@@ -221,12 +246,12 @@ class ItemViewSet(viewsets.ViewSet):
return Response({'message': 'item is not existed.'}, status=status.HTTP_204_NO_CONTENT)
child.parent = parent_id
child.save()
child = Item.objects.filter(item_id = pk)
child = Item.objects.filter(item_id=pk)
child_data = serializers.serialize("json", child)
json_child = json.loads(child_data)
res = json_child[0]['fields']
res['id'] = pk
parent = Item.objects.filter(item_id = parent_id)
parent = Item.objects.filter(item_id=parent_id)
parent_data = serializers.serialize("json", parent)
json_parent = json.loads(parent_data)[0]['fields']
res['parentInfo'] = json_parent
......@@ -249,15 +274,16 @@ class ItemViewSet(viewsets.ViewSet):
if child.is_folder == True:
return Response({'message': 'item is folder'}, status=status.HTTP_204_NO_CONTENT)
copiedName = child.name + "_복사본_" + str(datetime.now().strftime('%Y-%m-%d %H:%M'))
copiedItem = Item(is_folder = False, name = copiedName, path =child.path, parent = parent_id, user_id= child.user_id, size=child.size, status=child.status)
copiedItem = Item(is_folder=False, name=copiedName, path=child.path, parent=parent_id,
user_id=child.user_id, size=child.size, status=child.status)
copiedItem.save()
copiedItem = Item.objects.filter(name = copiedName)
copiedItem = Item.objects.filter(name=copiedName)
copied_data = serializers.serialize("json", copiedItem)
json_data = json.loads(copied_data)
res = json_data[0]['fields']
res['id'] = json_data[0]['pk']
parent = Item.objects.filter(item_id = parent_id)
parent = Item.objects.filter(item_id=parent_id)
parent_data = serializers.serialize("json", parent)
json_parent = json.loads(parent_data)[0]['fields']
res['parentInfo'] = json_parent
......@@ -281,10 +307,10 @@ class ItemViewSet(viewsets.ViewSet):
url_path='children', url_name='children')
def children(self, request, pk):
if request.method == 'GET':
children = Item.objects.filter(parent = pk, is_deleted=False)
children = Item.objects.filter(parent=pk, is_deleted=False)
children_data = serializers.serialize("json", children)
json_children = json.loads(children_data)
parent = Item.objects.filter(item_id=pk) #item
parent = Item.objects.filter(item_id=pk) # item
parent_data = serializers.serialize("json", parent)
json_parent = json.loads(parent_data)[0]['fields']
res = json_parent
......@@ -299,13 +325,14 @@ class ItemViewSet(viewsets.ViewSet):
if request.method == 'POST':
name = request.POST.get('name', '')
user_id = request.GET.get('user_id', '')
item = Item(is_folder=True, name=name, file_type="folder", path="", parent=pk, user_id=user_id, size=0, status=True)
item = Item(is_folder=True, name=name, file_type="folder", path="", parent=pk, user_id=user_id, size=0,
status=True)
item.save()
item = Item.objects.filter(item_id = item.item_id)
item = Item.objects.filter(item_id=item.item_id)
item_data = serializers.serialize("json", item)
json_item = json.loads(item_data)
res = json_item[0]['fields']
res['id']=json_item[0]['pk']
res['id'] = json_item[0]['pk']
res['inside_folder_list'] = []
res['inside_file_list'] = []
return Response({'data': res}, status=status.HTTP_200_OK)
......@@ -318,7 +345,7 @@ class ItemViewSet(viewsets.ViewSet):
s3 = boto3.client('s3')
s3_bucket = AWS_STORAGE_BUCKET_NAME
#파일 객체 생성
# 파일 객체 생성
file_name = request.POST.get('name', '')
file_size = request.POST.get('size', '')
file_parent = pk
......@@ -352,7 +379,7 @@ class ItemViewSet(viewsets.ViewSet):
'url': 'https://%s.s3.amazonaws.com/%s' % (s3_bucket, file_name)
}
return Response({'presigned_post':presigned_post, 'proc_data':data}, status=status.HTTP_200_OK)
return Response({'presigned_post': presigned_post, 'proc_data': data}, status=status.HTTP_200_OK)
# url: /status/
@action(methods=['POST'], detail=True, permission_classes=[AllowAny],
......@@ -360,7 +387,7 @@ class ItemViewSet(viewsets.ViewSet):
def status(self, request, *args, **kwargs):
if request.method == 'POST':
pk = request.POST.get('item_id', '')
queryset = Item.objects.filter(item_id = pk)
queryset = Item.objects.filter(item_id=pk)
for cand in queryset:
cand.status = True
cand.save()
......@@ -369,12 +396,12 @@ class ItemViewSet(viewsets.ViewSet):
class SharedItemViewSet(viewsets.ModelViewSet):
queryset = SharedItem.objects.all()
# serializer_class = SharedItemSerializer
permission_classes = [permissions.IsAuthenticatedOrReadOnly, permissions.AllowAny,
# IsOwnerOrReadOnly
]
# url: http://localhost:8000/items/1/share/
# 마지막 slash도 써주어야함
@csrf_exempt
......@@ -388,19 +415,20 @@ class SharedItemViewSet(viewsets.ModelViewSet):
if sharedfile != None:
# 서버는 정상이나 이미 공유객체로 등록된 파일임
return Response({'message': 'This file is already shared'}, status=status.HTTP_200_OK)
sharedfile = SharedItem(item_id =pk, password=password, expires = expires)
sharedfile = SharedItem(item_id=pk, password=password, expires=expires)
sharedfile.save()
sharedfile = SharedItem.objects.get(item_id = pk)
sharedfile = SharedItem.objects.get(item_id=pk)
# sf = serializers.serialize("json", sharedfile)
item = Item.objects.filter(item_id = pk)
item = Item.objects.filter(item_id=pk)
item_json = serializers.serialize("json", item)
json_data = json.loads(item_json)
print(json_data)
res = json_data[0]['fields']
res['id'] = json_data[0]['pk']
return Response({"shared": sharedfile.created_time , 'data': res}, status=status.HTTP_200_OK)
return Response({"shared": sharedfile.created_time, 'data': res}, status=status.HTTP_200_OK)
item = ItemViewSet.as_view({
'delete': 'destroy',
......
......@@ -30,6 +30,8 @@ urlpatterns = [
path('admin/', admin.site.urls),
path('', include(router.urls)),
url(r'^search/$', views.ItemViewSet.search, name='search'),
url(r'^<int:pk>/delete/$', views.ItemViewSet.delete, name='delete'),
url(r'^<int:pk>/restore/$', views.ItemViewSet.restore, name='restore'),
url(r'^<int:pk>/share/$', views.SharedItemViewSet.share, name='share'),
url(r'^<int:pk>/move/$', views.ItemViewSet.move, name='move'),
url(r'^<int:pk>/copy/$', views.ItemViewSet.copy, name='copy'),
......