def 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_url = form.save() return json_response({'url': image_url, }) return json_response({'error': dict(form.errors.items()), })
def file_upload_ajax(request): if not is_ajax(request): return Http404() form = CommentFileForm( user=request.user, data=request.POST, files=request.FILES) if form.is_valid(): file = form.save() return json_response({'url': file.url}) return json_response({'error': dict(form.errors.items())})
def create(request, comment_id): comment = get_object_or_404(Comment.objects.exclude(user=request.user), pk=comment_id) form = LikeForm(user=request.user, comment=comment, data=post_data(request)) if is_post(request) and form.is_valid(): like = form.save() like.comment.increase_likes_count() if is_ajax(request): return json_response({'url_delete': like.get_delete_url()}) return safe_redirect(request, 'next', comment.get_absolute_url(), method='POST') return render(request=request, template_name='spirit/comment/like/create.html', context={ 'form': form, 'comment': comment })
def create(request, topic_id): if not is_ajax(request): 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() # TODO: return errors (in json format)
def delete(request, pk): like = get_object_or_404(CommentLike, pk=pk, user=request.user) if is_post(request): like.delete() like.comment.decrease_likes_count() if is_ajax(request): url = reverse('spirit:comment: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())) return render(request=request, template_name='spirit/comment/like/delete.html', context={'like': like})