def validate_posts(self, data): if len(data) > POSTS_LIMIT: message = ngettext( "No more than %(limit)s post can be split at single time.", "No more than %(limit)s posts can be split at single time.", POSTS_LIMIT, ) raise ValidationError(message % {'limit': POSTS_LIMIT}) thread = self.context['thread'] user = self.context['user'] 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 try: allow_split_post(user, post) except PermissionDenied as e: raise ValidationError(e) posts.append(post) if len(posts) != len(data): raise ValidationError(_("One or more posts to split could not be found.")) return posts
def clean_posts_for_split(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 split.")) elif len(posts_ids) > SPLIT_LIMIT: message = ungettext( "No more than %(limit)s post can be split at single time.", "No more than %(limit)s posts can be split at single time.", SPLIT_LIMIT, ) raise PermissionDenied(message % {'limit': SPLIT_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_split_post(request.user, post) posts.append(post) if len(posts) != len(posts_ids): raise PermissionDenied(_("One or more posts to split could not be found.")) return posts