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.
This commit is contained in:
Nicolas F 2019-04-09 01:59:30 +02:00 committed by Arylide
parent 885cccca40
commit 7de7147743
3 changed files with 19 additions and 7 deletions

View File

@ -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

View File

@ -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)

View File

@ -151,8 +151,8 @@
<div class="row">
<h3>
Browsing <span class="text-{{ user.userlevel_color }}" data-toggle="tooltip" title="{{ user.userlevel_str }}">{{ user.username }}</span>'{{ '' 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 %}
</h3>