권주희

Merge branch 'feature/item_api' into 'develop'

Feature/item api

- return json object
- setting the default function
- implement destroy item api
- update the destroy api
- implement move api
- fix the response object
- implement copy item api

See merge request !5
# Generated by Django 3.0.6 on 2020-06-10 11:50
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('api', '0004_auto_20200606_0824'),
]
operations = [
migrations.AlterField(
model_name='item',
name='item_id',
field=models.IntegerField(auto_created=True, primary_key=True, serialize=False),
),
]
# Generated by Django 3.0.6 on 2020-06-10 12:09
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('api', '0005_auto_20200610_1150'),
]
operations = [
migrations.AlterField(
model_name='item',
name='item_id',
field=models.AutoField(primary_key=True, serialize=False),
),
]
# Generated by Django 3.0.6 on 2020-06-10 12:17
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('api', '0006_auto_20200610_1209'),
]
operations = [
migrations.AlterField(
model_name='item',
name='updated_time',
field=models.DateTimeField(null=True),
),
]
......@@ -2,7 +2,7 @@ from django.db import models
# Create your models here.
class Item(models.Model):
item_id = models.IntegerField(primary_key = True)
item_id = models.AutoField(primary_key = True)
is_folder = models.BooleanField(default = False)
name = models.CharField(max_length = 50)
path = models.TextField()
......@@ -12,7 +12,7 @@ class Item(models.Model):
size = models.IntegerField()
is_deleted = models.BooleanField(default = False)
created_time = models.DateTimeField(auto_now=True)
updated_time = models.DateTimeField()
updated_time = models.DateTimeField(null=True)
status = models.BooleanField()
#file = models.FileField(upload_to = \path)
......
......@@ -18,7 +18,3 @@ class ItemSerializer(serializers.ModelSerializer):
model = Item
fields = '__all__'
class SharedItemSerializer(serializers.ModelSerializer):
class Meta:
model = Item
fields = '__all__'
......
import mimetypes
import json
import os
from datetime import datetime
import boto3
from django.contrib.auth.models import User
......@@ -8,15 +10,12 @@ from django.core import serializers
from django.views.decorators.csrf import csrf_exempt
from rest_framework import viewsets
from rest_framework import permissions
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.decorators import action
from rest_framework.parsers import JSONParser
from rest_framework.permissions import IsAuthenticated, AllowAny
from api.models import Item, SharedItem
from api.serializers import UserSerializer,GroupSerializer,ItemSerializer,SharedItemSerializer
from rest_framework import generics
from api.serializers import UserSerializer,GroupSerializer,ItemSerializer
from rest_framework import status
from annoying.functions import get_object_or_None
......@@ -29,13 +28,15 @@ class UserViewSet(viewsets.ModelViewSet):
permission_classes = [permissions.IsAuthenticated]
class ItemViewSet(viewsets.ModelViewSet):
class ItemViewSet(viewsets.ViewSet):
queryset = Item.objects.all()
serializer_class = ItemSerializer
permission_classes = [permissions.IsAuthenticatedOrReadOnly, permissions.AllowAny,
#IsOwnerOrReadOnly
]
permission_classes_by_action = {'get': [permissions.AllowAny],
'destroy': [permissions.AllowAny]}
# url: items/search
@action(methods=['GET'], detail=False, permission_classes=[AllowAny], url_path='search', url_name='search')
......@@ -45,12 +46,109 @@ class ItemViewSet(viewsets.ModelViewSet):
item_list = Item.objects.filter(name__icontains = keyword)
data = serializers.serialize("json", item_list)
return Response({'data': {'list' : data}}, status=status.HTTP_200_OK)
json_data = json.loads(data)
res = []
for i in json_data:
t = i['fields']
t['id'] = i['pk']
res.append(t)
return Response({'data': {'list' : res}}, status=status.HTTP_200_OK)
# url: items/11/
# 마지막 slash도 써주어야함
def get(self, request, pk):
print(pk)
return Response({'message': "info complete"}, 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: # 폴더는 삭제 안되도록 처리
return Response({'message': 'This item is folder.'}, status=status.HTTP_200_OK)
item.is_deleted = True
item.save()
# item.delete() 이거 하면 완전 삭제되어버림 is deleted True 면 휴지통에서 리스트 조회할 수 있도록!
return Response({'message': 'delete complete'},status=status.HTTP_200_OK)
return Response({'message': 'item is not existed.'}, status=status.HTTP_204_NO_CONTENT)
# url: items/11/move
# 마지막 slash도 써주어야함
@action(methods=['POST'], detail=True, permission_classes=[AllowAny], url_path='move', url_name='move')
def move(self, request, pk):
if request.method == 'POST':
parent_id = request.POST.get('parent', '')
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)
if child == None:
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_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_data = serializers.serialize("json", parent)
json_parent = json.loads(parent_data)[0]['fields']
res['parentInfo'] = json_parent
return Response({'data': res}, status=status.HTTP_200_OK)
if parent == None:
return Response({'message': 'parent is not existed.'}, status=status.HTTP_200_OK)
if parent.is_folder == False:
return Response({'message': 'parent is not folder.'}, 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='copy', url_name='copy')
def copy(self, request, pk):
if request.method == 'POST':
parent_id = request.POST.get('parent', '')
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)
if child == None:
return Response({'message': 'item is not existed.'}, status=status.HTTP_204_NO_CONTENT)
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.save()
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_data = serializers.serialize("json", parent)
json_parent = json.loads(parent_data)[0]['fields']
res['parentInfo'] = json_parent
return Response({'data': res}, status=status.HTTP_200_OK)
if parent == None:
return Response({'message': 'parent is not existed.'}, status=status.HTTP_200_OK)
if parent.is_folder == False:
return Response({'message': 'parent is not folder.'}, status=status.HTTP_200_OK)
return Response({'message': 'item is not existed.'}, status=status.HTTP_204_NO_CONTENT)
def get_permissions(self):
try:
# return permission_classes depending on `action`
return [permission() for permission in self.permission_classes_by_action[self.action]]
except KeyError:
# action is not set return default permission_classes
return [permission() for permission in self.permission_classes]
class SharedItemViewSet(viewsets.ModelViewSet):
queryset = SharedItem.objects.all()
serializer_class = SharedItemSerializer
# serializer_class = SharedItemSerializer
permission_classes = [permissions.IsAuthenticatedOrReadOnly, permissions.AllowAny,
# IsOwnerOrReadOnly
]
......@@ -66,7 +164,7 @@ class SharedItemViewSet(viewsets.ModelViewSet):
sharedfile = get_object_or_None(SharedItem, item_id=pk)
if sharedfile != None:
# 서버는 정상이나 이미 공유객체로 등록된 파일임
return Response({'Message': 'This file is already shared'}, status=status.HTTP_200_OK)
return Response({'message': 'This file is already shared'}, status=status.HTTP_200_OK)
sharedfile = SharedItem(item_id =pk, password=password, expires = expires)
sharedfile.save()
sharedfile = SharedItem.objects.get(item_id = pk)
......@@ -75,5 +173,12 @@ class SharedItemViewSet(viewsets.ModelViewSet):
item = Item.objects.filter(item_id = pk)
item_json = serializers.serialize("json", item)
# data = serializers.serialize("json", item_list)
return Response({"shared": sharedfile.created_time , 'data': item_json}, status=status.HTTP_200_OK)
\ No newline at end of file
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)
item = ItemViewSet.as_view({
'delete': 'destroy',
})
\ No newline at end of file
......
......@@ -31,5 +31,7 @@ urlpatterns = [
path('', include(router.urls)),
url(r'^search/$', views.ItemViewSet.search, name='search'),
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'),
]
......