Skip to content

Commit 55bdbad

Browse files
committed
views: Limit max number of items per page to 1000
Allowing the users to set an arbitrary number of items per page may cause excessive load on the database. For example, a request to the patch list has to compute the number of tags for each listed patch.
1 parent 6aa0712 commit 55bdbad

3 files changed

Lines changed: 8 additions & 1 deletion

File tree

‎patchwork/models.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from django.contrib.auth.models import User
1414
from django.core.exceptions import ValidationError
1515
from django.core.validators import validate_unicode_slug
16+
from django.core.validators import MaxValueValidator
1617
from django.db import models
1718
from django.urls import reverse
1819
from django.utils.functional import cached_property
@@ -172,6 +173,7 @@ class UserProfile(models.Model):
172173
null=False,
173174
blank=False,
174175
help_text='Number of items to display per page',
176+
validators=[MaxValueValidator(settings.MAX_ITEMS_PER_PAGE)],
175177
)
176178
show_ids = models.BooleanField(
177179
default=False,
@@ -208,6 +210,9 @@ def token(self):
208210
except Token.DoesNotExist:
209211
return
210212

213+
def get_items_per_page(self):
214+
return min(self.items_per_page, settings.MAX_ITEMS_PER_PAGE)
215+
211216
def todo_patches(self, project=None):
212217
# filter on project, if necessary
213218
if project:

‎patchwork/paginator.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def __init__(self, request, objects):
2525
items_per_page = settings.DEFAULT_ITEMS_PER_PAGE
2626

2727
if request.user.is_authenticated:
28-
items_per_page = request.user.profile.items_per_page
28+
items_per_page = request.user.profile.get_items_per_page()
2929

3030
super().__init__(objects, items_per_page)
3131

‎patchwork/settings/base.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@
266266
REST_RESULTS_PER_PAGE = 30
267267
MAX_REST_RESULTS_PER_PAGE = 250
268268

269+
MAX_ITEMS_PER_PAGE = 1000
270+
269271
# Set to True to enable redirections or URLs from previous versions
270272
# of patchwork
271273
COMPAT_REDIR = True

0 commit comments

Comments
 (0)