Skip to content
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
22 changes: 22 additions & 0 deletions Todo/mytodo/manage.py
Original file line number Diff line number Diff line change
@@ -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', 'mytodo.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()
Empty file added Todo/mytodo/mytodo/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions Todo/mytodo/mytodo/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for mytodo 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/5.1/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

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

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

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

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

For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.1/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/5.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-d8t7xtj38q&ab_mfqypt=+vjapl$+klezt*7n=supwb09nq6wl'

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

ALLOWED_HOSTS = ['127.0.0.1']


# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

'rest_framework',
'todo',
]

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 = 'mytodo.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 = 'mytodo.wsgi.application'


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

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


# Password validation
# https://docs.djangoproject.com/en/5.1/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/5.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Seoul'

USE_I18N = True

USE_TZ = True


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

STATIC_URL = 'static/'

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

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
7 changes: 7 additions & 0 deletions Todo/mytodo/mytodo/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
path('admin/', admin.site.urls),
path('', include('todo.urls')),
]
16 changes: 16 additions & 0 deletions Todo/mytodo/mytodo/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
WSGI config for mytodo 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/5.1/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

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

application = get_wsgi_application()
Empty file added Todo/mytodo/todo/__init__.py
Empty file.
4 changes: 4 additions & 0 deletions Todo/mytodo/todo/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from django.contrib import admin
from .models import Todo

admin.site.register(Todo)
6 changes: 6 additions & 0 deletions Todo/mytodo/todo/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class TodoConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'todo'
25 changes: 25 additions & 0 deletions Todo/mytodo/todo/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 5.1.1 on 2024-09-18 07:37

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Todo',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=100)),
('description', models.TextField(blank=True)),
('created', models.DateTimeField(auto_now_add=True)),
('complete', models.BooleanField(default=False)),
('important', models.BooleanField(default=False)),
],
),
]
Empty file.
11 changes: 11 additions & 0 deletions Todo/mytodo/todo/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.db import models

class Todo(models.Model):
title = models.CharField(max_length=100)
description = models.TextField(blank=True)
created = models.DateTimeField(auto_now_add=True)
complete = models.BooleanField(default=False)
important = models.BooleanField(default=False)

def __str__(self):
return self.title
20 changes: 20 additions & 0 deletions Todo/mytodo/todo/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from rest_framework import serializers
from .models import Todo

# 전체 조회
class TodoSimpleSerializer(serializers.ModelSerializer):
class Meta:
model = Todo
fields = ('id', 'title', 'complete', 'important')

# 상세 조회
class TodoDetailSerializer(serializers.ModelSerializer):
class Meta:
model = Todo
fields = ('id', 'title', 'description', 'created', 'complete', 'important')

# Todo 생성
class TodoCreateSerializer(serializers.ModelSerializer):
class Meta:
model = Todo
fields = ('title', 'description', 'important')
3 changes: 3 additions & 0 deletions Todo/mytodo/todo/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.
10 changes: 10 additions & 0 deletions Todo/mytodo/todo/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.urls import path

from .views import *

urlpatterns = [
path('todo/', TodosAPIView.as_view()), # Todo 전체조회 & 생성
path('todo/<int:pk>/', TodoAPIView.as_view()), # Todo 상세조회 & 수정
path('done/', DoneTodosAPIView.as_view()), # 완료된 Todo 전체조회
path('done/<int:pk>/', DoneTodoAPIView.as_view()), # Todo 완료시키기
]
58 changes: 58 additions & 0 deletions Todo/mytodo/todo/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from rest_framework import status
from rest_framework.generics import get_object_or_404
from rest_framework.response import Response
from rest_framework.views import APIView

from rest_framework import viewsets

from .models import Todo
from .serializers import *

# 전체 조회
class TodosAPIView(APIView):
# 조회
def get(self, request):
todos = Todo.objects.filter(complete=False)
serializer = TodoSimpleSerializer(todos, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)

# Todo 생성 함수 - POST 방식
def post(self, request):
serializer = TodoCreateSerializer(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 TodoAPIView(APIView):
# 조회
def get(self, request, pk):
todo = get_object_or_404(Todo, id=pk)
serializer = TodoDetailSerializer(todo)
return Response(serializer.data, status=status.HTTP_200_OK)

# Todo 수정
def put(self, request, pk):
todo = get_object_or_404(Todo, id=pk)
serializer = TodoCreateSerializer(todo, 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)

# 완료된 Todo 전체조회
class DoneTodosAPIView(APIView):
def get(self, request):
dones = Todo.objects.filter(complete=True) # complete가 true인 것만 필터링
serializer = TodoSimpleSerializer(dones, many=True) # 완료된 Todo(dones)를 TodosimpleSerializer에 전달
return Response(serializer.data, status=status.HTTP_200_OK)

# Todo 완료시키기
class DoneTodoAPIView(APIView):
def get(self, request, pk):
done = get_object_or_404(Todo, id=pk)
done.complete = True # Todo 완료시키기(complete = True)
done.save() # 저장!
serializer = TodoDetailSerializer(done)
return Response(status=status.HTTP_200_OK)
Loading