신은섭(Shin Eun Seop)

first file restful

1 +# django
2 +# DB
3 +**/*.sqlite3
4 +**/migrations
5 +
6 +
1 # Byte-compiled / optimized / DLL files 7 # Byte-compiled / optimized / DLL files
2 __pycache__/ 8 __pycache__/
3 *.py[cod] 9 *.py[cod]
......
No preview for this file type
...@@ -20,7 +20,7 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) ...@@ -20,7 +20,7 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
20 # See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/ 20 # See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
21 21
22 # SECURITY WARNING: keep the secret key used in production secret! 22 # SECURITY WARNING: keep the secret key used in production secret!
23 -SECRET_KEY = 'e#usa0n532(ev$(mn-k-f-jjnw6@ecq3g33v9n$rb$r4&nr(+%' 23 +SECRET_KEY = '534f6m=i@*)=q3kuwlge1m3c+@^cabr3ttcx*omv^+dorydjfr'
24 24
25 # SECURITY WARNING: don't run with debug turned on in production! 25 # SECURITY WARNING: don't run with debug turned on in production!
26 DEBUG = True 26 DEBUG = True
...@@ -38,6 +38,7 @@ INSTALLED_APPS = [ ...@@ -38,6 +38,7 @@ INSTALLED_APPS = [
38 'django.contrib.messages', 38 'django.contrib.messages',
39 'django.contrib.staticfiles', 39 'django.contrib.staticfiles',
40 'rest_framework', 40 'rest_framework',
41 + 'restful.apps.RestfulConfig'
41 ] 42 ]
42 43
43 MIDDLEWARE = [ 44 MIDDLEWARE = [
......
1 +"""dcloud URL Configuration
2 +
3 +The `urlpatterns` list routes URLs to views. For more information please see:
4 + https://docs.djangoproject.com/en/2.0/topics/http/urls/
5 +Examples:
6 +Function views
7 + 1. Add an import: from my_app import views
8 + 2. Add a URL to urlpatterns: path('', views.home, name='home')
9 +Class-based views
10 + 1. Add an import: from other_app.views import Home
11 + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
12 +Including another URLconf
13 + 1. Import the include() function: from django.urls import include, path
14 + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
15 +"""
16 +from django.contrib import admin
17 +from django.conf.urls import url, include
18 +
19 +urlpatterns = [
20 + url('admin/', admin.site.urls),
21 + url(r'^', include('restful.urls')),
22 +]
1 from django.db import models 1 from django.db import models
2 2
3 +
3 # Create your models here. 4 # Create your models here.
5 +class File(models.Model):
6 + created = models.DateTimeField(auto_now_add=True)
7 + modified = models.DateTimeField(auto_now=True)
8 + title = models.CharField(max_length=100)
9 + object_key = models.CharField(max_length=1025)
10 + size = models.IntegerField()
11 + # owner = models.ForeignKey('auth.User', related_name='snippets', on_delete=models.CASCADE)
12 +
13 + class Meta:
14 + ordering = ('title',)
15 +
16 +
......
1 -""" 1 +from django.forms import widgets
2 -serializers 2 +from rest_framework import serializers
3 -""" 3 +from restful.models import File
4 4
5 -from django.contrib.auth.models import User, Group
6 -from rest_framework import serializers
7 5
6 +class FileSerializer(serializers.Serializer):
7 + pk = serializers.IntegerField(read_only=True)
8 + created = serializers.DateTimeField(read_only=True)
9 + modified = serializers.DateTimeField(read_only=True)
10 + title = serializers.CharField(max_length=100)
11 + object_key = serializers.CharField(max_length=1025)
12 + size = serializers.IntegerField()
8 13
9 -class UserSerializer(serializers.HyperlinkedModelSerializer):
10 - class Meta:
11 - model = User
12 - fields = ('url', 'username', 'email', 'groups')
13 14
15 + def create(self, validated_data):
16 + """
17 + Create and Return new `File` instance. Using validated_data.
18 + """
19 + return File.objects.create(**validated_data)
14 20
15 -class GroupSerializer(serializers.HyperlinkedModelSerializer): 21 +
16 - class Meta: 22 + def update(self, instance, validated_data):
17 - model = Group 23 + """
18 - fields = ('url', 'name') 24 + Update and Return existing `File` instance. Using validated_data.
25 + """
26 + instance.title = validated_data.get('title', instance.title)
27 + instance.object_key = validated_data.get('object_key', instance.object_key)
28 + instance.size = validated_data.get('size', instance.size)
29 + instance.language = validated_data.get('language', instance.language)
30 + instance.style = validated_data.get('style', instance.style)
31 + instance.save()
32 + return instance
......
1 +from django.conf.urls import url
2 +from rest_framework.urlpatterns import format_suffix_patterns
3 +from restful import views
4 +
5 +urlpatterns = [
6 + url(r'^files/$', views.FileList.as_view()),
7 + url(r'^files/(?P<pk>[0-9]+)/$', views.FileDetail.as_view()),
8 +]
9 +
10 +urlpatterns = format_suffix_patterns(urlpatterns)
...\ No newline at end of file ...\ No newline at end of file
1 -""" 1 +from restful.models import File
2 -views 2 +from restful.serializers import FileSerializer
3 -""" 3 +from django.http import Http404
4 +from rest_framework.views import APIView
5 +from rest_framework.response import Response
6 +from rest_framework import status
4 7
5 -from django.contrib.auth.models import User, Group
6 -from rest_framework import viewsets
7 -from tutorial.quickstart.serializers import UserSerializer, GroupSerializer
8 8
9 - 9 +# Create your views here.
10 -class UserViewSet(viewsets.ModelViewSet): 10 +class FileList(APIView):
11 """ 11 """
12 - API endpoint that allows users to be viewed or edited. 12 + List all file, or create a new snippet.
13 """ 13 """
14 - queryset = User.objects.all().order_by('-date_joined')
15 - serializer_class = UserSerializer
16 14
15 + def get(self, request, format=None):
16 + file = File.objects.all()
17 + serializer = FileSerializer(file, many=True)
18 + return Response(serializer.data)
19 +
20 + def post(self, request, format=None):
21 + serializer = FileSerializer(data=request.data)
22 + if serializer.is_valid():
23 + serializer.save()
24 + return Response(serializer.data, status=status.HTTP_201_CREATED)
25 + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
17 26
18 -class GroupViewSet(viewsets.ModelViewSet): 27 +
28 +class FileDetail(APIView):
19 """ 29 """
20 - API endpoint that allows groups to be viewed or edited. 30 + Retrieve, update or delete a file instance.
21 """ 31 """
22 - queryset = Group.objects.all()
23 - serializer_class = GroupSerializer
...\ No newline at end of file ...\ No newline at end of file
32 + def get_object(self, pk):
33 + try:
34 + return File.objects.get(pk=pk)
35 + except File.DoesNotExist:
36 + raise Http404
37 +
38 + def get(self, request, pk, format=None):
39 + file = self.get_object(pk)
40 + serializer = FileSerializer(file)
41 + return Response(serializer.data)
42 +
43 + def put(self, request, pk, format=None):
44 + file = self.get_object(pk)
45 + serializer = FileSerializer(file, data=request.data)
46 + if serializer.is_valid():
47 + serializer.save()
48 + return Response(serializer.data)
49 + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
50 +
51 + def delete(self, request, pk, format=None):
52 + file = self.get_object(pk)
53 + file.delete()
54 + return Response(status=status.HTTP_204_NO_CONTENT)
55 +
......
1 -"""
2 -urls
3 -"""
4 -
5 -from django.conf.urls import url, include
6 -from rest_framework import routers
7 -from tutorial.quickstart import views
8 -
9 -router = routers.DefaultRouter()
10 -router.register(r'users', views.UserViewSet)
11 -router.register(r'groups', views.GroupViewSet)
12 -
13 -# Wire up our API using automatic URL routing.
14 -# Additionally, we include login URLs for the browsable API.
15 -urlpatterns = [
16 - url(r'^', include(router.urls)),
17 - url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
18 -]