-
Notifications
You must be signed in to change notification settings - Fork 32
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
Joeyoojin
wants to merge
1
commit into
snulion11th-seminar:uzin
Choose a base branch
from
Joeyoojin:uzin
base: uzin
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
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(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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를 다시 불러올 필요 없이 쓰면 됩니다!