Example #1
0
    def validate_posts(self, data):
        data = list(set(data))
        if len(data) > POSTS_MOVE_LIMIT:
            message = ungettext(
                "No more than %(limit)s post can be moved at single time.",
                "No more than %(limit)s posts can be moved at single time.",
                POSTS_MOVE_LIMIT,
            )
            raise serializers.ValidationError(message % {'limit': POSTS_MOVE_LIMIT})

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

        posts_queryset = exclude_invisible_posts(request.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

            try:
                allow_move_post(request.user, post)
                posts.append(post)
            except PermissionDenied as e:
                raise serializers.ValidationError(six.text_type(e))

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

        self.posts_cache = posts

        return data
Example #2
0
def clean_posts_for_move(request, thread):
    posts_ids = clean_ids_list(
        request.data.get('posts', []),
        _("One or more post ids received were invalid."),
    )

    if not posts_ids:
        raise PermissionDenied(
            _("You have to specify at least one post to move."))
    elif len(posts_ids) > MOVE_LIMIT:
        message = ungettext(
            "No more than %(limit)s post can be moved at single time.",
            "No more than %(limit)s posts can be moved at single time.",
            MOVE_LIMIT,
        )
        raise PermissionDenied(message % {'limit': MOVE_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.category = thread.category
        post.thread = thread

        allow_move_post(request.user, post)
        posts.append(post)

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

    return posts