diff --git a/contentcuration/contentcuration/frontend/channelEdit/views/ImportFromChannels/ChannelList.vue b/contentcuration/contentcuration/frontend/channelEdit/views/ImportFromChannels/ChannelList.vue index dfa961a3b6..58bb5390af 100644 --- a/contentcuration/contentcuration/frontend/channelEdit/views/ImportFromChannels/ChannelList.vue +++ b/contentcuration/contentcuration/frontend/channelEdit/views/ImportFromChannels/ChannelList.vue @@ -124,6 +124,7 @@ [this.channelFilter]: true, page: this.$route.query.page || 1, exclude: this.currentChannelId, + ordering: this.channelFilter === 'public' ? 'name' : '-modified', }).then(page => { this.pageCount = page.total_pages; this.channels = page.results; diff --git a/contentcuration/contentcuration/viewsets/channel.py b/contentcuration/contentcuration/viewsets/channel.py index 07d27b7281..9c739cbdfe 100644 --- a/contentcuration/contentcuration/viewsets/channel.py +++ b/contentcuration/contentcuration/viewsets/channel.py @@ -382,9 +382,18 @@ def format_demo_server_url(item): def _unpublished_changes_query(channel): + """ + :param channel: Either an `OuterRef` or `Channel` object + :type channel: Channel|OuterRef + :return: QuerySet for unpublished changes + """ + # double wrap the channel if it's an outer ref so that we can match the outermost channel + # to optimize query performance + channel_ref = OuterRef(channel) if isinstance(channel, OuterRef) else channel + return Change.objects.filter( server_rev__gt=Coalesce(Change.objects.filter( - channel=OuterRef("channel"), + channel=channel_ref, change_type=PUBLISHED, errored=False ).values("server_rev").order_by("-server_rev")[:1], Value(0)), @@ -400,6 +409,8 @@ class ChannelViewSet(ValuesViewset): serializer_class = ChannelSerializer pagination_class = ChannelListPagination filterset_class = ChannelFilter + ordering_fields = ["modified", "name"] + ordering = "-modified" field_map = channel_field_map values = base_channel_values + ("edit", "view", "unpublished_changes") @@ -412,6 +423,15 @@ def get_queryset(self): queryset = super(ChannelViewSet, self).get_queryset() user_id = not self.request.user.is_anonymous and self.request.user.id user_queryset = User.objects.filter(id=user_id) + # Add the last modified node modified value as the channel last modified + channel_main_tree_nodes = ContentNode.objects.filter( + tree_id=OuterRef("main_tree__tree_id") + ) + queryset = queryset.annotate( + modified=Subquery( + channel_main_tree_nodes.values("modified").order_by("-modified")[:1] + ) + ) return queryset.annotate( edit=Exists(user_queryset.filter(editable_channels=OuterRef("id"))), @@ -434,12 +454,6 @@ def annotate_queryset(self, queryset): queryset = queryset.annotate( count=SQCount(non_topic_content_ids, field="content_id"), ) - # Add the last modified node modified value as the channel last modified - queryset = queryset.annotate( - modified=Subquery( - channel_main_tree_nodes.values("modified").order_by("-modified")[:1] - ) - ) queryset = queryset.annotate(unpublished_changes=Exists(_unpublished_changes_query(OuterRef("id")))) @@ -488,7 +502,7 @@ def publish(self, pk, version_notes="", language=None): "publishing": False, "primary_token": channel.get_human_token().token, "last_published": channel.last_published, - "unpublished_changes": _unpublished_changes_query(channel.id).exists() + "unpublished_changes": _unpublished_changes_query(channel).exists() }, channel_id=channel.id ), ], applied=True)