def new_comment(request, board_id): if request.method == 'GET': context = {'board_id': board_id} return render(request, 'boards/new_comment.html', context) if request.method == 'POST': board = get_object_or_404(Board, id=board_id) content = request.POST.get('content') comment = Comment(board_id=board.id, content=content) comment.save() return redirect('boards:detail', board.id)
def post_comment(request): comment = request.POST['comment'] list_id = int(request.POST['list_entry_id']) list_entry = get_object_or_404(ListEntry, id=list_id) get_object_or_404(list_entry.parent_list.board.members.all(), id=request.user.id) comment = Comment(user=request.user, list_entry=list_entry, text=comment) comment.save() return redirect("board", list_entry.parent_list.board.id)
def comments_create(request, board_pk): # 댓글을 달 게시물 board = Board.objects.get(pk=board_pk) if request.method == 'POST': # form 에서 넘어온 댓글 정보 content = request.POST.get('content') # 댓글 생성 및 저장 comment = Comment(board=board, content=content) # comment = Comment(board_id=board.pk, content=content) comment.save() return redirect('boards:detail', board.pk) # return redirect('boards:detail', comment.board_id) else: return redirect('boards:detail', board.pk)
def setUp(self): user = User.objects.create_user('test', '*****@*****.**', 'test1234@') self.user = user profile = Profile(user=user) profile.save() category = Category(title="카테고리", creator=self.user) category.save() self.category = category self.assertIsInstance(category, Category) post = Post(title="포스트", content="내용", category=self.category, creator=self.user) post.save() self.post = post self.assertIsInstance(post, Post) comment = Comment(content="댓글", creator=self.user, post=self.post) comment.save() self.comment = comment self.assertIsInstance(comment, Comment) reply = Reply(content="대댓글", creator=self.user, comment=self.comment) reply.save() self.reply = reply self.assertIsInstance(reply, Reply) user = {"username": "******", "password": "******"} response = self.client.post('/api/v1/auth/login/', json.dumps(user), content_type='application/json') self.assertEqual(response.status_code, 200) self.token = response.data['access_token'] self.csrftoken = response.cookies.get('csrftoken').value self.assertNotEqual(self.token, '') self.headers = { "HTTP_Authorization": "jwt " + self.token, "X-CSRFToken": self.csrftoken, }
def form_valid(self, form): """ Append default data while creating a comment, not provided in a form. """ obj = self.get_object() comment = Comment(author=current_user.id, object_id=obj.id, object_type=obj.object_type, text=form.text.data) db.session.add(comment) db.session.commit() return super(CommentMixin, self).form_valid(form)
def setUp(self): self.client = APIClient() self.user = setup_user() self.user.save() self.board = Board(title='Test Board', created_by=self.user) self.board.save() self.label = Label(board=self.board, title='Red Label', color='#FF0000') self.label.save() self.label2 = Label(board=self.board, title='Green Label', color='#00FF00') self.label2.save() self.card = Card(board=self.board, title='Test Card', description='Test Card description', created_by=self.user) self.card.save() self.comment = Comment(card=self.card, message='Test message.', created_by=self.user) self.comment.save()
def post(self, request, *args, **kwargs): """ post_id 글에 새로운 댓글을 작성한다. content 필수 """ post_id = kwargs['post_id'] post = get_object_or_404(Post, pk=post_id) content = request.data.get('content', '') if content == '': return Response({"message": "content required"}, status=status.HTTP_400_BAD_REQUEST) comment = Comment( creator=request.user, post=post, content=content, ) comment.save() return Response({"message": "Comment created success"}, status=status.HTTP_201_CREATED)
class CommentAPIViewSet(TestCase): """Test suite for the Comment API views.""" def setUp(self): self.client = APIClient() self.user = setup_user() self.user.save() self.board = Board(title='Test Board', created_by=self.user) self.board.save() self.label = Label(board=self.board, title='Red Label', color='#FF0000') self.label.save() self.label2 = Label(board=self.board, title='Green Label', color='#00FF00') self.label2.save() self.card = Card(board=self.board, title='Test Card', description='Test Card description', created_by=self.user) self.card.save() self.comment = Comment(card=self.card, message='Test message.', created_by=self.user) self.comment.save() # self.client.force_authenticate(self.user) def test_api_can_create_a_new_comment(self): response = self.client.post( '/api/v1/boards/{board_pk}/cards/{card_pk}/comments/'.format( board_pk=self.board.pk, card_pk=self.card.pk), { "message": "Test Message 2.", "created_by": self.user.pk, }, format='json' # Explicit format: otherwise list is sent as str() ) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response.data['id'], self.comment.pk + 1) def test_api_can_update_a_comment(self): response = self.client.put( '/api/v1/boards/{board_pk}/cards/{card_pk}/comments/{comment_pk}/'. format(board_pk=self.board.pk, card_pk=self.card.pk, comment_pk=self.comment.pk), { "message": "Test Message 2 edited.", "updated_at": "2017-12-17 06:26:53", "updated_by": self.user.pk }) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['message'], 'Test Message 2 edited.') self.assertEqual(response.data['updated_at'], "2017-12-17T06:26:53Z") self.assertEqual(response.data['updated_by'], self.user.pk) def test_api_can_delete_a_comment(self): old_count = Comment.objects.count() response = self.client.delete( '/api/v1/boards/{board_pk}/cards/{card_pk}/comments/{comment_pk}/'. format(board_pk=self.board.pk, card_pk=self.card.pk, comment_pk=self.comment.pk), ) new_count = Comment.objects.count() self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertNotEqual(old_count, new_count)
def comment_create(request, board_id): content = request.POST.get('content') comment = Comment(board_id=board_id, content=content) comment.save() return redirect('boards:detail', board_id)