From 7de71477433e68d26c1642922bded66ce6f1755b Mon Sep 17 00:00:00 2001 From: Nicolas F Date: Tue, 9 Apr 2019 01:59:30 +0200 Subject: [PATCH] Query limit fixes (#555) * Fix total torrent count for user listings The total count would previously be bound by the maximum number of pages we allow. Since we run the count query anyway, we can just save this result and use it in the template. * search: allow users to view all their uploads Maximum page limitations shouldn't take effect when users are looking at a listing of their own torrents. --- nyaa/extensions.py | 10 +++++++++- nyaa/search.py | 12 ++++++++---- nyaa/templates/user.html | 4 ++-- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/nyaa/extensions.py b/nyaa/extensions.py index 8ac62ac..384a2ea 100644 --- a/nyaa/extensions.py +++ b/nyaa/extensions.py @@ -13,6 +13,12 @@ toolbar = DebugToolbarExtension() cache = Cache(config={'CACHE_TYPE': 'simple'}) +class LimitedPagination(Pagination): + def __init__(self, actual_count, *args, **kwargs): + self.actual_count = actual_count + super().__init__(*args, **kwargs) + + def fix_paginate(): def paginate_faste(self, page=1, per_page=50, max_page=None, step=5, count_query=None): @@ -27,6 +33,7 @@ def fix_paginate(): total_query_count = count_query.scalar() else: total_query_count = self.count() + actual_query_count = total_query_count if max_page: total_query_count = min(total_query_count, max_page * per_page) @@ -36,7 +43,8 @@ def fix_paginate(): if not items and page != 1: abort(404) - return Pagination(self, page, per_page, total_query_count, items) + return LimitedPagination(actual_query_count, self, page, per_page, total_query_count, + items) BaseQuery.paginate_faste = paginate_faste diff --git a/nyaa/search.py b/nyaa/search.py index aa94e0a..3756eb5 100644 --- a/nyaa/search.py +++ b/nyaa/search.py @@ -374,6 +374,14 @@ def search_db(term='', user=None, sort='id', order='desc', category='0_0', MAX_PAGES = app.config.get("MAX_PAGES", 0) + same_user = False + if logged_in_user: + same_user = logged_in_user.id == user + + # Logged in users should always be able to view their full listing. + if same_user: + MAX_PAGES = 0 + if MAX_PAGES and page > MAX_PAGES: flask.abort(flask.Response("You've exceeded the maximum number of pages. Please " "make your search query less broad.", 403)) @@ -446,10 +454,6 @@ def search_db(term='', user=None, sort='id', order='desc', category='0_0', sort_column = sort_keys['id'] order = 'desc' - same_user = False - if logged_in_user: - same_user = logged_in_user.id == user - model_class = models.TorrentNameSearch if term else models.Torrent query = db.session.query(model_class) diff --git a/nyaa/templates/user.html b/nyaa/templates/user.html index 700db70..3becb30 100644 --- a/nyaa/templates/user.html +++ b/nyaa/templates/user.html @@ -151,8 +151,8 @@

Browsing {{ user.username }}'{{ '' if user.username[-1] == 's' else 's' }} torrents - {% if torrent_query.total is number and not search.term: %} - ({{ torrent_query.total }}) + {% if torrent_query.actual_count is number and not search.term: %} + ({{ torrent_query.actual_count }}) {% endif %}