Esempio n. 1
0
    def filter_queryset(self, queryset):
        queryset = super().filter_queryset(queryset)

        # optimizing queryset for list action
        if self.action in ['list', 'recent']:
            queryset = queryset.select_related(
                'created_by',
                'created_by__profile',
                'parent_topic',
                'parent_board',
            ).prefetch_related(
                ArticleReadLog.prefetch_my_article_read_log(
                    self.request.user), )

            # optimizing queryset for recent action
            if self.action == 'recent':
                last_read_log_of_the_article = ArticleReadLog.objects.filter(
                    article=models.OuterRef('pk')).order_by('-created_at')

                queryset = queryset.filter(
                    article_read_log_set__read_by=self.request.user,
                ).annotate(my_last_read_at=models.Subquery(
                    last_read_log_of_the_article.filter(
                        read_by=self.request.user, ).values('created_at')[:1]),
                           ).order_by('-my_last_read_at').distinct()

        # optimizing queryset for create, update, retrieve actions
        else:
            queryset = queryset.select_related(
                'created_by',
                'created_by__profile',
                'parent_topic',
                'parent_board',
            ).prefetch_related(
                'attachments',
                models.Prefetch(
                    'vote_set',
                    queryset=Vote.objects.select_related('voted_by', ),
                ),
                Scrap.prefetch_my_scrap(self.request.user),
                models.Prefetch(
                    'comment_set',
                    queryset=Comment.objects.reverse().select_related(
                        'created_by',
                        'created_by__profile',
                    ).prefetch_related(
                        Vote.prefetch_my_vote(self.request.user),
                        models.Prefetch(
                            'comment_set',
                            queryset=Comment.objects.reverse().select_related(
                                'created_by',
                                'created_by__profile',
                            ).prefetch_related(
                                Vote.prefetch_my_vote(self.request.user), ),
                        ),
                    ),
                ),
            )

        return queryset
Esempio n. 2
0
    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
Esempio n. 3
0
    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)
Esempio n. 4
0
    def get_queryset(self):
        queryset = super(ScrapViewSet, self).get_queryset()

        queryset = queryset.filter(
            scrapped_by=self.request.user, ).select_related(
                'scrapped_by',
                'scrapped_by__profile',
                'parent_article',
                'parent_article__created_by',
                'parent_article__created_by__profile',
                'parent_article__parent_topic',
                'parent_article__parent_board',
            ).prefetch_related(
                'parent_article__attachments',
                ArticleReadLog.prefetch_my_article_read_log(
                    self.request.user, prefix='parent_article__'),
            )

        return queryset