From 56151b78c4f22a9f7793abd21fe859fb3d39c05c Mon Sep 17 00:00:00 2001 From: pluja Date: Thu, 1 Oct 2020 09:47:45 +0200 Subject: [PATCH] Adds pagination on twitter feed This improves performance on low-end devices --- app/routes.py | 15 +++++++++++++-- app/templates/twitter.html | 15 +++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/app/routes.py b/app/routes.py index dda3854..21b28b6 100644 --- a/app/routes.py +++ b/app/routes.py @@ -59,8 +59,10 @@ def index(): return render_template('home.html', config=config) @app.route('/twitter') +@app.route('/twitter/') @login_required -def twitter(): +def twitter(page=0): + page = int(page) start_time = time.time() followingList = current_user.twitter_following_list() followCount = len(followingList) @@ -69,12 +71,21 @@ def twitter(): form = EmptyForm() posts.extend(getFeed(followingList)) posts.sort(key=lambda x: x.timeStamp, reverse=True) + + npage = page*10 + tenmore = npage+10 + pages = int(math.ceil(len(posts)/10)) + if posts and len(posts) > tenmore: + posts = posts[npage:tenmore] + else: + posts = posts[npage:] + if not posts: profilePic = avatarPath else: profilePic = posts[0].userProfilePic print("--- {} seconds fetching twitter feed---".format(time.time() - start_time)) - return render_template('twitter.html', title='Yotter | Twitter', posts=posts, avatar=avatarPath, profilePic = profilePic, followedCount=followCount, form=form, config=config) + return render_template('twitter.html', title='Yotter | Twitter', posts=posts, avatar=avatarPath, profilePic = profilePic, followedCount=followCount, form=form, config=config, pages=pages, actual_page=page) @app.route('/savePost/', methods=['POST']) @login_required diff --git a/app/templates/twitter.html b/app/templates/twitter.html index ea5a883..9171b28 100644 --- a/app/templates/twitter.html +++ b/app/templates/twitter.html @@ -25,4 +25,19 @@ +
+ +
{% endblock %}