Skip to content

Commit d2e197c

Browse files
fstachurastephenfin
authored andcommitted
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 1d5320b commit d2e197c

4 files changed

Lines changed: 30 additions & 1 deletion

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Generated by Django 6.1.2 on 2026-10-16 08:21
2+
3+
import django.core.validators
4+
from django.db import migrations, models
5+
6+
7+
class Migration(migrations.Migration):
8+
dependencies = [
9+
("patchwork", "0049_patch_patch_covering_idx_default"),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name="userprofile",
15+
name="items_per_page",
16+
field=models.PositiveIntegerField(
17+
default=100,
18+
help_text="Number of items to display per page",
19+
validators=[django.core.validators.MaxValueValidator(1000)],
20+
),
21+
),
22+
]

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