def get_queryset_with_related_ips()

in src/olympia/amo/admin.py [0:0]


    def get_queryset_with_related_ips(self, request, queryset, ips_and_networks):
        condition = models.Q()
        if ips_and_networks is not None:
            if ips_and_networks['ips']:
                # IPs search can be implemented in a single __in=() query.
                arg = (
                    self.get_activity_accessor_prefix() + 'iplog__ip_address_binary__in'
                )
                condition |= models.Q(**{arg: ips_and_networks['ips']})
            if ips_and_networks['networks']:
                # Networks search need one __range conditions for each network.
                arg = (
                    self.get_activity_accessor_prefix()
                    + 'iplog__ip_address_binary__range'
                )
                for network in ips_and_networks['networks']:
                    condition |= models.Q(**{arg: (network[0], network[-1])})
        if condition or (
            'known_ip_adresses' in self.list_display
            and 'for_count' not in queryset.query.annotations
        ):
            queryset = queryset.annotate(
                activity_ips=GroupConcat(
                    Inet6Ntoa(
                        self.get_activity_accessor_prefix() + 'iplog__ip_address_binary'
                    ),
                    distinct=True,
                )
            )
        if condition:
            arg = self.get_activity_accessor_prefix() + 'action__in'
            condition &= models.Q(**{arg: self.search_by_ip_actions})
            # When searching, we want to duplicate the joins against
            # activitylog + iplog so that one is used for the group concat
            # showing all IPs for activities related to that object and another
            # for the search results. Django doesn't let us do that out of the
            # box, but through FilteredRelation we can force it...
            aliases = {
                # Add an alias for {get_activity_accessor_prefix()}__iplog__id
                # so that we can apply a filter on the specific JOIN that will be
                # used to grab the IPs through GroupConcat to help MySQL optimizer
                # remove non relevant activities from the DISTINCT bit.
                'activity_ips_ids': models.F(
                    self.get_activity_accessor_prefix() + 'iplog__id'
                ),
                'activitylog_filtered': models.FilteredRelation(
                    self.get_activity_accessor_prefix() + 'iplog', condition=condition
                ),
            }
            queryset = queryset.alias(**aliases).filter(
                activity_ips_ids__isnull=False,
                activitylog_filtered__isnull=False,
            )
        # A GROUP_BY will already have been applied thanks to our annotations
        # so we can let django know there won't be any duplicates and avoid
        # doing a DISTINCT.
        may_have_duplicates = False
        return queryset, may_have_duplicates