From e8e971b3dec5cc1090e46314de7ea5d1699df380 Mon Sep 17 00:00:00 2001 From: summit45 Date: Mon, 25 Jul 2022 14:58:57 +0900 Subject: [PATCH 1/3] =?UTF-8?q?[feat]=20=EB=8C=93=EA=B8=80=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1,=EC=A1=B0=ED=9A=8C,=EC=82=AD=EC=A0=9C,=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .DS_Store | Bin 10244 -> 10244 bytes .../week11/myblog/.DS_Store" | Bin 0 -> 6148 bytes HW12/apiproject/apiproject/__init__.py | 0 HW12/apiproject/apiproject/asgi.py | 16 +++ HW12/apiproject/apiproject/settings.py | 126 ++++++++++++++++++ HW12/apiproject/apiproject/urls.py | 23 ++++ HW12/apiproject/apiproject/wsgi.py | 16 +++ HW12/apiproject/blogapp/__init__.py | 0 HW12/apiproject/blogapp/admin.py | 5 + HW12/apiproject/blogapp/apps.py | 6 + .../blogapp/migrations/0001_initial.py | 23 ++++ .../apiproject/blogapp/migrations/__init__.py | 0 HW12/apiproject/blogapp/models.py | 13 ++ HW12/apiproject/blogapp/serializers.py | 12 ++ HW12/apiproject/blogapp/tests.py | 3 + HW12/apiproject/blogapp/urls.py | 7 + HW12/apiproject/blogapp/views.py | 48 +++++++ HW12/apiproject/commentapp/__init__.py | 0 HW12/apiproject/commentapp/admin.py | 5 + HW12/apiproject/commentapp/apps.py | 6 + .../commentapp/migrations/0001_initial.py | 26 ++++ .../commentapp/migrations/__init__.py | 0 HW12/apiproject/commentapp/models.py | 13 ++ HW12/apiproject/commentapp/serializers.py | 12 ++ HW12/apiproject/commentapp/tests.py | 3 + HW12/apiproject/commentapp/urls.py | 8 ++ HW12/apiproject/commentapp/views.py | 58 ++++++++ HW12/apiproject/manage.py | 22 +++ 28 files changed, 451 insertions(+) create mode 100644 "11\354\243\274\354\260\250_Django(4)/week11/myblog/.DS_Store" create mode 100644 HW12/apiproject/apiproject/__init__.py create mode 100644 HW12/apiproject/apiproject/asgi.py create mode 100644 HW12/apiproject/apiproject/settings.py create mode 100644 HW12/apiproject/apiproject/urls.py create mode 100644 HW12/apiproject/apiproject/wsgi.py create mode 100644 HW12/apiproject/blogapp/__init__.py create mode 100644 HW12/apiproject/blogapp/admin.py create mode 100644 HW12/apiproject/blogapp/apps.py create mode 100644 HW12/apiproject/blogapp/migrations/0001_initial.py create mode 100644 HW12/apiproject/blogapp/migrations/__init__.py create mode 100644 HW12/apiproject/blogapp/models.py create mode 100644 HW12/apiproject/blogapp/serializers.py create mode 100644 HW12/apiproject/blogapp/tests.py create mode 100644 HW12/apiproject/blogapp/urls.py create mode 100644 HW12/apiproject/blogapp/views.py create mode 100644 HW12/apiproject/commentapp/__init__.py create mode 100644 HW12/apiproject/commentapp/admin.py create mode 100644 HW12/apiproject/commentapp/apps.py create mode 100644 HW12/apiproject/commentapp/migrations/0001_initial.py create mode 100644 HW12/apiproject/commentapp/migrations/__init__.py create mode 100644 HW12/apiproject/commentapp/models.py create mode 100644 HW12/apiproject/commentapp/serializers.py create mode 100644 HW12/apiproject/commentapp/tests.py create mode 100644 HW12/apiproject/commentapp/urls.py create mode 100644 HW12/apiproject/commentapp/views.py create mode 100755 HW12/apiproject/manage.py diff --git a/.DS_Store b/.DS_Store index 272c8806574bfad1a7cfcfd8e884652d10c0948e..b29301bb61d34574e63e656baa4dced8f09b2f59 100644 GIT binary patch delta 320 zcmZn(XbG6$&uFqSU^hRb$z%rsmCXhMhZyS_j0AZE^91<>6B*(ef*4X6iWy265*Z2@ zGIP=mgOl@f3m6c9kHHYP`rLdMm!zEhB%pGRV0#yv2vYez9ymDayk5`6fbUFkeO!Y^v>h9 zJ&QXX05UneJ^^C@4OJ1}YHHEAY9%+}84<fP!-$2-qDT@8XEzK`WtV;dz&SQMIFR8uy>?~B3?@LQi%~myqx|L zd2L|t=;e?YJ|vzjF`-E3?u_r#ej^a zv+0DN6uq_jIPJBCI;Wn5xt302t(a)7m', BlogDetail.as_view()), +] diff --git a/HW12/apiproject/blogapp/views.py b/HW12/apiproject/blogapp/views.py new file mode 100644 index 00000000..e4ed551c --- /dev/null +++ b/HW12/apiproject/blogapp/views.py @@ -0,0 +1,48 @@ +from rest_framework import status +from rest_framework.views import APIView +from rest_framework.response import Response + +from .serializers import BlogSerializer, BlogListSerializer +from .models import Blog + +from django.http import Http404 + +# Create your views here. +class BlogList(APIView): + def get(self, request): # 글 목록 조회하는 경우 + blogs = Blog.objects.all() + + serializer = BlogListSerializer(blogs, many=True) # 다수의 쿼리셋 전달 위해서 many = True + return Response(serializer.data) + + def post(self, request): # 글 작성하는 경우 + serializer = BlogSerializer(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 BlogDetail(APIView): + def get_object(self, pk): # blog 객체 가져오기 + try: + return Blog.objects.get(pk=pk) + except Blog.DoesNotExist: + raise Http404 + + def get(self, request, pk): # blog 상세조회 + blog = self.get_object(pk) + serializer = BlogSerializer(blog) + return Response(serializer.data) + + def put(self, request, pk): # blog 수정 + blog = self.get_object(pk) + serializer = BlogSerializer(blog, data=request.data) + if serializer.is_valid(): + serializer.save() + return Response(serializer.data, status=status.HTTP_200_OK) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + + def delete(self, request, pk): + blog = self.get_object(pk) + blog.delete() + return Response(status=status.HTTP_204_NO_CONTENT) diff --git a/HW12/apiproject/commentapp/__init__.py b/HW12/apiproject/commentapp/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/HW12/apiproject/commentapp/admin.py b/HW12/apiproject/commentapp/admin.py new file mode 100644 index 00000000..b992b77e --- /dev/null +++ b/HW12/apiproject/commentapp/admin.py @@ -0,0 +1,5 @@ +from django.contrib import admin +from commentapp.models import Comment + +# Register your models here. +admin.site.register(Comment) \ No newline at end of file diff --git a/HW12/apiproject/commentapp/apps.py b/HW12/apiproject/commentapp/apps.py new file mode 100644 index 00000000..f03b6eb4 --- /dev/null +++ b/HW12/apiproject/commentapp/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class CommentappConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'commentapp' diff --git a/HW12/apiproject/commentapp/migrations/0001_initial.py b/HW12/apiproject/commentapp/migrations/0001_initial.py new file mode 100644 index 00000000..1c346a71 --- /dev/null +++ b/HW12/apiproject/commentapp/migrations/0001_initial.py @@ -0,0 +1,26 @@ +# Generated by Django 4.0.6 on 2022-07-25 14:32 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('blogapp', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='Comment', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('content', models.CharField(max_length=200)), + ('created_at', models.DateTimeField(auto_now_add=True)), + ('updated_at', models.DateTimeField(auto_now=True)), + ('blog', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='blogapp.blog')), + ], + ), + ] diff --git a/HW12/apiproject/commentapp/migrations/__init__.py b/HW12/apiproject/commentapp/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/HW12/apiproject/commentapp/models.py b/HW12/apiproject/commentapp/models.py new file mode 100644 index 00000000..d5d0442f --- /dev/null +++ b/HW12/apiproject/commentapp/models.py @@ -0,0 +1,13 @@ +from django.db import models + +from blogapp.models import Blog + +# Create your models here. +class Comment(models.Model): + blog = models.ForeignKey(Blog, on_delete=models.CASCADE) + content = models.CharField(max_length=200) + created_at = models.DateTimeField(auto_now_add=True) + updated_at = models.DateTimeField(auto_now = True) + + def __str__(self): + return self.content diff --git a/HW12/apiproject/commentapp/serializers.py b/HW12/apiproject/commentapp/serializers.py new file mode 100644 index 00000000..f45faa1d --- /dev/null +++ b/HW12/apiproject/commentapp/serializers.py @@ -0,0 +1,12 @@ +from rest_framework import serializers +from commentapp.models import Comment + +class CommentSerializer(serializers.ModelSerializer): # 댓글 작성 + class Meta: + model = Comment + fields = ('id', 'blog', 'content') + +class CommentListSerializer(serializers.ModelSerializer): # 댓글 조회 + class Meta: + model = Comment + fields = ('id', 'blog', 'content', 'created_at') \ No newline at end of file diff --git a/HW12/apiproject/commentapp/tests.py b/HW12/apiproject/commentapp/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/HW12/apiproject/commentapp/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/HW12/apiproject/commentapp/urls.py b/HW12/apiproject/commentapp/urls.py new file mode 100644 index 00000000..f9db56ce --- /dev/null +++ b/HW12/apiproject/commentapp/urls.py @@ -0,0 +1,8 @@ +from django.urls import path +from commentapp.views import CommentList, CommentDetail, BlogCommentList + +urlpatterns = [ + path('main/', CommentList.as_view()), + path('blog/', BlogCommentList.as_view()), + path('main/', CommentDetail.as_view()), +] diff --git a/HW12/apiproject/commentapp/views.py b/HW12/apiproject/commentapp/views.py new file mode 100644 index 00000000..50c36ce4 --- /dev/null +++ b/HW12/apiproject/commentapp/views.py @@ -0,0 +1,58 @@ +from rest_framework import status +from rest_framework.views import APIView +from rest_framework.response import Response + +from .serializers import CommentSerializer, CommentListSerializer +from .models import Comment + +from django.http import Http404 + +# Create your views here. +class CommentList(APIView): + def get(self, request): # 모든 댓글 목록 조회하는 경우 + comments = Comment.objects.filter() + + serializer = CommentListSerializer(comments, many=True) # 다수의 쿼리셋 전달 위해서 many = True + return Response(serializer.data) + + def post(self, request): # 댓글 작성하는 경우 + serializer = CommentSerializer(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 BlogCommentList(APIView): + def get(self, request, blog_pk): # 모든 댓글 목록 조회하는 경우 + blog_id = request.data['blog'] + comments = Comment.objects.get( + blog_pk = blog_id + ) + serializer = CommentListSerializer(comments, many=True) # 다수의 쿼리셋 전달 위해서 many = True + return Response(serializer.data) + + +class CommentDetail(APIView): + def get_object(self, pk): # comment 객체 가져오기 + try: + return Comment.objects.get(pk=pk) + except Comment.DoesNotExist: + raise Http404 + + def get(self, request, pk): # comment 상세조회 + comment = self.get_object(pk) + serializer = CommentSerializer(comment) + return Response(serializer.data) + + def put(self, request, pk): # comment 수정 + comment = self.get_object(pk) + serializer = CommentSerializer(comment, data=request.data) + if serializer.is_valid(): + serializer.save() + return Response(serializer.data, status=status.HTTP_200_OK) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + + def delete(self, request, pk): + comment = self.get_object(pk) + comment.delete() + return Response(status=status.HTTP_204_NO_CONTENT) diff --git a/HW12/apiproject/manage.py b/HW12/apiproject/manage.py new file mode 100755 index 00000000..1d1d5144 --- /dev/null +++ b/HW12/apiproject/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'apiproject.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main() From 7f28bdf0e9b518da9acc95c76888c455ae640676 Mon Sep 17 00:00:00 2001 From: summit45 Date: Mon, 25 Jul 2022 15:07:31 +0900 Subject: [PATCH 2/3] =?UTF-8?q?[fix]=20=EB=B8=94=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=EB=8B=B9=20=EB=8C=93=EA=B8=80=20=EC=A1=B0=ED=9A=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HW12/apiproject/commentapp/views.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/HW12/apiproject/commentapp/views.py b/HW12/apiproject/commentapp/views.py index 50c36ce4..0d0f89c3 100644 --- a/HW12/apiproject/commentapp/views.py +++ b/HW12/apiproject/commentapp/views.py @@ -23,10 +23,12 @@ def post(self, request): # 댓글 작성하는 경우 return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) class BlogCommentList(APIView): - def get(self, request, blog_pk): # 모든 댓글 목록 조회하는 경우 + def get(self, request, pk): # 모든 댓글 목록 조회하는 경우 + blog_id = request.data['blog'] - comments = Comment.objects.get( - blog_pk = blog_id + print(blog_id) + comments = Comment.objects.filter( + blog = blog_id ) serializer = CommentListSerializer(comments, many=True) # 다수의 쿼리셋 전달 위해서 many = True return Response(serializer.data) From 022273ebdbe7302da18aab3fef1e8e7b4b52756c Mon Sep 17 00:00:00 2001 From: summit45 Date: Mon, 25 Jul 2022 15:13:01 +0900 Subject: [PATCH 3/3] =?UTF-8?q?[fix]=20=EB=B8=94=EB=A1=9C=EA=B7=B8=20?= =?UTF-8?q?=EB=8C=93=EA=B8=80=20=EC=A1=B0=ED=9A=8C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HW12/apiproject/commentapp/views.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/HW12/apiproject/commentapp/views.py b/HW12/apiproject/commentapp/views.py index 0d0f89c3..cf7a87c3 100644 --- a/HW12/apiproject/commentapp/views.py +++ b/HW12/apiproject/commentapp/views.py @@ -25,10 +25,8 @@ def post(self, request): # 댓글 작성하는 경우 class BlogCommentList(APIView): def get(self, request, pk): # 모든 댓글 목록 조회하는 경우 - blog_id = request.data['blog'] - print(blog_id) comments = Comment.objects.filter( - blog = blog_id + blog = pk ) serializer = CommentListSerializer(comments, many=True) # 다수의 쿼리셋 전달 위해서 many = True return Response(serializer.data)