def delete_comment(request, object_id='', comment_id='', commented_object_type=None):#ajax handler to delete comment response = None commented_object = None if commented_object_type == 'question': commented_object = Question elif commented_object_type == 'answer': commented_object = Answer if request.is_ajax(): comment = get_object_or_404(Comment, id=comment_id) if auth.can_delete_comment(request.user, comment): obj = get_object_or_404(commented_object, id=object_id) obj.comments.remove(comment) obj.comment_count = obj.comment_count - 1 obj.save() user = request.user return __generate_comments_json(obj, commented_object_type, user) raise PermissionDenied()
def __generate_comments_json(obj, type, user):#non-view generates json data for the post comments comments = obj.comments.all().order_by('id') # {"Id":6,"PostId":38589,"CreationDate":"an hour ago","Text":"hello there!","UserDisplayName":"Jarrod Dixon","UserUrl":"/users/3/jarrod-dixon","DeleteUrl":null} json_comments = [] from forum.templatetags.extra_tags import diff_date for comment in comments: comment_user = comment.user delete_url = "" if user != None and auth.can_delete_comment(user, comment): #/posts/392845/comments/219852/delete #todo translate this url delete_url = reverse('index') + type + "s/%s/comments/%s/delete/" % (obj.id, comment.id) json_comments.append({"id" : comment.id, "object_id" : obj.id, "comment_age" : diff_date(comment.added_at), "text" : comment.comment, "user_display_name" : comment_user.username, "user_url" : comment_user.get_profile_url(), "delete_url" : delete_url }) data = simplejson.dumps(json_comments) return HttpResponse(data, mimetype="application/json")
def can_delete_comment(user, comment): return auth.can_delete_comment(user, comment)