Toggle navigation
Toggle navigation
This project
Loading...
Sign in
2020-1-CloudComputing
/
C_Team_KhuDrive
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Graphs
Network
Create a new issue
Commits
Issue Boards
Authored by
권주희
2020-06-06 17:39:19 +0900
Browse Files
Options
Browse Files
Download
Plain Diff
Commit
08d51afc790484dfafb906a57251b68a8bb50766
08d51afc
2 parents
766de271
e3201e7d
Merge branch 'feature/item_api' into 'develop'
Feature/item api See merge request
!4
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
103 additions
and
218 deletions
backend/api/migrations/0003_auto_20200606_0817.py
backend/api/migrations/0004_auto_20200606_0824.py
backend/api/models.py
backend/api/serializers.py
backend/api/views.py
backend/khudrive/urls.py
backend/api/migrations/0003_auto_20200606_0817.py
0 → 100644
View file @
08d51af
# Generated by Django 3.0.6 on 2020-06-06 08:17
from
django.db
import
migrations
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'api'
,
'0002_auto_20200605_1851'
),
]
operations
=
[
migrations
.
RenameField
(
model_name
=
'shareditem'
,
old_name
=
'valid'
,
new_name
=
'expires'
,
),
]
backend/api/migrations/0004_auto_20200606_0824.py
0 → 100644
View file @
08d51af
# Generated by Django 3.0.6 on 2020-06-06 08:24
from
django.db
import
migrations
,
models
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'api'
,
'0003_auto_20200606_0817'
),
]
operations
=
[
migrations
.
AddField
(
model_name
=
'shareditem'
,
name
=
'created_time'
,
field
=
models
.
DateTimeField
(
auto_now
=
True
),
),
migrations
.
AddField
(
model_name
=
'user'
,
name
=
'created_time'
,
field
=
models
.
DateTimeField
(
auto_now
=
True
),
),
migrations
.
AlterField
(
model_name
=
'item'
,
name
=
'created_time'
,
field
=
models
.
DateTimeField
(
auto_now
=
True
),
),
]
backend/api/models.py
View file @
08d51af
...
...
@@ -11,7 +11,7 @@ class Item(models.Model):
user_id
=
models
.
IntegerField
()
size
=
models
.
IntegerField
()
is_deleted
=
models
.
BooleanField
(
default
=
False
)
created_time
=
models
.
DateTimeField
(
blank
=
Fals
e
)
created_time
=
models
.
DateTimeField
(
auto_now
=
Tru
e
)
updated_time
=
models
.
DateTimeField
()
status
=
models
.
BooleanField
()
...
...
@@ -24,9 +24,9 @@ class Item(models.Model):
class
SharedItem
(
models
.
Model
):
item_id
=
models
.
IntegerField
()
#file_id?
valid
=
models
.
DateTimeField
()
expires
=
models
.
DateTimeField
()
password
=
models
.
CharField
(
max_length
=
20
)
created_time
=
models
.
DateTimeField
(
auto_now
=
True
)
class
Meta
:
ordering
=
[
'item_id'
]
...
...
@@ -38,6 +38,6 @@ class User(models.Model):
password
=
models
.
CharField
(
max_length
=
20
)
total_size
=
models
.
IntegerField
()
current_size
=
models
.
IntegerField
()
created_time
=
models
.
DateTimeField
(
auto_now
=
True
)
class
Meta
:
ordering
=
[
'int_id'
]
...
...
backend/api/serializers.py
View file @
08d51af
from
django.contrib.auth.models
import
User
,
Group
from
rest_framework
import
serializers
from
api.models
import
Item
from
api.models
import
Item
,
SharedItem
class
UserSerializer
(
serializers
.
HyperlinkedModelSerializer
):
...
...
@@ -17,3 +17,8 @@ class ItemSerializer(serializers.ModelSerializer):
class
Meta
:
model
=
Item
fields
=
'__all__'
class
SharedItemSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
model
=
Item
fields
=
'__all__'
...
...
backend/api/views.py
View file @
08d51af
import
mimetypes
import
json
import
os
import
boto3
from
django.contrib.auth.models
import
User
,
Group
from
django.http
import
HttpResponse
,
JsonResponse
from
django.contrib.auth.models
import
User
from
django.core
import
serializers
from
django.views.decorators.csrf
import
csrf_exempt
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
api.models
import
Item
,
SharedItem
from
api.serializers
import
UserSerializer
,
GroupSerializer
,
ItemSerializer
,
SharedItemSerializer
from
rest_framework
import
generics
from
rest_framework
import
status
from
annoying.functions
import
get_object_or_None
class
UserViewSet
(
viewsets
.
ModelViewSet
):
"""
...
...
@@ -27,70 +28,6 @@ class UserViewSet(viewsets.ModelViewSet):
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
):
...
...
@@ -99,149 +36,44 @@ class ItemViewSet(viewsets.ModelViewSet):
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):
# url: items/search
@action
(
methods
=
[
'GET'
],
detail
=
False
,
permission_classes
=
[
AllowAny
],
url_path
=
'search'
,
url_name
=
'search'
)
def
search
(
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']
"""
keyword
=
request
.
GET
.
get
(
'keyword'
,
''
)
item_list
=
Item
.
objects
.
filter
(
name__icontains
=
keyword
)
"""
#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
=
serializers
.
serialize
(
"json"
,
item_list
)
return
Response
({
'data'
:
{
'list'
:
data
}},
status
=
status
.
HTTP_200_OK
)
data = {
"signed_url": presigned_post,
'url': 'https://
%
s.s3.amazonaws.com/
%
s'
%
(s3_bucket, file_name)
}
return presigned_post['url']
class
SharedItemViewSet
(
viewsets
.
ModelViewSet
):
def upload(self, request):
queryset
=
SharedItem
.
objects
.
all
()
serializer_class
=
SharedItemSerializer
permission_classes
=
[
permissions
.
IsAuthenticatedOrReadOnly
,
permissions
.
AllowAny
,
# IsOwnerOrReadOnly
]
# url: http://localhost:8000/items/1/share/
# 마지막 slash도 써주어야함
@csrf_exempt
@action
(
methods
=
[
'POST'
],
detail
=
True
,
permission_classes
=
[
AllowAny
],
url_path
=
'share'
,
url_name
=
'share'
)
def
share
(
self
,
request
,
pk
):
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, ]
"""
password
=
request
.
POST
.
get
(
'password'
,
''
)
expires
=
request
.
POST
.
get
(
'expires'
,
''
)
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
)
sharedfile
=
SharedItem
(
item_id
=
pk
,
password
=
password
,
expires
=
expires
)
sharedfile
.
save
()
sharedfile
=
SharedItem
.
objects
.
get
(
item_id
=
pk
)
# sf = serializers.serialize("json", sharedfile)
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
...
...
backend/khudrive/urls.py
View file @
08d51af
...
...
@@ -22,12 +22,14 @@ from django.conf.urls import url
router
=
routers
.
DefaultRouter
()
router
.
register
(
r'users'
,
views
.
UserViewSet
)
router
.
register
(
r'items'
,
views
.
ItemViewSet
)
router
.
register
(
r'items'
,
views
.
SharedItemViewSet
)
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns
=
[
path
(
'admin/'
,
admin
.
site
.
urls
),
path
(
''
,
include
(
router
.
urls
)),
url
(
r'^upload/$'
,
views
.
ItemViewSet
.
upload
,
name
=
'upload'
),
url
(
r'^status/$'
,
views
.
ItemViewSet
.
status
,
name
=
'status'
),
url
(
r'^search/$'
,
views
.
ItemViewSet
.
search
,
name
=
'search'
),
url
(
r'^<int:pk>/share/$'
,
views
.
SharedItemViewSet
.
share
,
name
=
'share'
),
]
...
...
Please
register
or
login
to post a comment