def validate_posts(self, data): data = list(set(data)) if len(data) < 2: raise ValidationError(self.error_empty_or_required) if len(data) > POSTS_LIMIT: message = ungettext( "No more than %(limit)s post can be merged at single time.", "No more than %(limit)s posts can be merged 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 try: allow_merge_post(user, post) except PermissionDenied as e: raise ValidationError(e) if not posts: posts.append(post) continue authorship_error = _( "Posts made by different users can't be merged.") if post.poster_id != posts[0].poster_id: raise serializers.ValidationError(authorship_error) elif (post.poster_id is None and posts[0].poster_id is None and post.poster_name != posts[0].poster_name): raise serializers.ValidationError(authorship_error) if posts[0].is_first_post and post.is_best_answer: raise serializers.ValidationError( _("Post marked as best answer can't be merged with thread's first post." )) if not posts[0].is_first_post: if (posts[0].is_hidden != post.is_hidden or posts[0].is_unapproved != post.is_unapproved): raise serializers.ValidationError( _("Posts with different visibility can't be merged.")) posts.append(post) if len(posts) != len(data): raise ValidationError( _("One or more posts to merge could not be found.")) return posts
def validate_posts(self, data): data = list(set(data)) if len(data) < 2: raise serializers.ValidationError(_("You have to select at least two posts to merge.")) if len(data) > POSTS_MERGE_LIMIT: message = ungettext( "No more than %(limit)s post can be merged at single time.", "No more than %(limit)s posts can be merged at single time.", POSTS_MERGE_LIMIT, ) raise serializers.ValidationError(message % {'limit': POSTS_MERGE_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 try: allow_merge_post(user, post) except PermissionDenied as e: raise serializers.ValidationError(six.text_type(e)) if not posts: posts.append(post) else: authorship_error = _("Posts made by different users can't be merged.") if posts[0].poster_id: if post.poster_id != posts[0].poster_id: raise serializers.ValidationError(authorship_error) else: if post.poster_id or post.poster_name != posts[0].poster_name: raise serializers.ValidationError(authorship_error) if posts[0].pk != thread.first_post_id: if (posts[0].is_hidden != post.is_hidden or posts[0].is_unapproved != post.is_unapproved): raise serializers.ValidationError( _("Posts with different visibility can't be merged.") ) posts.append(post) if len(posts) != len(data): raise serializers.ValidationError(_("One or more posts to merge could not be found.")) self.posts_cache = posts return data
def clean_posts_for_merge(request, thread): try: posts_ids = list(map(int, request.data.get('posts', []))) except (ValueError, TypeError): raise PermissionDenied( _("One or more post ids received were invalid.")) if len(posts_ids) < 2: raise PermissionDenied( _("You have to select at least two posts to merge.")) elif len(posts_ids) > MERGE_LIMIT: message = ungettext( "No more than %(limit)s post can be merged at single time.", "No more than %(limit)s posts can be merged at single time.", MERGE_LIMIT, ) raise PermissionDenied(message % {'limit': MERGE_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_merge_post(request.user, post) if not posts: posts.append(post) else: authorship_error = _( "Posts made by different users can't be merged.") if posts[0].poster_id: if post.poster_id != posts[0].poster_id: raise PermissionDenied(authorship_error) else: if post.poster_id or post.poster_name != posts[0].poster_name: raise PermissionDenied(authorship_error) if posts[0].pk != thread.first_post_id: if (posts[0].is_hidden != post.is_hidden or posts[0].is_unapproved != post.is_unapproved): raise PermissionDenied( _("Posts with different visibility can't be merged.")) posts.append(post) if len(posts) != len(posts_ids): raise PermissionDenied( _("One or more posts to merge could not be found.")) return posts