신은섭(Shin Eun Seop)

Merge branch 'feature/#10' into feature/user

kairos03/2018-1-d.cloud#10
......@@ -23,7 +23,6 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = '534f6m=i@*)=q3kuwlge1m3c+@^cabr3ttcx*omv^+dorydjfr'
# SECURITY WARNING: don't run with debug turned on in production!
# TODO
DEBUG = True
ALLOWED_HOSTS = []
......@@ -107,7 +106,7 @@ AUTH_PASSWORD_VALIDATORS = [
# Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'ko_kr'
TIME_ZONE = 'UTC'
......
......@@ -2,7 +2,6 @@ from django.contrib import admin
from django.conf.urls import url, include
from django.contrib.auth import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^accounts/login/$', views.login, name='login'),
......@@ -10,3 +9,4 @@ urlpatterns = [
url(r'^restapi/', include('restful.urls')),
url(r'^', include('website.urls')),
]
......
#!/bin/bash
rm -f db.sqlite3
rm -r restful/migrations
python manage.py makemigrations restful
python manage.py migrate
from django.db import models
# Create your models here.
class File(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
title = models.CharField(max_length=100)
# file_name = models.CharField(max_length=100, primary_key=True)
object_key = models.CharField(max_length=1025)
size = models.IntegerField()
# owner = models.ForeignKey('auth.User', related_name='snippets', on_delete=models.CASCADE)
class Meta:
ordering = ('title',)
ordering = ('pk',)
......
import boto3
import json
S3 = boto3.client('s3')
BUCKET = '2018-dcloud'
def list_path(bucket, user, path):
files = []
# get list
objects = S3.list_objects(Bucket=bucket, Prefix='{}/{}'.format(user, path), Delimiter='/')
# get sub directorys
common_prefixes = objects.get('CommonPrefixes')
if common_prefixes:
for obj in common_prefixes:
files.append({'type':'diretory', 'name':obj.get('Prefix').split('/')[-2]})
# get files
contents = objects.get('Contents')
if contents:
for obj in contents:
file = obj.get('Key').split('/')[-1]
if file != '':
files.append({'type':'file', 'name':file})
return {'files':files}
# print(list_path(BUCKET, 'test1', ''))
\ No newline at end of file
......@@ -3,30 +3,8 @@ from rest_framework import serializers
from restful.models import File
class FileSerializer(serializers.Serializer):
pk = serializers.IntegerField(read_only=True)
created = serializers.DateTimeField(read_only=True)
modified = serializers.DateTimeField(read_only=True)
title = serializers.CharField(max_length=100)
object_key = serializers.CharField(max_length=1025)
size = serializers.IntegerField()
class FileSerializer(serializers.ModelSerializer):
def create(self, validated_data):
"""
Create and Return new `File` instance. Using validated_data.
"""
return File.objects.create(**validated_data)
def update(self, instance, validated_data):
"""
Update and Return existing `File` instance. Using validated_data.
"""
instance.title = validated_data.get('title', instance.title)
instance.object_key = validated_data.get('object_key', instance.object_key)
instance.size = validated_data.get('size', instance.size)
instance.language = validated_data.get('language', instance.language)
instance.style = validated_data.get('style', instance.style)
instance.save()
return instance
class Meta:
model = File
fields = ('created', 'updated', 'object_key')
......
from django.test import TestCase
from rest_framework.test import APITestCase
from django.urls import reverse
from rest_framework import status
from restful.models import File
class FileListTestCase(APITestCase):
def setUp(self):
self.tearDown()
def tearDown(self):
pass
def test_upload(self):
url = reverse('file-list')
data = {'object_key': 'test_object_key'}
response = self.client.post(url, data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(File.objects.count(), 1)
def test_list(self):
url = reverse('file-list')
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
class FileDetailTestCase(APITestCase):
def setUp(self):
self.tearDown()
File.objects.create(object_key='test_object')
def tearDown(self):
File.objects.all().delete()
def test_delete(self):
url = reverse('file-detail', kwargs={'pk' : 1 })
response = self.client.delete(url)
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
def test_update(self):
url = reverse('file-detail', kwargs={'pk' : 1 })
response = self.client.put(url, {"object_key":"test_update"})
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
def test_retrieve(self):
url = reverse('file-detail', kwargs={'pk' : 1 })
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
\ No newline at end of file
# Create your tests here.
......
from django.conf.urls import url
from django.shortcuts import redirect
from rest_framework.urlpatterns import format_suffix_patterns
from restful import views
urlpatterns = [
url(r'^files/$', views.FileList.as_view()),
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()),
]
......
......@@ -3,6 +3,7 @@ from django.contrib.auth.decorators import login_required
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from restful import s3_interface
from restful.models import File
from restful.serializers import FileSerializer
......@@ -12,10 +13,14 @@ class FileList(APIView):
List all file, or create a new snippet.
"""
def get(self, request, format=None):
file = File.objects.all()
serializer = FileSerializer(file, many=True)
return Response(serializer.data)
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)
return Response(data)
def post(self, request, format=None):
serializer = FileSerializer(data=request.data)
......@@ -38,14 +43,14 @@ class FileDetail(APIView):
def get(self, request, pk, format=None):
file = self.get_object(pk)
serializer = FileSerializer(file)
return Response(serializer.data)
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)
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):
......