From fb2eea956ef7416eb755ebecf58db7f9a57215c4 Mon Sep 17 00:00:00 2001 From: Gabriel Rodríguez Alberich Date: Sat, 17 Dec 2022 01:06:29 +0100 Subject: Several pagination improvements (#170) Home/Notification gets pagination, Follows becomes ListView--- activities/views/follows.py | 46 ++++++++++++++++++++++++------------------- activities/views/timelines.py | 18 ++++++++++------- 2 files changed, 37 insertions(+), 27 deletions(-) (limited to 'activities/views') diff --git a/activities/views/follows.py b/activities/views/follows.py index 44d8adc..6b0881a 100644 --- a/activities/views/follows.py +++ b/activities/views/follows.py @@ -1,33 +1,39 @@ +from django.db.models import Q from django.utils.decorators import method_decorator -from django.views.generic import TemplateView +from django.views.generic import ListView from users.decorators import identity_required -from users.models import FollowStates +from users.models import Follow, FollowStates @method_decorator(identity_required, name="dispatch") -class FollowsPage(TemplateView): +class Follows(ListView): """ Shows followers/follows. """ template_name = "activities/follows.html" + extra_context = { + "section": "follows", + } + paginate_by = 50 - def get_context_data(self): - # Gather all identities with a following relationship with us - identities = {} - for outbound_follow in self.request.identity.outbound_follows.filter( - state__in=FollowStates.group_active() - ): - identities.setdefault(outbound_follow.target, {})[ - "outbound" - ] = outbound_follow - for inbound_follow in self.request.identity.inbound_follows.filter( - state__in=FollowStates.group_active() - ): - identities.setdefault(inbound_follow.source, {})["inbound"] = inbound_follow + 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") - return { - "section": "follows", - "identities": sorted(identities.items(), key=lambda i: i[0].username), - } + 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 + return context diff --git a/activities/views/timelines.py b/activities/views/timelines.py index d797cfd..84e490f 100644 --- a/activities/views/timelines.py +++ b/activities/views/timelines.py @@ -1,3 +1,4 @@ +from django.core.paginator import Paginator from django.shortcuts import get_object_or_404, redirect from django.template.defaultfilters import linebreaks_filter from django.utils.decorators import method_decorator @@ -22,20 +23,23 @@ class Home(FormView): def get_context_data(self): context = super().get_context_data() - context["events"] = list( + events = ( TimelineEvent.objects.filter( identity=self.request.identity, type__in=[TimelineEvent.Types.post, TimelineEvent.Types.boost], ) .select_related("subject_post", "subject_post__author") .prefetch_related("subject_post__attachments", "subject_post__mentions") - .order_by("-created")[:50] + .order_by("-created") ) context["interactions"] = PostInteraction.get_event_interactions( - context["events"], self.request.identity + events, self.request.identity ) context["current_page"] = "home" context["allows_refresh"] = True + paginator = Paginator(events, 50) + page_number = self.request.GET.get("page") + context["page_obj"] = paginator.get_page(page_number) return context def form_valid(self, form): @@ -74,7 +78,7 @@ class Tag(ListView): .tagged_with(self.hashtag) .select_related("author") .prefetch_related("attachments", "mentions") - .order_by("-created")[:50] + .order_by("-created") ) def get_context_data(self): @@ -103,7 +107,7 @@ class Local(ListView): Post.objects.local_public() .select_related("author", "author__domain") .prefetch_related("attachments", "mentions", "emojis") - .order_by("-created")[:50] + .order_by("-created") ) def get_context_data(self): @@ -131,7 +135,7 @@ class Federated(ListView): ) .select_related("author", "author__domain") .prefetch_related("attachments", "mentions", "emojis") - .order_by("-created")[:50] + .order_by("-created") ) def get_context_data(self): @@ -175,7 +179,7 @@ class Notifications(ListView): types.append(type) return ( TimelineEvent.objects.filter(identity=self.request.identity, type__in=types) - .order_by("-created")[:50] + .order_by("-created") .select_related( "subject_post", "subject_post__author", -- cgit v1.2.3