예제 #1
0
파일: writers.py 프로젝트: JvGinkel/openode
def edit_comment(request):

    if request.user.is_anonymous():
        raise exceptions.PermissionDenied(_('Sorry, anonymous users cannot edit comments'))

    comment_id = int(request.POST['comment_id'])
    try:
        comment_post = models.Post.objects.get(
            post_type='comment',
            pk=comment_id
        )
    except models.Post.DoesNotExist:
        return HttpResponseNotFound(mimetype="application/json")

    thread = comment_post.thread
    if thread and not request.user.has_openode_perm("question_answer_comment_update_any", thread):
        return HttpResponseForbidden(mimetype="application/json")

    request.user.edit_comment(comment_post=comment_post, body_text=request.POST['comment'])

    is_deletable = template_filters.can_delete_comment(comment_post.author, comment_post)
    is_editable = template_filters.can_edit_comment(comment_post.author, comment_post)
    tz = ' ' + template_filters.TIMEZONE_STR

    tz = template_filters.TIMEZONE_STR

    # show last visit for posts (comments, ...)
    try:
        thread_view_last_visit = thread.viewed.get(user=request.user).last_visit
    except (exceptions.ObjectDoesNotExist, TypeError):
        thread_view_last_visit = datetime.datetime.now()

    thread.visit(request.user)

    return {
        'id': comment_post.id,
        'object_id': comment_post.parent.id,
        'comment_added_at': str(comment_post.added_at.replace(microsecond=0)) + tz,
        "html": mark_safe(render_to_string("node/snippets/one_comment.html", {"comment": comment_post, "user": request.user, "thread_view_last_visit": thread_view_last_visit, "wrap_content": False})),
        'user_display_name': comment_post.author.username,
        'user_url': comment_post.author.get_profile_url(),
        'user_id': comment_post.author.id,
        'is_deletable': is_deletable,
        'is_editable': is_editable,
        'score': comment_post.points,  # to support unchanged js
        'points': comment_post.points,
        'voted': comment_post.is_upvoted_by(request.user),
    }
예제 #2
0
파일: writers.py 프로젝트: JvGinkel/openode
def __generate_comments_json(obj, user, new_comment=None):  # non-view generates json data for the post comments
    """non-view generates json data for the post comments
    """
    models.Post.objects.precache_comments(for_posts=[obj], visitor=user)
    comments = obj._cached_comments

    # {"Id":6,"PostId":38589,"CreationDate":"an hour ago","Text":"hello there!","UserDisplayName":"Jarrod Dixon","UserUrl":"/users/3/jarrod-dixon","DeleteUrl":null}
    json_comments = []

    for comment in comments:

        if user and user.is_authenticated():
            try:
                user.assert_can_delete_comment(comment)
                #/posts/392845/comments/219852/delete
                #todo translate this url
                is_deletable = True
            except exceptions.PermissionDenied:
                is_deletable = False
            is_editable = template_filters.can_edit_comment(comment.author, comment)
        else:
            is_deletable = False
            is_editable = False

        comment_owner = comment.author
        tz = ' ' + template_filters.TIMEZONE_STR
        comment_data = {'id': comment.id,
            'object_id': obj.id,
            'comment_added_at': str(comment.added_at.replace(microsecond=0)) + tz,
            'html': mark_safe(comment.text),
            'user_display_name': escape(comment_owner.username),
            'user_url': comment_owner.get_profile_url(),
            'user_id': comment_owner.id,
            'is_deletable': is_deletable,
            'is_editable': is_editable,
            'points': comment.points,
            'score': comment.points,  # to support js
            'upvoted_by_user': getattr(comment, 'upvoted_by_user', False)
        }
        json_comments.append(comment_data)

    if new_comment:

        # show last visit for posts (comments, ...)
        try:
            thread_view_last_visit = new_comment.thread.viewed.get(user=user).last_visit
        except (exceptions.ObjectDoesNotExist, TypeError):
            thread_view_last_visit = datetime.datetime.now()

        data = simplejson.dumps({
            "parent_post_type": new_comment.parent.post_type,
            "all_comments": json_comments,
            "html": mark_safe(render_to_string("node/snippets/one_comment.html", {
                "comment": new_comment,
                "user": user,
                "thread_view_last_visit": thread_view_last_visit,
                "wrap_content": True,
            }))
        })
        new_comment.thread.visit(user)

        # add new comment is "activity" a must be reflected
        new_comment.thread.set_last_activity(
            last_activity_at=datetime.datetime.now(),
            last_activity_by=user
        )
    else:
        data = simplejson.dumps(json_comments)

    return HttpResponse(data, mimetype="application/json")