Skip to content

Commit

Permalink
Query limit fixes (nyaadevs#555)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
CounterPillow authored and Arylide committed Apr 8, 2019
1 parent 885cccc commit 7de7147
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
10 changes: 9 additions & 1 deletion nyaa/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
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):
Expand All @@ -27,6 +33,7 @@ def paginate_faste(self, page=1, per_page=50, max_page=None, step=5, count_query
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)

Expand All @@ -36,7 +43,8 @@ def paginate_faste(self, page=1, per_page=50, max_page=None, step=5, count_query
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

Expand Down
12 changes: 8 additions & 4 deletions nyaa/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions nyaa/templates/user.html
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ <h3 class="panel-title">Danger Zone</h3>
<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>

Expand Down

0 comments on commit 7de7147

Please sign in to comment.