Exemplo n.º 1
0
    def validate_posts(self, data):
        if len(data) > POSTS_LIMIT:
            message = ngettext(
                "No more than %(limit)s post can be deleted at single time.",
                "No more than %(limit)s posts can be deleted at single time.",
                POSTS_LIMIT,
            )
            raise ValidationError(message % {'limit': POSTS_LIMIT})

        user = self.context['user']
        thread = self.context['thread']

        posts_queryset = exclude_invisible_posts(user, thread.category, thread.post_set)
        posts_queryset = posts_queryset.filter(id__in=data).order_by('id')

        posts = []
        for post in posts_queryset:
            post.category = thread.category
            post.thread = thread

            if post.is_event:
                allow_delete_event(user, post)
            else:
                allow_delete_best_answer(user, post)
                allow_delete_post(user, post)

            posts.append(post)

        if len(posts) != len(data):
            raise PermissionDenied(_("One or more posts to delete could not be found."))

        return posts
Exemplo n.º 2
0
def delete_post(request, thread, post):
    if post.is_event:
        allow_delete_event(request.user, post)
    else:
        allow_delete_post(request.user, post)

    moderation.delete_post(request.user, post)

    sync_related(thread)
    return Response({})
Exemplo n.º 3
0
    def delete(self, request, thread_pk, pk):
        thread = self.get_thread_for_update(request, thread_pk)
        post = self.get_post_for_update(request, thread, pk).unwrap()

        if post.is_event:
            allow_delete_event(request.user, post)
        else:
            allow_delete_post(request.user, post)

        moderation.delete_post(request.user, post)

        thread.synchronize()
        thread.save()

        thread.category.synchronize()
        thread.category.save()

        return Response({})
Exemplo n.º 4
0
def clean_posts_for_delete(request, thread):
    try:
        posts_ids = list(map(int, request.data or []))
    except (ValueError, TypeError):
        raise PermissionDenied(
            _("One or more post ids received were invalid."))

    if not posts_ids:
        raise PermissionDenied(
            _("You have to specify at least one post to delete."))
    elif len(posts_ids) > DELETE_LIMIT:
        message = ungettext(
            "No more than %(limit)s post can be deleted at single time.",
            "No more than %(limit)s posts can be deleted at single time.",
            DELETE_LIMIT,
        )
        raise PermissionDenied(message % {'limit': DELETE_LIMIT})

    posts_queryset = exclude_invisible_posts(request.user, thread.category,
                                             thread.post_set)
    posts_queryset = posts_queryset.filter(id__in=posts_ids).order_by('id')

    posts = []
    for post in posts_queryset:
        post.thread = thread
        if post.is_event:
            allow_delete_event(request.user, post)
        else:
            allow_delete_post(request.user, post)
        posts.append(post)

    if len(posts) != len(posts_ids):
        raise PermissionDenied(
            _("One or more posts to delete could not be found."))

    return posts