views.py
8.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import mimetypes
import os
import boto3
from django.contrib.auth.models import User, Group
from django.http import HttpResponse, JsonResponse
from rest_framework import viewsets
from rest_framework import permissions
from django.views.decorators.csrf import csrf_exempt
from rest_framework import renderers
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
from api.serializers import UserSerializer,GroupSerializer,ItemSerializer
from rest_framework import generics
class UserViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
queryset = User.objects.all().order_by('-date_joined')
serializer_class = UserSerializer
permission_classes = [permissions.IsAuthenticated]
"""
def item_list(request):
if request.method == 'GET':
items = Item.objects.all()
serializer = ItemSerializer(items, many=True)
return JsonResponse(serializer.data, safe=False)
elif request.method == 'POST':
s3 = boto3.client('s3')
s3_bucket = os.environ.get('AWS_STORAGE_BUCKET_NAME')
file_name = request.GET['image_name']
file_type = mimetypes.guess_type(file_name)[0]
presigned_post = s3.generate_presigned_post(
Bucket=s3_bucket,
Key=file_name,
Fields={"acl": "private", "Content-Type": file_type},
Conditions=[
{"acl": "public-read"},
{"Content-Type": file_type}
],
ExpiresIn=3600
)
data = {
"signed_url": presigned_post,
'url': 'https://%s.s3.amazonaws.com/%s' % (s3_bucket, file_name)
}
return presigned_post['url']
def item_detail(request, pk):
try:
item = Item.objects.get(pk=pk)
except Item.DoesNotExist:
return HttpResponse(status=404)
if request.method == 'GET':
serializer = ItemSerializer(item)
return JsonResponse(serializer.data)
elif request.method == 'PUT':
s3 = boto3.client('s3')
s3_bucket = os.environ.get('AWS_STORAGE_BUCKET_NAME')
file_name = item.GET['image_name']
file_type = mimetypes.guess_type(file_name)[0]
presigned_post = s3.generate_presigned_post(
Bucket=s3_bucket,
Key=file_name,
Fields={"acl": "private", "Content-Type": file_type},
Conditions=[
{"acl": "public-read"},
{"Content-Type": file_type}
],
ExpiresIn=3600
)
data = {
"signed_url": presigned_post,
'url': 'https://%s.s3.amazonaws.com/%s' % (s3_bucket, file_name)
}
return presigned_post['url']
elif request.method == 'DELETE':
item.delete()
return HttpResponse(status=204)
"""
class ItemViewSet(viewsets.ModelViewSet):
queryset = Item.objects.all()
serializer_class = ItemSerializer
permission_classes = [permissions.IsAuthenticatedOrReadOnly, permissions.AllowAny,
#IsOwnerOrReadOnly
]
# url: /upload
@action(methods=['POST'], detail=True, permission_classes=[AllowAny],
url_path='upload', url_name='upload')
def upload(self, request, pk):
if request.method == 'POST':
s3 = boto3.client('s3')
#s3_bucket = os.environ.get('AWS_STORAGE_BUCKET_NAME')
s3_bucket = 'arn:aws:s3:::[s3id]'
#file_name = request.GET['image_name']
file_name = request.GET.get('image_name', '')
file_type = mimetypes.guess_type(file_name)[0]
presigned_post = s3.generate_presigned_post(
Bucket=s3_bucket,
Key=file_name,
Fields={"acl": "private", "Content-Type": file_type},
Conditions=[
{"acl": "public-read"},
{"Content-Type": file_type}
],
ExpiresIn=3600
)
data = {
"signed_url": presigned_post,
'url': 'https://%s.s3.amazonaws.com/%s' % (s3_bucket, file_name)
}
return presigned_post['url']
# url: /status
@action(methods=['POST'], detail=True, permission_classes=[AllowAny],
url_path='status', url_name='status')
def status(self, request):
if request.method == 'POST':
#name = request.POST['name']
name = request.POST.get('name', '')
up_time = request.POST.get('updated_time', '')
try:
item = Item.objects.get(name=name, updated_time=up_time)
except Item.DoesNotExist:
return Response({'Error': 'File Upload Error'}, status=status.HTTP_200_OK)
return Response({'Message': 'File Upload Successful'}, status=status.HTTP_200_OK)
"""
# url: /children
@action(methods=['POST'], detail=True, permission_classes=[AllowAny],
url_path='children', url_name='children')
def children(self, request):
if request.method == 'POST':
#name = request.POST['name']
name = request.POST.get('name', '')
path = request.POST.get('path', '')
parent = request.POST.get('item_id', '')
daughter = Item(name = name, path = path, parent = parent)
daughter.save()
"""
"""
class ItemViewSet(viewsets.ModelViewSet):
#API endpoint that allows groups to be viewed or edited.
queryset = Item.objects.all().order_by('-item_id')
serializer_class = ItemSerializer
permission_classes = [permissions.IsAuthenticated]
def item_list(self, request):
if request.method == 'GET':
items = Item.objects.all()
serializer = ItemSerializer(items, many=True)
return JsonResponse(serializer.data, safe=False)
elif request.method == 'POST':
s3 = boto3.client('s3')
s3_bucket = os.environ.get('AWS_STORAGE_BUCKET_NAME')
file_name = request.GET['image_name']
file_type = mimetypes.guess_type(file_name)[0]
presigned_post = s3.generate_presigned_post(
Bucket=s3_bucket,
Key=file_name,
Fields={"acl": "private", "Content-Type": file_type},
Conditions=[
{"acl": "public-read"},
{"Content-Type": file_type}
],
ExpiresIn=3600
)
data = {
"signed_url": presigned_post,
'url': 'https://%s.s3.amazonaws.com/%s' % (s3_bucket, file_name)
}
return presigned_post['url']
"""
"""
#url: /items/{item_id}/upload
@action(methods=['post'], detail=True, permission_classes=[IsAuthenticated],
url_path='upload', url_name='upload')
def upload(self, request):
s3 = boto3.client('s3')
s3_bucket = os.environ.get('AWS_STORAGE_BUCKET_NAME')
file_name = request.GET['image_name']
file_type = mimetypes.guess_type(file_name)[0]
presigned_post = s3.generate_presigned_post(
Bucket=s3_bucket,
Key=file_name,
Fields={"acl": "private", "Content-Type": file_type},
Conditions=[
{"acl": "public-read"},
{"Content-Type": file_type}
],
ExpiresIn=3600
)
data = {
"signed_url": presigned_post,
'url': 'https://%s.s3.amazonaws.com/%s' % (s3_bucket, file_name)
}
return presigned_post['url']
def upload(self, request):
if request.method == 'POST':
serializer = ItemSerializer(data=request.data)
if serializer.is_valid():
BUCKET_NAME = 'presigned-post-example'
KEY_NAME = 'cat.jpg'
s3 = boto3.client('s3')
resp = s3.generate_presigned_post(
Bucket = BUCKET_NAME,
Key = KEY_NAME,
)
resp['fields']['file'] = '@{key}'.format(key=KEY_NAME)
form_values = "\n".join(["-F {key}={value} \\".format(key=key, value=value)
for key, value in resp['fields'].items()])
# authentication_classes = (authentication.SessionAuthentication,)
# permission_classes = [IsAuthenticated, ]
"""