From 00795f119eeb4b8eb1330b7959ca5ef93accc64b Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Fri, 16 Dec 2022 17:24:56 -0700 Subject: Split Follows page into two types This overcomes the query problem of pulling a combined list --- activities/views/follows.py | 47 ++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) (limited to 'activities') diff --git a/activities/views/follows.py b/activities/views/follows.py index 6b0881a..f5f5909 100644 --- a/activities/views/follows.py +++ b/activities/views/follows.py @@ -1,4 +1,3 @@ -from django.db.models import Q from django.utils.decorators import method_decorator from django.views.generic import ListView @@ -18,22 +17,40 @@ class Follows(ListView): } paginate_by = 50 + def get(self, request, *args, **kwargs): + self.inbound = self.request.GET.get("inbound") + return super().get(request, *args, **kwargs) + def get_queryset(self): - return Follow.objects.filter( - Q(source=self.request.identity) | Q(target=self.request.identity), - state__in=FollowStates.group_active(), - ).order_by("-created") + if self.inbound: + return Follow.objects.filter( + target=self.request.identity, + state__in=FollowStates.group_active(), + ).order_by("-created") + else: + return Follow.objects.filter( + source=self.request.identity, + state__in=FollowStates.group_active(), + ).order_by("-created") def get_context_data(self): context = super().get_context_data() - identities = [] - for follow in context["page_obj"].object_list: - if follow.source == self.request.identity: - identity = follow.target - follow_type = "outbound" - else: - identity = follow.source - follow_type = "inbound" - identities.append((identity, follow_type)) - context["page_obj"].object_list = identities + # Go work out if any of these people also follow us/are followed + if self.inbound: + context["page_obj"].object_list = [ + follow.source for follow in context["page_obj"] + ] + identity_ids = [identity.id for identity in context["page_obj"]] + context["outbound_ids"] = Follow.objects.filter( + source=self.request.identity, target_id__in=identity_ids + ).values_list("target_id", flat=True) + else: + context["page_obj"].object_list = [ + follow.target for follow in context["page_obj"] + ] + identity_ids = [identity.id for identity in context["page_obj"]] + context["inbound_ids"] = Follow.objects.filter( + target=self.request.identity, source_id__in=identity_ids + ).values_list("source_id", flat=True) + context["inbound"] = self.inbound return context -- cgit v1.2.3