Exemplo n.º 1
0
    def append_context(self, queryset):
        """Appending collection data to context."""
        repo_version = self._repository_version
        collections_qs = queryset.annotate(available_versions=ArrayAgg("versions__version"))
        versions_context = {}
        for collection_id, available_versions in collections_qs.values_list(
            "pk", "available_versions"
        ):
            versions_context[collection_id] = available_versions

        self.available_versions_context = versions_context  # needed by get__serializer_context
        self._deprecation

        collections = Collection.objects.filter(
            pk__in=versions_context.keys(),
            versions__version_memberships__repository=repo_version.repository,
        ).annotate(
            repo_version_added_at=Window(
                expression=FirstValue(
                    "versions__version_memberships__version_added__pulp_last_updated"
                ),
                partition_by=[F("versions__collection_id")],
                order_by=F("versions__version_memberships__version_added__pulp_last_updated").desc(
                    nulls_last=True
                ),
            ),
            repo_version_removed_at=Window(
                expression=FirstValue(
                    "versions__version_memberships__version_removed__pulp_last_updated"
                ),
                partition_by=[F("versions__collection_id")],
                order_by=F(
                    "versions__version_memberships__version_removed__pulp_last_updated"
                ).desc(nulls_last=True),
            ),
        )

        return collections.distinct("versions__collection_id").only(
            "pulp_created", "name", "namespace"
        )
Exemplo n.º 2
0
    def test_last_activity__window_function(self):
        final_activities = Activity.objects.annotate(max_when=Window(
            expression=FirstValue('when'),
            partition_by=[F('who')],
            order_by=F('when').desc(),
        ))

        # final1 = final_activities.filter(when=F('max_when'))
        # print(final1.query)
        # will produce an sql error as django attempts to put the window function in the where clause
        # print(final1)

        cte = With(final_activities)
        final2 = cte.join(Activity, who=cte.col.who,
                          when=cte.col.max_when).with_cte(cte)
        print(final2.query)
        print(final2)