def filter_queryset(self, queryset): queryset = super().filter_queryset(queryset) if self.action != 'list': # optimizing queryset for create, update, retrieve actions queryset = queryset.select_related( 'created_by', 'created_by__profile', 'parent_topic', 'parent_board', ).prefetch_related( Scrap.prefetch_my_scrap(self.request.user), models.Prefetch( 'comment_set', queryset=Comment.objects.order_by('created_at'). select_related('attachment', ).prefetch_related( 'comment_update_log_set', Vote.prefetch_my_vote(self.request.user), Block.prefetch_my_block(self.request.user), Report.prefetch_my_report(self.request.user), models.Prefetch( 'comment_set', queryset=Comment.objects.order_by('created_at'). select_related('attachment', ).prefetch_related( 'comment_update_log_set', Vote.prefetch_my_vote(self.request.user), Block.prefetch_my_block(self.request.user), Report.prefetch_my_report(self.request.user), ), ), ), ), ) return queryset
def get_queryset(self): queryset = super(ReportViewSet, self).get_queryset() queryset = queryset.filter( reported_by=self.request.user, ).select_related( 'reported_by', 'reported_by__profile', 'parent_article', 'parent_article__created_by', 'parent_article__created_by__profile', 'parent_article__parent_topic', 'parent_article__parent_board', 'parent_comment', 'parent_comment__created_by', 'parent_comment__created_by__profile', ).prefetch_related( 'parent_article__comment_set', 'parent_article__comment_set__comment_set', 'parent_article__attachments', 'parent_article__article_update_log_set', Block.prefetch_my_block(self.request.user, prefix='parent_article__'), Block.prefetch_my_block(self.request.user, prefix='parent_comment__'), ArticleReadLog.prefetch_my_article_read_log( self.request.user, prefix='parent_article__'), ) return queryset
def validate_hidden(self, obj): errors = [] if Block.is_blocked(blocked_by=self.context['request'].user, user=obj.created_by): errors.append(exceptions.ValidationError('차단한 사용자의 게시물입니다.')) return errors
def get_created_by(self, obj): from apps.user.serializers.user import PublicUserSerializer if obj.is_anonymous: return '익명' data = PublicUserSerializer(obj.created_by).data data['is_blocked'] = Block.is_blocked( blocked_by=self.context['request'].user, user=obj.created_by) return data
def paginate_queryset(self, queryset): # optimizing queryset for list action queryset = queryset.select_related( 'created_by', 'created_by__profile', 'parent_topic', 'parent_board', ).prefetch_related( 'attachments', 'article_update_log_set', Block.prefetch_my_block(self.request.user), ArticleReadLog.prefetch_my_article_read_log(self.request.user), ) return super().paginate_queryset(queryset)
def validate_hidden(self, obj: Article): errors = [] request = self.context['request'] if Block.is_blocked(blocked_by=request.user, user=obj.created_by): errors.append(exceptions.ValidationError('차단한 사용자의 게시물입니다.')) if obj.is_content_sexual and not request.user.profile.see_sexual: errors.append(exceptions.ValidationError('성인/음란성 내용의 게시물입니다.')) if obj.is_content_social and not request.user.profile.see_social: errors.append(exceptions.ValidationError('정치/사회성 내용의 게시물입니다.')) if not obj.parent_board.group_has_access(request.user.profile.group): errors.append(exceptions.ValidationError('접근 권한이 없는 게시판입니다.')) return errors