def comment(request, pk=None): """ Create a new comment. Django comments framework sucks! """ if request.GET.get('delete'): if request.user.is_staff: get_object_or_404(SnippetComment, pk=pk).delete() request.session['flash'] = ['Comment deleted succesfully', 'success'] else: request.session['flash'] = ['Permission denied', 'error'] return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/')) else: data = {} snippet = get_object_or_404(Snippet, pk=pk) if request.user.is_authenticated(): body = request.POST.get('body') if body: comment = SnippetComment(snippet=snippet, user=request.user, body=body) comment.save() # send notification if you are not the author # TODO: This code should be catched by an event handler if snippet.author != request.user: profile = UserProfile.objects.get(user=snippet.author) #User wants to recieve a notification if profile.user_commented: queue = EmailQueue( mail_to=snippet.author.email, mail_subject="Your snippet has been commented", mail_body=render_to_string( 'emails/user_commented.txt', { 'user': snippet.author, 'username': request.user.username, 'comment': comment, 'snippet': snippet, 'SITE': request.META.get('HTTP_HOST', 'localhost') } ) ) queue.save() data['content'] = render_to_string('elements/comment.html', {'comment': comment}) else: data['error'] = 'Body field is required' else: data['error'] = 'You must login to post a comment' return JsonResponse(data)
def comment(request, id = None): """ Create a new comment. Django comments framework (v1.2) doesn't have a simple ajax api. Only staff users can delete comments. Also only logged users can post. """ if request.GET.get('delete'): if request.user.is_staff: get_object_or_404(SnippetComment, pk=id).delete() request.session['flash'] = ['Comment deleted succesfully', 'success']; else: request.session['flash'] = ['Permission denied', 'error']; return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/accounts/profile/')) else: data = {} snippet = get_object_or_404(Snippet, pk=id) if request.user.is_authenticated: body = request.POST.get('body') if body: comment = SnippetComment(snippet = snippet, user = request.user, body = body) comment.save() if snippet.author != request.user: # send notification if you are not the author profile = UserProfile.objects.get(user=snippet.author) if profile.user_commented: #User wants to recieve a notification queue = EmailQueue( mail_to=snippet.author.email, mail_subject="Your snippet has been commented", mail_body=render_to_string('emails/user_commented.txt', { 'user': snippet.author, 'username': request.user.username, 'comment': comment, 'snippet': snippet, 'SITE': request.META['HTTP_HOST']} ) ) queue.save() data['content'] = render_to_string('elements/comment.html', {'comment': comment}) else: data['error'] = 'Body field is required' else: data['error'] = 'You must login to post a comment' return JsonResponse(data)