Beispiel #1
0
def post_comment(request, context, comment_id=None):
    clist = CommentList.for_object(context['object'])

    if clist.locked():
        return TemplateResponse(request,
                                get_template('comments_locked.html',
                                             context['object'],
                                             request.is_ajax()),
                                context,
                                status=403)

    comment = None
    user = request.user
    if comment_id:
        try:
            comment = clist.get_comment(comment_id)
        except FlatComment.DoesNotExist:
            raise Http404()

        # make sure we don't change user when mod is editting a comment
        user = comment.user

        # you can only comment your own comments or you have to be a moderator
        if comment.user != request.user and not comments_settings.IS_MODERATOR_FUNC(
                request.user):
            return HttpResponseForbidden(
                "You cannot edit other people's comments.")

    data, files = None, None
    if request.method == 'POST':
        data, files = request.POST, request.FILES

    form = FlatCommentMultiForm(context['object'],
                                user,
                                data=data,
                                files=files,
                                instance=comment)
    if form.is_valid():
        comment, success, reason = form.post()
        if not success:
            return HttpResponseForbidden(reason)
        comment_updated.send(sender=comment.__class__,
                             comment=comment,
                             updating_user=request.user,
                             date_updated=now())

        if request.is_ajax():
            return TemplateResponse(
                request,
                get_template('comment_detail_async.html', context['object']),
                context)
        return HttpResponseRedirect(
            comment.get_absolute_url(show_reversed(request)))

    context.update({'comment': comment, 'form': form})
    return TemplateResponse(
        request,
        get_template('comment_form.html', context['object'],
                     request.is_ajax()), context)
Beispiel #2
0
def comment_detail(request, context, comment_id):
    clist = CommentList.for_object(context['object'])
    try:
        comment = clist.get_comment(comment_id)
    except FlatComment.DoesNotExist:
        raise Http404()

    # avoid additional lookup
    comment.content_object = context['object']
    return HttpResponseRedirect(comment.get_absolute_url(show_reversed(request)))
Beispiel #3
0
    def get_comment_list(self, context):
        reversed = show_reversed(context['request'])
        if isinstance(self.lookup, template.Variable):
            return CommentList.for_object(self.lookup.resolve(context), reversed)

        ct, obj_pk = map(lambda v: v.resolve(context), self.lookup)
        if isinstance(ct, int):
            ct = ContentType.objects.get_for_id(ct)

        return CommentList(ct, obj_pk, reversed)
Beispiel #4
0
    def get_comment_list(self, context):
        reversed = show_reversed(context['request'])
        if isinstance(self.lookup, template.Variable):
            return CommentList.for_object(self.lookup.resolve(context), reversed)

        ct, obj_pk = map(lambda v: v.resolve(context), self.lookup)
        if isinstance(ct, int):
            ct = ContentType.objects.get_for_id(ct)

        return CommentList(ct, obj_pk, reversed)
Beispiel #5
0
def post_comment(request, context, comment_id=None):
    clist = CommentList.for_object(context['object'])

    if clist.locked():
        return TemplateResponse(request, get_template('comments_locked.html', context['object'], request.is_ajax()), context, status=403)

    comment = None
    user = request.user
    if comment_id:
        try:
            comment = clist.get_comment(comment_id)
        except FlatComment.DoesNotExist:
            raise Http404()

        # make sure we don't change user when mod is editting a comment
        user = comment.user

        # you can only comment your own comments or you have to be a moderator
        if comment.user != request.user and not comments_settings.IS_MODERATOR_FUNC(request.user):
            return HttpResponseForbidden("You cannot edit other people's comments.")

        # Don't allow users to edit a comment after the allowed edit time has expired
        if comment.user == request.user and not comments_settings.IS_MODERATOR_FUNC(request.user) and comment.is_edit_timer_expired():
            if request.is_ajax():
                context.update({'comment': comment})
                return TemplateResponse(request, get_template('comment_detail_async.html', context['object']), context)
            return HttpResponseForbidden("The allowed time to edit the comment has expired.")

    data, files = None, None
    if request.method == 'POST':
        data, files = request.POST, request.FILES

    form = FlatCommentMultiForm(context['object'], user, data=data, files=files, instance=comment)
    if form.is_valid():
        comment, success, reason = form.post(request)
        if not success:
            return HttpResponseForbidden(reason)
        comment_updated.send(
            sender=comment.__class__,
            comment=comment,
            updating_user=request.user,
            date_updated=now()
        )

        if request.is_ajax():
            context.update({'comment': comment})
            return TemplateResponse(request, get_template('comment_detail_async.html', context['object']), context)
        return HttpResponseRedirect(comment.get_absolute_url(show_reversed(request)))

    context.update({
        'comment': comment,
        'form': form
    })
    return TemplateResponse(request, get_template('comment_form.html', context['object'], request.is_ajax()), context)
Beispiel #6
0
def list_comments(request, context, reverse=None):
    if reverse is None:
        reverse = show_reversed(request)
    clist = CommentList.for_object(context['object'], reverse)
    paginator = Paginator(clist, comments_settings.PAGINATE_BY)
    try:
        context['page'] = paginator.page(request.GET.get('p', 1))
    except (PageNotAnInteger, EmptyPage):
        raise Http404()

    context['comment_list'] = context['page'].object_list
    context['is_paginated'] = context['page'].has_other_pages()
    return TemplateResponse(request, get_template('comment_list.html', context['object'], request.is_ajax()), context)
Beispiel #7
0
def list_comments(request, context, reverse=None):
    if reverse is None:
        reverse = show_reversed(request)
    clist = CommentList.for_object(context['object'], reverse)
    paginator = Paginator(clist, comments_settings.PAGINATE_BY)
    try:
        context['page'] = paginator.page(request.GET.get('p', 1))
    except (PageNotAnInteger, EmptyPage):
        raise Http404()

    context['comment_list'] = context['page'].object_list
    context['is_paginated'] = context['page'].has_other_pages()
    return TemplateResponse(
        request,
        get_template('comment_list.html', context['object'],
                     request.is_ajax()), context)