2022-11-18 04:04:01 +01:00
|
|
|
from django.utils.decorators import method_decorator
|
2022-12-17 01:06:29 +01:00
|
|
|
from django.views.generic import ListView
|
2022-11-18 04:04:01 +01:00
|
|
|
|
|
|
|
from users.decorators import identity_required
|
2022-12-17 01:06:29 +01:00
|
|
|
from users.models import Follow, FollowStates
|
2022-11-18 04:04:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
@method_decorator(identity_required, name="dispatch")
|
2022-12-17 01:06:29 +01:00
|
|
|
class Follows(ListView):
|
2022-11-18 04:04:01 +01:00
|
|
|
"""
|
|
|
|
Shows followers/follows.
|
|
|
|
"""
|
|
|
|
|
2022-12-04 17:41:41 +01:00
|
|
|
template_name = "activities/follows.html"
|
2022-12-17 01:06:29 +01:00
|
|
|
extra_context = {
|
|
|
|
"section": "follows",
|
|
|
|
}
|
|
|
|
paginate_by = 50
|
2022-11-18 04:04:01 +01:00
|
|
|
|
2022-12-17 01:24:56 +01:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
self.inbound = self.request.GET.get("inbound")
|
|
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
|
2022-12-17 01:06:29 +01:00
|
|
|
def get_queryset(self):
|
2022-12-17 01:24:56 +01:00
|
|
|
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")
|
2022-11-18 04:04:01 +01:00
|
|
|
|
2022-12-17 01:06:29 +01:00
|
|
|
def get_context_data(self):
|
|
|
|
context = super().get_context_data()
|
2022-12-17 01:24:56 +01:00
|
|
|
# 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
|
2022-12-17 01:32:04 +01:00
|
|
|
context["num_inbound"] = Follow.objects.filter(
|
|
|
|
target=self.request.identity,
|
|
|
|
state__in=FollowStates.group_active(),
|
|
|
|
).count()
|
|
|
|
context["num_outbound"] = Follow.objects.filter(
|
|
|
|
source=self.request.identity,
|
|
|
|
state__in=FollowStates.group_active(),
|
|
|
|
).count()
|
2022-12-17 01:06:29 +01:00
|
|
|
return context
|