권주희

implement copy item api

1 +# Generated by Django 3.0.6 on 2020-06-10 11:50
2 +
3 +from django.db import migrations, models
4 +
5 +
6 +class Migration(migrations.Migration):
7 +
8 + dependencies = [
9 + ('api', '0004_auto_20200606_0824'),
10 + ]
11 +
12 + operations = [
13 + migrations.AlterField(
14 + model_name='item',
15 + name='item_id',
16 + field=models.IntegerField(auto_created=True, primary_key=True, serialize=False),
17 + ),
18 + ]
1 +# Generated by Django 3.0.6 on 2020-06-10 12:09
2 +
3 +from django.db import migrations, models
4 +
5 +
6 +class Migration(migrations.Migration):
7 +
8 + dependencies = [
9 + ('api', '0005_auto_20200610_1150'),
10 + ]
11 +
12 + operations = [
13 + migrations.AlterField(
14 + model_name='item',
15 + name='item_id',
16 + field=models.AutoField(primary_key=True, serialize=False),
17 + ),
18 + ]
1 +# Generated by Django 3.0.6 on 2020-06-10 12:17
2 +
3 +from django.db import migrations, models
4 +
5 +
6 +class Migration(migrations.Migration):
7 +
8 + dependencies = [
9 + ('api', '0006_auto_20200610_1209'),
10 + ]
11 +
12 + operations = [
13 + migrations.AlterField(
14 + model_name='item',
15 + name='updated_time',
16 + field=models.DateTimeField(null=True),
17 + ),
18 + ]
...@@ -2,7 +2,7 @@ from django.db import models ...@@ -2,7 +2,7 @@ from django.db import models
2 2
3 # Create your models here. 3 # Create your models here.
4 class Item(models.Model): 4 class Item(models.Model):
5 - item_id = models.IntegerField(primary_key = True) 5 + item_id = models.AutoField(primary_key = True)
6 is_folder = models.BooleanField(default = False) 6 is_folder = models.BooleanField(default = False)
7 name = models.CharField(max_length = 50) 7 name = models.CharField(max_length = 50)
8 path = models.TextField() 8 path = models.TextField()
...@@ -12,7 +12,7 @@ class Item(models.Model): ...@@ -12,7 +12,7 @@ class Item(models.Model):
12 size = models.IntegerField() 12 size = models.IntegerField()
13 is_deleted = models.BooleanField(default = False) 13 is_deleted = models.BooleanField(default = False)
14 created_time = models.DateTimeField(auto_now=True) 14 created_time = models.DateTimeField(auto_now=True)
15 - updated_time = models.DateTimeField() 15 + updated_time = models.DateTimeField(null=True)
16 status = models.BooleanField() 16 status = models.BooleanField()
17 17
18 #file = models.FileField(upload_to = \path) 18 #file = models.FileField(upload_to = \path)
......
1 import mimetypes 1 import mimetypes
2 import json 2 import json
3 import os 3 import os
4 +from datetime import datetime
5 +
4 import boto3 6 import boto3
5 7
6 from django.contrib.auth.models import User 8 from django.contrib.auth.models import User
...@@ -83,6 +85,8 @@ class ItemViewSet(viewsets.ViewSet): ...@@ -83,6 +85,8 @@ class ItemViewSet(viewsets.ViewSet):
83 parent = get_object_or_None(Item, item_id=parent_id) 85 parent = get_object_or_None(Item, item_id=parent_id)
84 if parent != None and parent.is_folder == True: 86 if parent != None and parent.is_folder == True:
85 child = get_object_or_None(Item, item_id=pk) 87 child = get_object_or_None(Item, item_id=pk)
88 + if child == None:
89 + return Response({'message': 'item is not existed.'}, status=status.HTTP_204_NO_CONTENT)
86 child.parent = parent_id 90 child.parent = parent_id
87 child.save() 91 child.save()
88 child = Item.objects.filter(item_id = pk) 92 child = Item.objects.filter(item_id = pk)
...@@ -96,12 +100,41 @@ class ItemViewSet(viewsets.ViewSet): ...@@ -96,12 +100,41 @@ class ItemViewSet(viewsets.ViewSet):
96 res['parentInfo'] = json_parent 100 res['parentInfo'] = json_parent
97 return Response({'data': res}, status=status.HTTP_200_OK) 101 return Response({'data': res}, status=status.HTTP_200_OK)
98 if parent == None: 102 if parent == None:
99 - return Response({'message': 'item is not existed.'}, status=status.HTTP_200_OK) 103 + return Response({'message': 'parent is not existed.'}, status=status.HTTP_200_OK)
100 if parent.is_folder == False: 104 if parent.is_folder == False:
101 - return Response({'message': 'item is not folder.'}, status=status.HTTP_200_OK) 105 + return Response({'message': 'parent is not folder.'}, status=status.HTTP_200_OK)
102 return Response({'message': 'item is not existed.'}, status=status.HTTP_204_NO_CONTENT) 106 return Response({'message': 'item is not existed.'}, status=status.HTTP_204_NO_CONTENT)
103 107
104 - 108 + @action(methods=['POST'], detail=True, permission_classes=[AllowAny], url_path='copy', url_name='copy')
109 + def copy(self, request, pk):
110 + if request.method == 'POST':
111 + parent_id = request.POST.get('parent', '')
112 + parent = get_object_or_None(Item, item_id=parent_id)
113 + if parent != None and parent.is_folder == True:
114 + child = get_object_or_None(Item, item_id=pk)
115 + if child == None:
116 + return Response({'message': 'item is not existed.'}, status=status.HTTP_204_NO_CONTENT)
117 + if child.is_folder == True:
118 + return Response({'message': 'item is folder'}, status=status.HTTP_204_NO_CONTENT)
119 + copiedName = child.name + "_복사본_" + str(datetime.now().strftime('%Y-%m-%d %H:%M'))
120 + copiedItem = Item(is_folder = False, name = copiedName, path =child.path, parent = parent_id, user_id= child.user_id, size=child.size, status=child.status)
121 + copiedItem.save()
122 +
123 + copiedItem = Item.objects.filter(name = copiedName)
124 + copied_data = serializers.serialize("json", copiedItem)
125 + json_data = json.loads(copied_data)
126 + res = json_data[0]['fields']
127 + res['id'] = json_data[0]['pk']
128 + parent = Item.objects.filter(item_id = parent_id)
129 + parent_data = serializers.serialize("json", parent)
130 + json_parent = json.loads(parent_data)[0]['fields']
131 + res['parentInfo'] = json_parent
132 + return Response({'data': res}, status=status.HTTP_200_OK)
133 + if parent == None:
134 + return Response({'message': 'parent is not existed.'}, status=status.HTTP_200_OK)
135 + if parent.is_folder == False:
136 + return Response({'message': 'parent is not folder.'}, status=status.HTTP_200_OK)
137 + return Response({'message': 'item is not existed.'}, status=status.HTTP_204_NO_CONTENT)
105 138
106 def get_permissions(self): 139 def get_permissions(self):
107 try: 140 try:
......
...@@ -32,5 +32,6 @@ urlpatterns = [ ...@@ -32,5 +32,6 @@ urlpatterns = [
32 url(r'^search/$', views.ItemViewSet.search, name='search'), 32 url(r'^search/$', views.ItemViewSet.search, name='search'),
33 url(r'^<int:pk>/share/$', views.SharedItemViewSet.share, name='share'), 33 url(r'^<int:pk>/share/$', views.SharedItemViewSet.share, name='share'),
34 url(r'^<int:pk>/move/$', views.ItemViewSet.move, name='move'), 34 url(r'^<int:pk>/move/$', views.ItemViewSet.move, name='move'),
35 + url(r'^<int:pk>/copy/$', views.ItemViewSet.copy, name='copy'),
35 36
36 ] 37 ]
......