From dd88bcea29e312463d29b9dd28d9d14eae2c89bf Mon Sep 17 00:00:00 2001 From: Jason McBrayer Date: Fri, 18 May 2018 09:22:27 -0400 Subject: [PATCH] Home, local, and public timelines have simple pagination. Notification and tag timelines should also have pagination, but they have to be done separately. --- brutaldon/templates/main/home_timeline.html | 1 + brutaldon/templates/main/local_timeline.html | 12 +++++++ brutaldon/templates/main/public_timeline.html | 12 +++++++ brutaldon/templates/main/timeline.html | 14 ++++++-- brutaldon/urls.py | 6 ++++ brutaldon/views.py | 36 ++++++++++++------- 6 files changed, 67 insertions(+), 14 deletions(-) create mode 100644 brutaldon/templates/main/home_timeline.html create mode 100644 brutaldon/templates/main/local_timeline.html create mode 100644 brutaldon/templates/main/public_timeline.html diff --git a/brutaldon/templates/main/home_timeline.html b/brutaldon/templates/main/home_timeline.html new file mode 100644 index 0000000..f04b1ed --- /dev/null +++ b/brutaldon/templates/main/home_timeline.html @@ -0,0 +1 @@ +{{% extends "main/timeline.html" %} diff --git a/brutaldon/templates/main/local_timeline.html b/brutaldon/templates/main/local_timeline.html new file mode 100644 index 0000000..b29b8c1 --- /dev/null +++ b/brutaldon/templates/main/local_timeline.html @@ -0,0 +1,12 @@ +{% extends "main/timeline.html" %} + +{% block pagination %} + +{% endblock %} diff --git a/brutaldon/templates/main/public_timeline.html b/brutaldon/templates/main/public_timeline.html new file mode 100644 index 0000000..9df48bc --- /dev/null +++ b/brutaldon/templates/main/public_timeline.html @@ -0,0 +1,12 @@ +{% extends "main/timeline.html" %} + +{% block pagination %} + +{% endblock %} diff --git a/brutaldon/templates/main/timeline.html b/brutaldon/templates/main/timeline.html index 6548b27..ab9a290 100644 --- a/brutaldon/templates/main/timeline.html +++ b/brutaldon/templates/main/timeline.html @@ -2,7 +2,7 @@ {% load humanize %} {% block title %} - Brutaldon - {{ timeline }} timelime + Brutaldon - {{ timeline_name }} timelime {% endblock %} {% block content %} @@ -13,7 +13,7 @@ {% endif %} -

Your {{ timeline }} timeline

+

Your {{ timeline_name }} timeline

{% for toot in toots %}
{% if toot.reblog %} @@ -25,4 +25,14 @@ {% endfor %} + {% block pagination %} + + {% endblock %} {% endblock %} diff --git a/brutaldon/urls.py b/brutaldon/urls.py index 86d0e41..492d0b8 100644 --- a/brutaldon/urls.py +++ b/brutaldon/urls.py @@ -19,6 +19,8 @@ from brutaldon import views urlpatterns = [ path('admin/', admin.site.urls), + path('home/next/', views.home, name='home_next'), + path('home/prev/', views.home, name='home_prev'), path('home', views.home, name='home'), path('login', views.login, name="login"), path('oldlogin', views.old_login, name="oldlogin"), @@ -27,7 +29,11 @@ urlpatterns = [ path('error', views.error, name='error'), path('note', views.note, name='note'), path('local', views.local, name='local'), + path('local/next/', views.local, name='local_next'), + path('local/prev/', views.local, name='local_prev'), path('fed', views.fed, name='fed'), + path('fed/next/', views.fed, name='fed_next'), + path('fed/prev/', views.fed, name='fed_prev'), path('settings', views.settings, name='settings'), path('thread/', views.thread, name='thread'), path('tags/', views.tag, name='tag'), diff --git a/brutaldon/views.py b/brutaldon/views.py index 38a4105..6052691 100644 --- a/brutaldon/views.py +++ b/brutaldon/views.py @@ -2,7 +2,7 @@ from django.http import HttpResponse, Http404 from django.shortcuts import render, redirect from django.views.decorators.cache import never_cache from django.urls import reverse -from django.core.files.uploadhandler import TemporaryFileUploadHandler +from django.core.files.uploadhandler import TemporaryFileUploadHandler from brutaldon.forms import LoginForm, OAuthLoginForm, SettingsForm, PostForm from brutaldon.models import Client, Account from mastodon import Mastodon @@ -51,25 +51,37 @@ def fullbrutalism_p(request): fullbrutalism = False return fullbrutalism -def timeline(request, timeline='home', timeline_name='Home'): +def timeline(request, timeline='home', timeline_name='Home', max_id=None, since_id=None): try: mastodon = get_mastodon(request) except NotLoggedInException: return redirect(login) - data = mastodon.timeline(timeline) + data = mastodon.timeline(timeline, limit=100, max_id=max_id, since_id=since_id) form = PostForm() - return render(request, 'main/timeline.html', - {'toots': data, 'form': form, 'timeline': timeline_name, - 'fullbrutalism': fullbrutalism_p(request)}) + try: + prev = data[0]._pagination_prev + if len(mastodon.timeline(since_id=prev['since_id'])) == 0: + prev = None + except IndexError: + prev = None + try: + next = data[-1]._pagination_next + except IndexError: + next = None + return render(request, 'main/%s_timeline.html' % timeline, + {'toots': data, 'form': form, 'timeline': timeline, + 'timeline_name': timeline_name, + 'fullbrutalism': fullbrutalism_p(request), + 'prev': prev, 'next': next}) -def home(request): - return timeline(request, 'home', 'Home') +def home(request, next=None, prev=None): + return timeline(request, 'home', 'Home', max_id=next, since_id=prev) -def local(request): - return timeline(request, 'local', 'Local') +def local(request, next=None, prev=None): + return timeline(request, 'local', 'Local', max_id=next, since_id=prev) -def fed(request): - return timeline(request, 'public', 'Federated') +def fed(request, next=None, prev=None): + return timeline(request, 'public', 'Federated', max_id=next, since_id=prev) def tag(request, tag): try: