신은섭(Shin Eun Seop)

Merge branch 'develop' into feature/ui

......@@ -105,3 +105,9 @@ ENV/
# mypy
.mypy_cache/
# aws configutation file
aws_conf.py
# media file
**/media/*
\ No newline at end of file
......
"""
Django settings for dcloud project.
Generated by 'django-admin startproject' using Django 2.0.4.
For more information on this file, see
https://docs.djangoproject.com/en/2.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.0/ref/settings/
"""
import os
from dcloud import aws_conf
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '534f6m=i@*)=q3kuwlge1m3c+@^cabr3ttcx*omv^+dorydjfr'
......@@ -27,9 +12,7 @@ DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
......@@ -39,7 +22,8 @@ INSTALLED_APPS = [
'django.contrib.staticfiles',
'rest_framework',
'restful.apps.RestfulConfig',
'website'
'website',
's3direct',
]
MIDDLEWARE = [
......@@ -123,6 +107,59 @@ USE_TZ = True
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
# Login redirect
LOGIN_REDIRECT_URL = '/'
\ No newline at end of file
LOGIN_REDIRECT_URL = '/'
# AWS
# If these are not defined, the EC2 instance profile and IAM role are used.
# This requires you to add boto3 (or botocore, which is a dependency of boto3)
# to your project dependencies.
AWS_ACCESS_KEY_ID = aws_conf.AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY = aws_conf.AWS_SECRET_ACCESS_KEY
AWS_STORAGE_BUCKET_NAME = aws_conf.AWS_STORAGE_BUCKET_NAME
# The region of your bucket, more info:
# http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region
S3DIRECT_REGION = 'ap-northeast-2'
# Destinations, with the following keys:
#
# key [required] Where to upload the file to, can be either:
# 1. '/' = Upload to root with the original filename.
# 2. 'some/path' = Upload to some/path with the original filename.
# 3. functionName = Pass a function and create your own path/filename.
# key_args [optional] Arguments to be passed to 'key' if it's a function.
# auth [optional] An ACL function to whether the current Django user can perform this action.
# allowed [optional] List of allowed MIME types.
# acl [optional] Give the object another ACL rather than 'public-read'.
# cache_control [optional] Cache control headers, eg 'max-age=2592000'.
# content_disposition [optional] Useful for sending files as attachments.
# bucket [optional] Specify a different bucket for this particular object.
# server_side_encryption [optional] Encryption headers for buckets that require it.
S3DIRECT_DESTINATIONS = {
'example_destination': {
# REQUIRED
'key': '/',
# OPTIONAL
#'auth': lambda u: u.is_staff, # Default allow anybody to upload
#'allowed': ['image/jpeg', 'image/png', 'video/mp4'], # Default allow all mime types
#'bucket': 'pdf-bucket', # Default is 'AWS_STORAGE_BUCKET_NAME'
'acl': 'private', # Defaults to 'public-read'
'cache_control': 'max-age=2592000', # Default no cache-control
#'content_disposition': 'attachment', # Default no content disposition
#'content_length_range': (5000, 20000000), # Default allow any size
#'server_side_encryption': 'AES256', # Default no encryption
},
'example_other': {
'key': lambda filename, args: args + '/' + filename,
'key_args': 'uploads/images', # Only if 'key' is a function
}
}
......
......@@ -10,5 +10,6 @@ urlpatterns = [
url(r'^accounts/login/$', views.login, name='login'),
url(r'^accounts/logout/$', views.logout, name='logout', kwargs={'next_page': '/'}),
url(r'^s3direct/', include('s3direct.urls')),
]
......
......@@ -2,12 +2,13 @@ from django.db import models
# Create your models here.
class File(models.Model):
file = models.FileField(blank=False, null=False)
# title = models.CharField(max_length=100)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
title = models.CharField(max_length=100)
# modified = models.DateTimeField(auto_now=True)
# file_name = models.CharField(max_length=100, primary_key=True)
object_key = models.CharField(max_length=1025)
# object_key = models.CharField(max_length=1025)
# owner = models.ForeignKey('auth.User', related_name='snippets', on_delete=models.CASCADE)
class Meta:
......
......@@ -27,4 +27,24 @@ def list_path(bucket, user, path):
return {'files':files}
# print(list_path(BUCKET, 'test1', ''))
\ No newline at end of file
def upload_file(bucket, user, local_path, key):
return S3.upload_file(local_path, bucket, user+"/"+key)
def download_file(bucket, user, local_path, key):
return S3.download_file(bucket, user+"/"+key, local_path)
def delete_path(bucket, user, path):
return S3.delete_object(Bucket=bucket, Key=user+"/"+path)
def make_directory(bucket, user, path):
return S3.put_object(Bucket=BUCKET, Key=user+"/"+path)
#
def move_file(bucket, user, old_path, new_path):
S3.copy_object(Bucket=bucket, CopySource=bucket+"/"+user+"/"+old_path, Key=user+"/"+new_path)
S3.delete_object(Bucket=bucket, Key=user+"/"+old_path)
return
def copy_file(bucket, user, old_path, new_path):
S3.copy_object(Bucket=bucket, CopySource=bucket+"/"+user+"/"+old_path, Key=user+"/"+new_path)
return
......
......@@ -7,4 +7,4 @@ class FileSerializer(serializers.ModelSerializer):
class Meta:
model = File
fields = ('created', 'updated', 'object_key')
fields = ('file', 'created')
......
......@@ -3,9 +3,12 @@ from django.shortcuts import redirect
from rest_framework.urlpatterns import format_suffix_patterns
from restful import views
file_regex = '[\w\s가-힣.\`\'\˜\=\+\#\ˆ\@\$\&\-\.\(\)\{\}\;\[\]]'
urlpatterns = [
url(r'^files/(?P<path>([a-zA-z0-9가-힣._-]*/)*)$', views.FileList.as_view(), name='file-list'),
url(r'^files/(?P<pk>[0-9]+)/$', views.FileDetail.as_view()),
url(r'^list/(?P<path>([\w\s가-힣.\`\'\˜\=\+\#\ˆ\@\$\&\-\.\(\)\{\}\;\[\]]*/)*)$', views.FileList.as_view(), name='file-list'),
url(r'^file/(?P<path>([\w\s가-힣.\`\'\˜\=\+\#\ˆ\@\$\&\-\.\(\)\{\}\;\[\]]*/*)*)$', views.FileDetail.as_view(), name='file-detail'),
url(r'^file-mod/(?P<old_path>([\w\s가-힣.\`\'\˜\=\+\#\ˆ\@\$\&\-\.\(\)\{\}\;\[\]]*/*)*)&(?P<new_path>([\w\s가-힣.\`\'\˜\=\+\#\ˆ\@\$\&\-\.\(\)\{\}\;\[\]]]*/*)*)$', views.FileCopyMove.as_view(), name='file-copy-move')
]
urlpatterns = format_suffix_patterns(urlpatterns)
\ No newline at end of file
......
......@@ -13,48 +13,74 @@ class FileList(APIView):
List all file, or create a new snippet.
"""
def get(self, request, path='/', format=None):
# file = File.objects.all()
# serializer = FileSerializer(file, many=True)
# print(serializer.data)
# return Response(serializer.data)
data = s3_interface.list_path(s3_interface.BUCKET, 'test1', path)
"""
list files or view detail
"""
def get(self, request, path="/", format=None):
user = request.user
data = s3_interface.list_path(s3_interface.BUCKET, user.username, path)
return Response(data)
"""
upload file
"""
def post(self, request, path="/", format=None):
# file upload
# upload to server
file_serializer = FileSerializer(data=request.data)
if file_serializer.is_valid():
file_serializer.save()
# upload to s3
file_path = '.' + file_serializer.data.get('file')
user = request.user
data = s3_interface.upload_file(s3_interface.BUCKET, user.username, file_path, path+file_path.split('/')[-1])
# TODO upload check
# TODO remove local file
return Response(file_serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(file_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
return Response(file_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
"""
make directory
"""
def put(self, request, path="/", format=None):
user = request.user
data = s3_interface.make_directory(s3_interface.BUCKET, user.username, path)
return Response(data, status=status.HTTP_201_CREATED)
def post(self, request, format=None):
serializer = FileSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class FileDetail(APIView):
"""
Download or delete a file instance.
"""
def get(self, request, path="/", format=None):
# download file from s3
file = 'media/'+path.split('/')[-1]
user = request.user
s3_interface.download_file(s3_interface.BUCKET, user.username, file, path)
# TODO error
return Response({'file': file})
class FileDetail(APIView):
def delete(self, request, path="/", format=None):
user = request.user
result = s3_interface.delete_path(s3_interface.BUCKET, user.username, path)
return Response(result)
class FileCopyMove(APIView):
"""
Download or delete a file instance.
"""
Retrieve, update or delete a file instance.
"""
def get_object(self, pk):
try:
return File.objects.get(pk=pk)
except File.DoesNotExist:
raise Http404
def get(self, request, pk, format=None):
file = self.get_object(pk)
serializer = FileSerializer(file)
return Response(serializer.data, status=status.HTTP_200_OK)
def put(self, request, pk, format=None):
file = self.get_object(pk)
serializer = FileSerializer(file, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_204_NO_CONTENT)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def delete(self, request, pk, format=None):
file = self.get_object(pk)
file.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
#TODO is folder move, copy well?
# move
def post(self, request, old_path, new_path, format=None):
user = request.user
if request.data.get('method') == 'mv':
s3_interface.move_file(s3_interface.BUCKET, user.username, old_path, new_path)
elif request.data.get('method') == 'cp':
s3_interface.copy_file(s3_interface.BUCKET, user.username, old_path, new_path)
else:
return Response({'stats': 'bad_request'}, status=status.HTTP_400_BAD_REQUEST)
return Response({'old_path': old_path, 'new_path': new_path})
......
......@@ -2,6 +2,7 @@ from django.contrib.auth import login, authenticate, logout
from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
def signup(request):
if request.method == 'POST':
......@@ -31,7 +32,10 @@ def delete_account(request):
@login_required
def delete_account_success(request):
if request.method == 'GET':
# TODO Add delete account
# delete account
u = User.objects.get(username = request.user.username)
u.delete()
# logout
logout(request)
return render(request, 'registration/delete_account_success.html')
......
from django import forms
from s3direct.widgets import S3DirectWidget
# class PostForm(forms.ModelForm):
# class Meta:
# model = Post
# fields = ('title', 'text')
\ No newline at end of file
class S3DirectUploadForm(forms.Form):
images = forms.URLField(widget=S3DirectWidget(dest='example_destination'))
......
from django.db import models
from django.utils import timezone
from s3direct.fields import S3DirectField
class Example(models.Model):
video = S3DirectField(dest='example_destination')
\ No newline at end of file
......
from django.shortcuts import render, get_object_or_404, redirect, Http404
from django.utils import timezone
from django.contrib.auth.decorators import login_required
from django.views.generic import FormView
from website.forms import S3DirectUploadForm
from restful.models import File
import requests
......@@ -11,6 +13,6 @@ def home(request):
@login_required
def file_list(request):
files = requests.get('http://localhost:8000/restapi/files')
files = requests.get('http://localhost:8000/restapi/list')
files = files.json()
return render(request, 'website/file_list.html', files)
......