def test_like_create(self): """ create like """ form_data = {} form = LikeForm(data=form_data) form.comment = self.comment form.user = self.user self.assertEqual(form.is_valid(), True)
def test_like_create_invalid(self): """ create like twice """ CommentLike.objects.create(user=self.user, comment=self.comment) form_data = {} form = LikeForm(data=form_data) form.comment = self.comment form.user = self.user self.assertEqual(form.is_valid(), False)
def like_create(request, comment_id): comment = get_object_or_404(Comment.objects.exclude(user=request.user), pk=comment_id) if request.method == 'POST': form = LikeForm(user=request.user, comment=comment, data=request.POST) if form.is_valid(): like = form.save() comment_like_post_create.send(sender=like.__class__, comment=like.comment) if request.is_ajax(): return json_response({'url_delete': like.get_delete_url(), }) return redirect(request.POST.get('next', comment.get_absolute_url())) else: form = LikeForm() return render(request, 'spirit/comment_like/like_create.html', {'form': form, 'comment': comment})
def like_create(request, comment_id): comment = get_object_or_404(Comment.objects.exclude(user=request.user), pk=comment_id) if request.method == 'POST': form = LikeForm(user=request.user, comment=comment, data=request.POST) if form.is_valid(): like = form.save() comment_like_post_create.send(sender=like.__class__, comment=like.comment) if request.is_ajax(): return json_response({ 'url_delete': like.get_delete_url(), }) return redirect( request.POST.get('next', comment.get_absolute_url())) else: form = LikeForm() return render(request, 'spirit/comment_like/like_create.html', { 'form': form, 'comment': comment })
def render_like_form(comment, like, next=None): form = LikeForm() return {'form': form, 'comment_id': comment.pk, 'like': like, 'next': next}