def test_saving_and_retrieving_comment(self): second_post = Post.objects.create( board=self.default_board, title='some post title', content='some post content' ) first_comment = Comment() first_comment.post = self.default_post first_comment.content = 'This is a first comment' first_comment.account = self.user first_comment.ip = '127.0.0.1' first_comment.save() second_comment = Comment() second_comment.post = second_post second_comment.content = 'This is a second comment' second_comment.account = self.user second_comment.ip = '127.0.0.1' second_comment.save() saved_comments = Comment.objects.all() self.assertEqual(saved_comments.count(), 2) saved_comments_on_default_post = Comment.objects.filter(post=self.default_post) saved_comments_on_second_post = Comment.objects.filter(post=second_post) self.assertEqual(saved_comments_on_default_post.count(), 1) self.assertEqual(saved_comments_on_second_post.count(), 1) first_saved_comment = saved_comments[0] second_saved_comment = saved_comments[1] self.assertEqual(first_saved_comment.content, 'This is a first comment') self.assertEqual(second_saved_comment.content, 'This is a second comment')
def write_comment(request): board_id = request.GET.get('board_id', ) input_comment = request.GET.get('comment', ) user = request.user.pk comment = Comment() comment.board_id = Board(board_id) comment.writer = User(user) comment.content = input_comment comment.status = 'y' try: comment.save() result = json.dumps([ {'result': 'seccess'} ]) except Exception as e: print(e) result = json.dumps([ {'result':'fail'} ]) return HttpResponse(result, content_type="application/json:charset=UTF-8")
def create_comment(request, article_id): article = get_object_or_404(Article, id=article_id) comment = Comment() comment.content = request.POST.get('content') comment.article = article comment.save() return redirect('board:article_detail', article.id)
def create_comment(request, article_id): # 존재하지 않는 article number에 대한 제어 article = get_object_or_404(Article, id=article_id) if request.method == 'POST': comment = Comment() comment.content = request.POST.get('content') comment.article_id = article.id comment.save() # 잘못된 (POST가 아닌)요청이 들어오더라도 return redirect('board:article_detail', article_id)
def comment(request, commentid): # 인증 authuser = request.session.get('authuser') if authuser is None: return HttpResponseRedirect('/board') # 댓글 입력 if commentid is None: comment = Comment() comment.content = request.GET['content'] comment.board_id = request.GET['boardid'] comment.user_id = authuser['id'] comment.save() # 댓글 삭제 else: comment = Comment.objects.get(id=commentid) comment.delete() # 댓글 리스트 추출 commentlist = Comment.objects.all().filter(board_id=request.GET['boardid']) id = [] content = [] regdate = [] user = [] for c in commentlist: id.append(c.id) content.append(c.content) regdate.append(c.regdate) user.append(c.user.name) # json 형태인 dict 자료형으로 리턴 result = { 'result': 'success', 'id': id, 'content': content, 'regdate': regdate, 'user': user, 'nowuser': request.session['authuser']['name'] } return JsonResponse(result) # get, post action 동시 처리 # def test(request, id): # if id is None: # print("get") # else: # print("post") # pass
def setUpTestData(cls): # create user user = User.objects.create(name=name, email=email, password=password) # get token when login request = RequestFactory().post('/login/', data=json.dumps({ 'email': email, 'password': password }), content_type='application/json') global token token = json.loads(login(request=request).content)['token'] # create a post post = Post() post.author = user post.content = "測試用的發文" post.save() # get post_id for post_item in Post.objects.filter(content="測試用的發文"): global post_id post_id = post_item.id # create a comment comment = Comment() comment.post = post comment.author = user comment.content = "對測試發文留言哦" comment.save() # get comment_id for comment_item in Comment.objects.filter(content='對測試發文留言哦'): global comment_id comment_id = comment_item.id