권주희

implement share API by item_id

1 +# Generated by Django 3.0.6 on 2020-06-06 08:17
2 +
3 +from django.db import migrations
4 +
5 +
6 +class Migration(migrations.Migration):
7 +
8 + dependencies = [
9 + ('api', '0002_auto_20200605_1851'),
10 + ]
11 +
12 + operations = [
13 + migrations.RenameField(
14 + model_name='shareditem',
15 + old_name='valid',
16 + new_name='expires',
17 + ),
18 + ]
1 +# Generated by Django 3.0.6 on 2020-06-06 08:24
2 +
3 +from django.db import migrations, models
4 +
5 +
6 +class Migration(migrations.Migration):
7 +
8 + dependencies = [
9 + ('api', '0003_auto_20200606_0817'),
10 + ]
11 +
12 + operations = [
13 + migrations.AddField(
14 + model_name='shareditem',
15 + name='created_time',
16 + field=models.DateTimeField(auto_now=True),
17 + ),
18 + migrations.AddField(
19 + model_name='user',
20 + name='created_time',
21 + field=models.DateTimeField(auto_now=True),
22 + ),
23 + migrations.AlterField(
24 + model_name='item',
25 + name='created_time',
26 + field=models.DateTimeField(auto_now=True),
27 + ),
28 + ]
...@@ -11,7 +11,7 @@ class Item(models.Model): ...@@ -11,7 +11,7 @@ class Item(models.Model):
11 user_id = models.IntegerField() 11 user_id = models.IntegerField()
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(blank = False) 14 + created_time = models.DateTimeField(auto_now=True)
15 updated_time = models.DateTimeField() 15 updated_time = models.DateTimeField()
16 status = models.BooleanField() 16 status = models.BooleanField()
17 17
...@@ -24,9 +24,9 @@ class Item(models.Model): ...@@ -24,9 +24,9 @@ class Item(models.Model):
24 class SharedItem(models.Model): 24 class SharedItem(models.Model):
25 item_id = models.IntegerField() 25 item_id = models.IntegerField()
26 #file_id? 26 #file_id?
27 - valid = models.DateTimeField() 27 + expires = models.DateTimeField()
28 password = models.CharField(max_length = 20) 28 password = models.CharField(max_length = 20)
29 - 29 + created_time = models.DateTimeField(auto_now=True)
30 class Meta: 30 class Meta:
31 ordering = ['item_id'] 31 ordering = ['item_id']
32 32
...@@ -38,6 +38,6 @@ class User(models.Model): ...@@ -38,6 +38,6 @@ class User(models.Model):
38 password = models.CharField(max_length = 20) 38 password = models.CharField(max_length = 20)
39 total_size = models.IntegerField() 39 total_size = models.IntegerField()
40 current_size = models.IntegerField() 40 current_size = models.IntegerField()
41 - 41 + created_time = models.DateTimeField(auto_now=True)
42 class Meta: 42 class Meta:
43 ordering = ['int_id'] 43 ordering = ['int_id']
......
...@@ -5,11 +5,9 @@ import boto3 ...@@ -5,11 +5,9 @@ import boto3
5 5
6 from django.contrib.auth.models import User 6 from django.contrib.auth.models import User
7 from django.core import serializers 7 from django.core import serializers
8 -from django.http import HttpResponse, JsonResponse 8 +from django.views.decorators.csrf import csrf_exempt
9 from rest_framework import viewsets 9 from rest_framework import viewsets
10 from rest_framework import permissions 10 from rest_framework import permissions
11 -from django.views.decorators.csrf import csrf_exempt
12 -from rest_framework import renderers
13 from rest_framework.views import APIView 11 from rest_framework.views import APIView
14 from rest_framework.response import Response 12 from rest_framework.response import Response
15 from rest_framework.decorators import action 13 from rest_framework.decorators import action
...@@ -17,9 +15,10 @@ from rest_framework.parsers import JSONParser ...@@ -17,9 +15,10 @@ from rest_framework.parsers import JSONParser
17 from rest_framework.permissions import IsAuthenticated, AllowAny 15 from rest_framework.permissions import IsAuthenticated, AllowAny
18 16
19 from api.models import Item, SharedItem 17 from api.models import Item, SharedItem
20 -from api.serializers import UserSerializer,GroupSerializer,ItemSerializer 18 +from api.serializers import UserSerializer,GroupSerializer,ItemSerializer,SharedItemSerializer
21 from rest_framework import generics 19 from rest_framework import generics
22 from rest_framework import status 20 from rest_framework import status
21 +from annoying.functions import get_object_or_None
23 22
24 class UserViewSet(viewsets.ModelViewSet): 23 class UserViewSet(viewsets.ModelViewSet):
25 """ 24 """
...@@ -37,48 +36,6 @@ class ItemViewSet(viewsets.ModelViewSet): ...@@ -37,48 +36,6 @@ class ItemViewSet(viewsets.ModelViewSet):
37 permission_classes = [permissions.IsAuthenticatedOrReadOnly, permissions.AllowAny, 36 permission_classes = [permissions.IsAuthenticatedOrReadOnly, permissions.AllowAny,
38 #IsOwnerOrReadOnly 37 #IsOwnerOrReadOnly
39 ] 38 ]
40 - # url: /upload
41 - @action(methods=['POST'], detail=True, permission_classes=[AllowAny],
42 - url_path='upload', url_name='upload')
43 - def upload(self, request, pk):
44 - if request.method == 'POST':
45 - s3 = boto3.client('s3')
46 - #s3_bucket = os.environ.get('AWS_STORAGE_BUCKET_NAME')
47 - s3_bucket = 'arn:aws:s3:::[s3id]'
48 - #file_name = request.GET['image_name']
49 - file_name = request.GET.get('image_name', '')
50 - file_type = mimetypes.guess_type(file_name)[0]
51 - presigned_post = s3.generate_presigned_post(
52 - Bucket=s3_bucket,
53 - Key=file_name,
54 - Fields={"acl": "private", "Content-Type": file_type},
55 - Conditions=[
56 - {"acl": "public-read"},
57 - {"Content-Type": file_type}
58 - ],
59 - ExpiresIn=3600
60 - )
61 -
62 - data = {
63 - "signed_url": presigned_post,
64 - 'url': 'https://%s.s3.amazonaws.com/%s' % (s3_bucket, file_name)
65 - }
66 - return presigned_post['url']
67 -
68 - # url: /status
69 - @action(methods=['POST'], detail=True, permission_classes=[AllowAny],
70 - url_path='status', url_name='status')
71 - def status(self, request):
72 - if request.method == 'POST':
73 - #name = request.POST['name']
74 - name = request.POST.get('name', '')
75 - up_time = request.POST.get('updated_time', '')
76 - try:
77 - item = Item.objects.get(name=name, updated_time=up_time)
78 - except Item.DoesNotExist:
79 - return Response({'Error': 'File Upload Error'}, status=status.HTTP_200_OK)
80 - return Response({'Message': 'File Upload Successful'}, status=status.HTTP_200_OK)
81 -
82 39
83 # url: items/search 40 # url: items/search
84 @action(methods=['GET'], detail=False, permission_classes=[AllowAny], url_path='search', url_name='search') 41 @action(methods=['GET'], detail=False, permission_classes=[AllowAny], url_path='search', url_name='search')
...@@ -90,3 +47,33 @@ class ItemViewSet(viewsets.ModelViewSet): ...@@ -90,3 +47,33 @@ class ItemViewSet(viewsets.ModelViewSet):
90 data = serializers.serialize("json", item_list) 47 data = serializers.serialize("json", item_list)
91 return Response({'data': {'list' : data}}, status=status.HTTP_200_OK) 48 return Response({'data': {'list' : data}}, status=status.HTTP_200_OK)
92 49
50 +class SharedItemViewSet(viewsets.ModelViewSet):
51 +
52 + queryset = SharedItem.objects.all()
53 + serializer_class = SharedItemSerializer
54 + permission_classes = [permissions.IsAuthenticatedOrReadOnly, permissions.AllowAny,
55 + # IsOwnerOrReadOnly
56 + ]
57 + # url: http://localhost:8000/items/1/share/
58 + # 마지막 slash도 써주어야함
59 + @csrf_exempt
60 + @action(methods=['POST'], detail=True, permission_classes=[AllowAny], url_path='share', url_name='share')
61 + def share(self, request, pk):
62 + if request.method == 'POST':
63 + password = request.POST.get('password', '')
64 + expires = request.POST.get('expires', '')
65 +
66 + sharedfile = get_object_or_None(SharedItem, item_id=pk)
67 + if sharedfile != None:
68 + # 서버는 정상이나 이미 공유객체로 등록된 파일임
69 + return Response({'Message': 'This file is already shared'}, status=status.HTTP_200_OK)
70 + sharedfile = SharedItem(item_id =pk, password=password, expires = expires)
71 + sharedfile.save()
72 + sharedfile = SharedItem.objects.get(item_id = pk)
73 +
74 + # sf = serializers.serialize("json", sharedfile)
75 + item = Item.objects.filter(item_id = pk)
76 + item_json = serializers.serialize("json", item)
77 +
78 + # data = serializers.serialize("json", item_list)
79 + return Response({"shared": sharedfile.created_time , 'data': item_json}, status=status.HTTP_200_OK)
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -22,14 +22,14 @@ from django.conf.urls import url ...@@ -22,14 +22,14 @@ from django.conf.urls import url
22 router = routers.DefaultRouter() 22 router = routers.DefaultRouter()
23 router.register(r'users', views.UserViewSet) 23 router.register(r'users', views.UserViewSet)
24 router.register(r'items', views.ItemViewSet) 24 router.register(r'items', views.ItemViewSet)
25 +router.register(r'items', views.SharedItemViewSet)
25 26
26 # Wire up our API using automatic URL routing. 27 # Wire up our API using automatic URL routing.
27 # Additionally, we include login URLs for the browsable API. 28 # Additionally, we include login URLs for the browsable API.
28 urlpatterns = [ 29 urlpatterns = [
29 path('admin/', admin.site.urls), 30 path('admin/', admin.site.urls),
30 path('', include(router.urls)), 31 path('', include(router.urls)),
31 - url(r'^upload/$', views.ItemViewSet.upload, name='upload'),
32 - url(r'^status/$', views.ItemViewSet.status, name='status'),
33 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'),
34 34
35 ] 35 ]
......