신은섭(Shin Eun Seop)

first file restful

# django
# DB
**/*.sqlite3
**/migrations
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
......
No preview for this file type
......@@ -20,7 +20,7 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'e#usa0n532(ev$(mn-k-f-jjnw6@ecq3g33v9n$rb$r4&nr(+%'
SECRET_KEY = '534f6m=i@*)=q3kuwlge1m3c+@^cabr3ttcx*omv^+dorydjfr'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
......@@ -38,6 +38,7 @@ INSTALLED_APPS = [
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'restful.apps.RestfulConfig'
]
MIDDLEWARE = [
......
"""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
urlpatterns = [
url('admin/', admin.site.urls),
url(r'^', include('restful.urls')),
]
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)
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',)
......
"""
serializers
"""
from django.forms import widgets
from rest_framework import serializers
from restful.models import File
from django.contrib.auth.models import User, Group
from rest_framework import serializers
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 UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('url', 'username', 'email', 'groups')
def create(self, validated_data):
"""
Create and Return new `File` instance. Using validated_data.
"""
return File.objects.create(**validated_data)
class GroupSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Group
fields = ('url', 'name')
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
......
from django.conf.urls import url
from rest_framework.urlpatterns import format_suffix_patterns
from restful import views
urlpatterns = [
url(r'^files/$', views.FileList.as_view()),
url(r'^files/(?P<pk>[0-9]+)/$', views.FileDetail.as_view()),
]
urlpatterns = format_suffix_patterns(urlpatterns)
\ No newline at end of file
"""
views
"""
from restful.models import File
from restful.serializers import FileSerializer
from django.http import Http404
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from django.contrib.auth.models import User, Group
from rest_framework import viewsets
from tutorial.quickstart.serializers import UserSerializer, GroupSerializer
class UserViewSet(viewsets.ModelViewSet):
# Create your views here.
class FileList(APIView):
"""
API endpoint that allows users to be viewed or edited.
List all file, or create a new snippet.
"""
queryset = User.objects.all().order_by('-date_joined')
serializer_class = UserSerializer
def get(self, request, format=None):
file = File.objects.all()
serializer = FileSerializer(file, many=True)
return Response(serializer.data)
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 GroupViewSet(viewsets.ModelViewSet):
class FileDetail(APIView):
"""
API endpoint that allows groups to be viewed or edited.
Retrieve, update or delete a file instance.
"""
queryset = Group.objects.all()
serializer_class = GroupSerializer
\ No newline at end of file
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)
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.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)
......
"""
urls
"""
from django.conf.urls import url, include
from rest_framework import routers
from tutorial.quickstart import views
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
url(r'^', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]