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

7th Assignment Submission #74

Open
wants to merge 1 commit into
base: uzin
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
Empty file added comment/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions comment/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions comment/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class CommentConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'comment'
29 changes: 29 additions & 0 deletions comment/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 4.1.7 on 2023-05-05 12:16

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone


class Migration(migrations.Migration):

initial = True

dependencies = [
('post', '0004_post_tags'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='Comment',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('content', models.TextField()),
('created_at', models.DateTimeField(default=django.utils.timezone.now)),
('author', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
('post', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='post.post')),
],
),
]
Empty file added comment/migrations/__init__.py
Empty file.
11 changes: 11 additions & 0 deletions comment/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from post.models import Post

# Create your models here.
class Comment(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE)
content = models.TextField()
author = models.ForeignKey(User, null=True, on_delete=models.CASCADE)
created_at = models.DateTimeField(default=timezone.now)
8 changes: 8 additions & 0 deletions comment/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from rest_framework.serializers import ModelSerializer
from .models import Comment


class CommentSerializer(ModelSerializer):
class Meta:
model = Comment
fields = "__all__"
3 changes: 3 additions & 0 deletions comment/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.
9 changes: 9 additions & 0 deletions comment/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.urls import path
from .views import CommentListView
from .views import CommentDetailView

app_name = 'comment'
urlpatterns = [
path("", CommentListView.as_view()),
path("<int:comment_id>/", CommentDetailView.as_view()),
]
97 changes: 97 additions & 0 deletions comment/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
from django.shortcuts import render
from rest_framework import status
from rest_framework.response import Response
from rest_framework.decorators import api_view
from rest_framework.views import APIView

from post.models import Post
from .models import Comment
from .serializers import CommentSerializer


# Create your views here.
class CommentListView(APIView):
# get, post
def get(self, request):

if not 'post' in request.GET:
return Response({"detail": "missing fields ['post']"}, status=status.HTTP_400_BAD_REQUEST)

post_id = request.GET.get('post')

try:
post = Post.objects.get(id=post_id)
except:
return Response({"detail": "Not found."}, status=status.HTTP_404_NOT_FOUND)

comments = Comment.objects.filter(post=post_id)
serializer = CommentSerializer(comments, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)

def post(self, request):
post_id = request.data.get('post')
content = request.data.get('content')
author = request.user

if not author.is_authenticated:
return Response({"detail": "Authentication credentials not provided"}, status=status.HTTP_401_UNAUTHORIZED)

if not post_id or not content:
return Response({"detail": "missing fields ['post', 'content']"}, status=status.HTTP_400_BAD_REQUEST)

try:
post = Post.objects.get(id=post_id)
except:
return Response({"detail": "Not found."}, status=status.HTTP_404_NOT_FOUND)

comment = Comment.objects.create(post=post, content=content, author=author)
serializer = CommentSerializer(comment)
return Response(serializer.data, status=status.HTTP_201_CREATED)


#delete, put
class CommentDetailView(APIView):

def delete(self, request, comment_id):

author = request.user

if not author.is_authenticated:
return Response({"detail": "Authentication credentials not provided"}, status=status.HTTP_401_UNAUTHORIZED)

try:
comment = Comment.objects.get(id=comment_id)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

굳굳 comment_id를 이미 path parameter에서 받아왔기 때문에(55line) comment_id를 다시 불러올 필요 없이 쓰면 됩니다!

except:
return Response({"detail": "Not found."}, status=status.HTTP_404_NOT_FOUND)

if request.user != comment.author:
return Response({"detail": "Permission denied"}, status=status.HTTP_401_UNAUTHORIZED)

comment.delete()
return Response(status=status.HTTP_204_NO_CONTENT)


def put(self, request, comment_id):

content = request.data.get('content')
author = request.user

try:
comment = Comment.objects.get(id=comment_id)
except:
return Response({"detail": "Not found."}, status=status.HTTP_404_NOT_FOUND)

if not author.is_authenticated:
return Response({"detail": "Authentication credentials not provided"}, status=status.HTTP_401_UNAUTHORIZED)

if request.user != comment.author:
return Response({"detail": "Permission denied"}, status=status.HTTP_401_UNAUTHORIZED)

if not content:
return Response({"detail": "missing fields ['content']"}, status=status.HTTP_400_BAD_REQUEST)

serializer = CommentSerializer(comment, data=request.data, partial=True)
if not serializer.is_valid():

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

유효성검사 👍

return Response({"detail": "data validation error"}, status=status.HTTP_400_BAD_REQUEST)
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
1 change: 1 addition & 0 deletions seminar/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
'post',
'account',
'tag',
'comment',
'rest_framework_simplejwt',
"rest_framework_simplejwt.token_blacklist",
]
Expand Down
3 changes: 2 additions & 1 deletion seminar/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
path('admin/', admin.site.urls),
path('api/post/', include('post.urls')),
path('api/tag/', include('tag.urls')),
path('api/account/', include('account.urls'))
path('api/account/', include('account.urls')),
path('api/comment/', include('comment.urls'))
]