2022-12-21 11:09:18 +00:00
|
|
|
from django.db import models
|
2022-12-17 00:06:29 +00:00
|
|
|
from django.views.generic import ListView
|
2022-11-18 03:04:01 +00:00
|
|
|
|
2022-12-21 20:56:52 +00:00
|
|
|
from users.models import Follow, FollowStates, IdentityStates
|
2023-05-04 04:42:37 +00:00
|
|
|
from users.views.base import IdentityViewMixin
|
2022-11-18 03:04:01 +00:00
|
|
|
|
|
|
|
|
2023-05-04 04:42:37 +00:00
|
|
|
class FollowsPage(IdentityViewMixin, ListView):
|
2022-11-18 03:04:01 +00:00
|
|
|
"""
|
|
|
|
Shows followers/follows.
|
|
|
|
"""
|
|
|
|
|
2023-05-04 04:42:37 +00:00
|
|
|
template_name = "settings/follows.html"
|
2022-12-17 00:06:29 +00:00
|
|
|
extra_context = {
|
|
|
|
"section": "follows",
|
|
|
|
}
|
|
|
|
paginate_by = 50
|
2022-11-18 03:04:01 +00:00
|
|
|
|
2022-12-17 00:24:56 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
self.inbound = self.request.GET.get("inbound")
|
|
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
|
2022-12-17 00:06:29 +00:00
|
|
|
def get_queryset(self):
|
2022-12-17 00:24:56 +00:00
|
|
|
if self.inbound:
|
2023-05-04 04:42:37 +00:00
|
|
|
follow_dir = models.Q(target=self.identity)
|
2022-12-17 00:24:56 +00:00
|
|
|
else:
|
2023-05-04 04:42:37 +00:00
|
|
|
follow_dir = models.Q(source=self.identity)
|
2022-12-21 11:09:18 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
Follow.objects.filter(
|
|
|
|
follow_dir,
|
2022-12-17 00:24:56 +00:00
|
|
|
state__in=FollowStates.group_active(),
|
2022-12-21 11:09:18 +00:00
|
|
|
)
|
|
|
|
.select_related(
|
|
|
|
"target",
|
|
|
|
"target__domain",
|
|
|
|
"source",
|
|
|
|
"source__domain",
|
|
|
|
)
|
2022-12-21 20:56:52 +00:00
|
|
|
.exclude(source__state__in=IdentityStates.group_deleted())
|
|
|
|
.exclude(target__state__in=IdentityStates.group_deleted())
|
2022-12-21 11:09:18 +00:00
|
|
|
.order_by("-created")
|
|
|
|
)
|
2022-11-18 03:04:01 +00:00
|
|
|
|
2022-12-17 00:39:10 +00:00
|
|
|
def follows_to_identities(self, follows, attr):
|
|
|
|
"""
|
|
|
|
Turns a list of follows into a list of identities (ith the
|
|
|
|
follow creation date preserved on them.
|
|
|
|
"""
|
|
|
|
result = []
|
|
|
|
for follow in follows:
|
|
|
|
identity = getattr(follow, attr)
|
|
|
|
identity.follow_date = follow.state_changed
|
|
|
|
result.append(identity)
|
|
|
|
return result
|
|
|
|
|
2022-12-17 00:06:29 +00:00
|
|
|
def get_context_data(self):
|
|
|
|
context = super().get_context_data()
|
2022-12-17 00:24:56 +00:00
|
|
|
# Go work out if any of these people also follow us/are followed
|
|
|
|
if self.inbound:
|
2022-12-17 00:39:10 +00:00
|
|
|
context["page_obj"].object_list = self.follows_to_identities(
|
|
|
|
context["page_obj"], "source"
|
|
|
|
)
|
2022-12-17 00:24:56 +00:00
|
|
|
identity_ids = [identity.id for identity in context["page_obj"]]
|
|
|
|
context["outbound_ids"] = Follow.objects.filter(
|
2023-05-04 04:42:37 +00:00
|
|
|
source=self.identity,
|
2022-12-21 19:57:14 +00:00
|
|
|
target_id__in=identity_ids,
|
|
|
|
state__in=FollowStates.group_active(),
|
2022-12-17 00:24:56 +00:00
|
|
|
).values_list("target_id", flat=True)
|
|
|
|
else:
|
2022-12-17 00:39:10 +00:00
|
|
|
context["page_obj"].object_list = self.follows_to_identities(
|
|
|
|
context["page_obj"], "target"
|
|
|
|
)
|
2022-12-17 00:24:56 +00:00
|
|
|
identity_ids = [identity.id for identity in context["page_obj"]]
|
|
|
|
context["inbound_ids"] = Follow.objects.filter(
|
2023-05-04 04:42:37 +00:00
|
|
|
target=self.identity,
|
2022-12-21 19:57:14 +00:00
|
|
|
source_id__in=identity_ids,
|
|
|
|
state__in=FollowStates.group_active(),
|
2022-12-17 00:24:56 +00:00
|
|
|
).values_list("source_id", flat=True)
|
|
|
|
context["inbound"] = self.inbound
|
2022-12-17 00:32:04 +00:00
|
|
|
context["num_inbound"] = Follow.objects.filter(
|
2023-05-04 04:42:37 +00:00
|
|
|
target=self.identity,
|
2022-12-17 00:32:04 +00:00
|
|
|
state__in=FollowStates.group_active(),
|
|
|
|
).count()
|
|
|
|
context["num_outbound"] = Follow.objects.filter(
|
2023-05-04 04:42:37 +00:00
|
|
|
source=self.identity,
|
2022-12-17 00:32:04 +00:00
|
|
|
state__in=FollowStates.group_active(),
|
|
|
|
).count()
|
2023-05-04 04:42:37 +00:00
|
|
|
context["identity"] = self.identity
|
2022-12-17 00:06:29 +00:00
|
|
|
return context
|