Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

35 12th hw #36

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
Binary file not shown.
Empty file.
16 changes: 16 additions & 0 deletions HW12/apiproject/apiproject/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for apiproject project.

It exposes the ASGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'apiproject.settings')

application = get_asgi_application()
126 changes: 126 additions & 0 deletions HW12/apiproject/apiproject/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
"""
Django settings for apiproject project.

Generated by 'django-admin startproject' using Django 4.0.6.

For more information on this file, see
https://docs.djangoproject.com/en/4.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.0/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-vtlx_=#j2b_jdo*7kr3koq87gy@uecjis2l=%iuf7bzs^znfk5'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blogapp',
'rest_framework',
'commentapp',
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'apiproject.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'apiproject.wsgi.application'


# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}


# Password validation
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Seoul'

USE_I18N = True

USE_TZ = False


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/

STATIC_URL = 'static/'

# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
23 changes: 23 additions & 0 deletions HW12/apiproject/apiproject/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""apiproject URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.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.urls import path, include

urlpatterns = [
path('admin/', admin.site.urls),
path('blog/', include('blogapp.urls')),
path('comment/', include('commentapp.urls')),
]
16 changes: 16 additions & 0 deletions HW12/apiproject/apiproject/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
WSGI config for apiproject project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/4.0/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'apiproject.settings')

application = get_wsgi_application()
Empty file.
5 changes: 5 additions & 0 deletions HW12/apiproject/blogapp/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.contrib import admin
from blogapp.models import Blog

# Register your models here.
admin.site.register(Blog)
6 changes: 6 additions & 0 deletions HW12/apiproject/blogapp/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class BlogappConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'blogapp'
23 changes: 23 additions & 0 deletions HW12/apiproject/blogapp/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.0.6 on 2022-07-25 14:32

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Blog',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=50)),
('date', models.DateTimeField(auto_now_add=True)),
('body', models.TextField()),
],
),
]
Empty file.
13 changes: 13 additions & 0 deletions HW12/apiproject/blogapp/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.db import models

# Create your models here.
class Blog(models.Model):
title = models.CharField(max_length=50)
date = models.DateTimeField(auto_now_add=True)
body = models.TextField()

def __str__(self):
return self.title

def summary(self):
return self.body[:30]
12 changes: 12 additions & 0 deletions HW12/apiproject/blogapp/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from rest_framework import serializers
from blogapp.models import Blog

class BlogSerializer(serializers.ModelSerializer): # ๊ธ€ ์ž‘์„ฑ
class Meta:
model = Blog
fields = ('id', 'title', 'date', 'body')

class BlogListSerializer(serializers.ModelSerializer): # ๊ธ€ ๋ชฉ๋ก ์กฐํšŒ
class Meta:
model = Blog
fields = ('id', 'title', 'date', 'summary')
3 changes: 3 additions & 0 deletions HW12/apiproject/blogapp/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
7 changes: 7 additions & 0 deletions HW12/apiproject/blogapp/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.urls import path
from blogapp.views import BlogList, BlogDetail

urlpatterns = [
path('main/', BlogList.as_view()),
path('main/<int:pk>', BlogDetail.as_view()),
]
48 changes: 48 additions & 0 deletions HW12/apiproject/blogapp/views.py
Original file line number Diff line number Diff line change
@@ -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)
Empty file.
5 changes: 5 additions & 0 deletions HW12/apiproject/commentapp/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.contrib import admin
from commentapp.models import Comment

# Register your models here.
admin.site.register(Comment)
6 changes: 6 additions & 0 deletions HW12/apiproject/commentapp/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class CommentappConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'commentapp'
26 changes: 26 additions & 0 deletions HW12/apiproject/commentapp/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -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')),
],
),
]
Empty file.
13 changes: 13 additions & 0 deletions HW12/apiproject/commentapp/models.py
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions HW12/apiproject/commentapp/serializers.py
Original file line number Diff line number Diff line change
@@ -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')
3 changes: 3 additions & 0 deletions HW12/apiproject/commentapp/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
8 changes: 8 additions & 0 deletions HW12/apiproject/commentapp/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.urls import path
from commentapp.views import CommentList, CommentDetail, BlogCommentList

urlpatterns = [
path('main/', CommentList.as_view()),
path('blog/<int:pk>', BlogCommentList.as_view()),
path('main/<int:pk>', CommentDetail.as_view()),
]
Loading