신은섭(Shin Eun Seop)
Committed by GitHub

Merge pull request #22 from kairos03/feature/user

Feature/user
......@@ -38,7 +38,8 @@ INSTALLED_APPS = [
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'restful.apps.RestfulConfig'
'restful.apps.RestfulConfig',
'website'
]
MIDDLEWARE = [
......@@ -105,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'
......@@ -120,3 +121,8 @@ USE_TZ = True
# https://docs.djangoproject.com/en/2.0/howto/static-files/
STATIC_URL = '/static/'
# Login redirect
LOGIN_REDIRECT_URL = '/'
\ No newline at end of file
......
"""dcloud URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.conf.urls import url, include
from django.contrib.auth import views
urlpatterns = [
url('admin/', admin.site.urls),
url(r'^', include('restful.urls')),
url(r'^admin/', admin.site.urls),
url(r'^restapi/', include('restful.urls')),
url(r'^', include('website.urls')),
url(r'^accounts/login/$', views.login, name='login'),
url(r'^accounts/logout/$', views.logout, name='logout', kwargs={'next_page': '/'}),
]
......
#!/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
# Create your tests here.
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
......
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()),
]
......
from restful.models import File
from restful.serializers import FileSerializer
from django.http import Http404
from django.http import Http404
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
# Create your views here.
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):
......
from django.contrib import admin
# from website.models import Post
# admin.site.register(Post)
\ No newline at end of file
from django.apps import AppConfig
class WebsiteConfig(AppConfig):
name = 'website'
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
def signup(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
raw_password = form.cleaned_data.get('password1')
user = authenticate(username=username, password=raw_password)
login(request, user)
return redirect('/')
else:
form = UserCreationForm()
return render(request, 'registration/signup.html', {'form': form})
@login_required
def delete_account(request):
if request.method == 'GET':
return render(request, 'registration/delete_account.html')
elif request.method == 'POST':
if request.POST.get('yes'):
return redirect('delete_account_success')
else:
return redirect('/')
@login_required
def delete_account_success(request):
if request.method == 'GET':
# TODO Add delete account
logout(request)
return render(request, 'registration/delete_account_success.html')
from django import forms
# class PostForm(forms.ModelForm):
# class Meta:
# model = Post
# fields = ('title', 'text')
\ No newline at end of file
from django.db import models
from django.utils import timezone
h1 a {
color: #FCA205;
font-family: 'Lobster';
}
body {
padding-left: 15px;
}
.page-header {
background-color: #ff9400;
margin-top: 0;
padding: 20px 20px 20px 40px;
}
.page-header h1, .page-header h1 a, .page-header h1 a:visited, .page-header h1 a:active {
color: #ffffff;
font-size: 36pt;
text-decoration: none;
}
.content {
margin-left: 40px;
}
h1, h2, h3, h4 {
font-family: 'Lobster', cursive;
}
.date {
color: #828282;
}
.save {
float: right;
}
.post-form textarea, .post-form input {
width: 100%;
}
.top-menu, .top-menu:hover, .top-menu:visited {
color: #ffffff;
float: right;
font-size: 26pt;
margin-right: 20px;
}
.post {
margin-bottom: 70px;
}
.post h1 a, .post h1 a:visited {
color: #000000;
}
\ No newline at end of file
{% extends "website/baseline.html" %}
{% block content %}
{% if user.is_authenticated %}
<h1> {{user.username}} really want to delete your account? </h1>
<form action='#' method="POST">
{% csrf_token %}
<input type="submit" value="yes" name="yes">
<input type="submit" value="no" name="no">
</form>
{% endif %}
{% endblock %}
\ No newline at end of file
{% extends "website/baseline.html" %}
{% block content %}
<h1>DELTED</h1>
<!--TODO AUTO logout -->
{% endblock %}
\ No newline at end of file
{% extends "website/baseline.html" %}
{% block content %}
{% if form.errors %}
<p>이름과 비밀번호가 일치하지 않습니다. 다시 시도해주세요.</p>
{% endif %}
<form method="post" action="{% url 'login' %}">
{% csrf_token %}
<table>
<tr>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>{{ form.password.label_tag }}</td>
<td>{{ form.password }}</td>
</tr>
</table>
<input type="submit" value="login" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
{% endblock %}
\ No newline at end of file
{% extends 'website/baseline.html' %} {% block content %}
<h2>Sign up</h2>
<form method="post">
{% csrf_token %} {% for field in form %}
<p>
{{ field.label_tag }}
<br> {{ field }} {% if field.help_text %}
<small style="color: grey">{% autoescape off %}{{ field.help_text }}{% endautoescape %}</small>
{% endif %} {% for error in field.errors %}
<p style="color: red">{{ error }}</p>
{% endfor %}
</p>
{% endfor %}
<button type="submit">Sign up</button>
</form>
{% endblock %}
\ No newline at end of file
<html>
<header>
<title>D.cloud</title>
</header>
<body>
<div class="page-header">
{% if user.is_authenticated %}
<p class="top-menu">Hello {{ user.username }} <small>(<a href="{% url 'logout' %}">Log out</a>)</small></p>
{% else %}
<a href="{% url 'login' %}" class="top-menu">Log in<span class="glyphicon glyphicon-lock"></span></a>
{% endif %}
<h1><a href="/">D.cloud</a></h1>
</div>
<div class="content">
{% block content %}
{% endblock %}
</div>
<div class="page-footer">
</div>
</body>
</html>
\ No newline at end of file
{% extends 'website/baseline.html' %}
{% block content %}
{% for file in files %}
<h1>{{file.name}}</h1>
<p>{{file.type}}</p>
{% endfor %}
{% endblock %}
\ No newline at end of file
{% extends 'website/baseline.html' %}
{% block content %}
<h1> Hello </h1>
{% endblock %}
\ No newline at end of file
from django.test import TestCase
# Create your tests here.
from django.conf.urls import url
from django.shortcuts import redirect
from website import views, auth_views
urlpatterns = [
url(r'^accounts/signup/$', auth_views.signup, name='signup'),
url(r'^accounts/delete_account/$', auth_views.delete_account, name='delete_account'),
url(r'^accounts/delete_account_success/$', auth_views.delete_account_success, name='delete_account_success'),
# blog
url(r'^$', views.home),
url(r'^files/', views.file_list, name='file_list'),
]
\ 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 restful.models import File
import requests
def home(request):
return render(request, 'website/home.html')
@login_required
def file_list(request):
files = requests.get('http://localhost:8000/restapi/files')
files = files.json()
return render(request, 'website/file_list.html', files)