def comment_image_upload_ajax(request): if not request.is_ajax(): return Http404() form = CommentImageForm(user=request.user, data=request.POST, files=request.FILES) if form.is_valid(): image = form.save() return json_response({'url': image.url, }) return json_response({'error': dict(form.errors.items()), })
def test_json_response(self): """ return json_response """ res = spirit_utils.json_response() self.assertIsInstance(res, HttpResponse) self.assertEqual(res.status_code, 200) self.assertEqual(res['Content-Type'], 'application/json') self.assertDictEqual(json.loads(res.content), {}) res = spirit_utils.json_response({"foo": "bar", }) self.assertDictEqual(json.loads(res.content), {"foo": "bar", }) res = spirit_utils.json_response(status=404) self.assertEqual(res.status_code, 404)
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_delete(request, pk): like = get_object_or_404(CommentLike, pk=pk, user=request.user) if request.method == 'POST': like.delete() comment_like_post_delete.send(sender=like.__class__, comment=like.comment) if request.is_ajax(): url = reverse('spirit:like-create', kwargs={ 'comment_id': like.comment.pk, }) return json_response({ 'url_create': url, }) return redirect( request.POST.get('next', like.comment.get_absolute_url())) context = { 'like': like, } return render(request, 'spirit/comment_like/like_delete.html', context)
def comment_image_upload_ajax(request): if not request.is_ajax(): return Http404() form = CommentImageForm(user=request.user, data=request.POST, files=request.FILES) if form.is_valid(): image = form.save() return json_response({ 'url': image.url, }) return json_response({ 'error': dict(form.errors.items()), })
def bookmark_create(request, topic_id): if not request.is_ajax(): return Http404() topic = get_object_or_404(Topic, pk=topic_id) form = BookmarkForm(user=request.user, topic=topic, data=request.POST) if form.is_valid(): form.save() return json_response() return Http404()
def like_delete(request, pk): like = get_object_or_404(CommentLike, pk=pk, user=request.user) if request.method == 'POST': like.delete() comment_like_post_delete.send(sender=like.__class__, comment=like.comment) if request.is_ajax(): return json_response({'url_create': reverse('spirit:like-create', kwargs={'comment_id': like.comment.pk, }), }) return redirect(request.POST.get('next', like.comment.get_absolute_url())) return render(request, 'spirit/comment_like/like_delete.html', {'like': like, })
def bookmark_create(request, topic_id): if not request.is_ajax(): return Http404() topic = get_object_or_404(Topic, pk=topic_id) form = BookmarkForm(data=request.POST) if not form.is_valid(): return Http404() comment_number = form.cleaned_data['comment_number'] # Bookmark is created/updated on topic view. CommentBookmark.objects.filter(user=request.user, topic=topic)\ .update(comment_number=comment_number) return json_response()
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})