Skip to content

Commit

Permalink
Comment 삭제 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
saintdragon2 committed Oct 1, 2020
1 parent 3af818a commit 03331b1
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 6 deletions.
43 changes: 37 additions & 6 deletions blog/templates/blog/post_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,43 @@ <h5 class="card-header">Leave a Comment:</h5>
<img class="d-flex mr-3 rounded-circle" src="http://placehold.it/50x50" alt="">
<div class="media-body">
{% if user.is_authenticated and comment.author == user %}
<a role="button"
class="btn btn-sm btn-info float-right"
id="comment-{{ comment.pk }}-update-btn"
href="/blog/update_comment/{{ comment.pk }}/">
edit
</a>
<div class="float-right">
<a role="button"
class="btn btn-sm btn-info"
id="comment-{{ comment.pk }}-update-btn"
href="/blog/update_comment/{{ comment.pk }}/">
edit
</a>
<a role="button"
href="#"
id="comment-{{ comment.pk }}-delete-modal-btn"
class="btn btn-sm btn-danger"
data-toggle="modal" data-target="#deleteCommentModal-{{ comment.pk }}">
delete
</a>
</div>

<!-- Modal -->
<div class="modal fade" id="deleteCommentModal-{{ comment.pk }}" tabindex="-1" role="dialog" aria-labelledby="deleteCommentModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="deleteModalLabel">Are You Sure?</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<del>{{ comment | linebreaks }}</del>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<a role="button" class="btn btn-danger" href="/blog/delete_comment/{{ comment.pk }}/">Delete</a>
</div>
</div>
</div>
</div>

{% endif %}
<h5 class="mt-0">
{{ comment.author.username }}
Expand Down
55 changes: 55 additions & 0 deletions blog/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,4 +379,59 @@ def test_comment_update(self):
self.assertIn('오바마의 댓글을 수정합니다.', comment_001_div.text)
self.assertIn('Updated: ', comment_001_div.text)

def test_delete_comment(self):
comment_by_trump = Comment.objects.create(
post=self.post_001,
author=self.user_trump,
content='트럼프의 댓글입니다.'
)

self.assertEqual(Comment.objects.count(), 2)
self.assertEqual(self.post_001.comment_set.count(), 2)

# 로그인 하지 않은 상태
response = self.client.get(self.post_001.get_absolute_url())
self.assertEqual(response.status_code, 200)
soup = BeautifulSoup(response.content, 'html.parser')

comment_area = soup.find('div', id='comment-area')
self.assertFalse(comment_area.find('a', id='comment-1-delete-btn'))
self.assertFalse(comment_area.find('a', id='comment-2-delete-btn'))

# trump로 로그인 한 상태
self.client.login(username='trump', password='somepassword')
response = self.client.get(self.post_001.get_absolute_url())
self.assertEqual(response.status_code, 200)
soup = BeautifulSoup(response.content, 'html.parser')

comment_area = soup.find('div', id='comment-area')
self.assertFalse(comment_area.find('a', id='comment-1-delete-btn'))
comment_002_delete_modal_btn = comment_area.find(
'a', id='comment-2-delete-modal-btn'
)
self.assertIn('delete', comment_002_delete_modal_btn.text)
self.assertEqual(
comment_002_delete_modal_btn.attrs['data-target'],
'#deleteCommentModal-2'
)

delete_comment_modal_002 = soup.find('div', id='deleteCommentModal-2')
self.assertIn('Are You Sure?', delete_comment_modal_002.text)
really_delete_btn_002 = delete_comment_modal_002.find('a')
self.assertIn('Delete', really_delete_btn_002.text)
self.assertEqual(
really_delete_btn_002.attrs['href'],
'/blog/delete_comment/2/'
)

response = self.client.get('/blog/delete_comment/2/', follow=True)
self.assertEqual(response.status_code, 200)
soup = BeautifulSoup(response.content, 'html.parser')
self.assertIn(self.post_001.title, soup.title.text)
comment_area = soup.find('div', id='comment-area')
self.assertNotIn('트럼프의 댓글입니다.', comment_area.text)

self.assertEqual(Comment.objects.count(), 1)
self.assertEqual(self.post_001.comment_set.count(), 1)


1 change: 1 addition & 0 deletions blog/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from . import views

urlpatterns = [
path('delete_comment/<int:pk>/', views.delete_comment),
path('update_comment/<int:pk>/', views.CommentUpdate.as_view()),
path('update_post/<int:pk>/', views.PostUpdate.as_view()),
path('create_post/', views.PostCreate.as_view()),
Expand Down
10 changes: 10 additions & 0 deletions blog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,13 @@ def dispatch(self, request, *args, **kwargs):
return super(CommentUpdate, self).dispatch(request, *args, **kwargs)
else:
raise PermissionDenied


def delete_comment(request, pk):
comment = get_object_or_404(Comment, pk=pk)
post = comment.post
if request.user.is_authenticated and request.user == comment.author:
comment.delete()
return redirect(post.get_absolute_url())
else:
raise PermissionDenied

0 comments on commit 03331b1

Please sign in to comment.