Skip to content

Commit efe5469

Browse files
Use clearer names for restricted features
1 parent 5c46e83 commit efe5469

File tree

5 files changed

+23
-23
lines changed

5 files changed

+23
-23
lines changed

api/api/constants/restricted_features.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ def choices(cls):
5151
return ((slug,) * 2 for slug in _RESTRICTED_FEATURES)
5252

5353

54-
PAGE_SIZE: RestrictedFeature[int] = RestrictedFeature(
55-
"page_size",
54+
MAX_PAGE_SIZE: RestrictedFeature[int] = RestrictedFeature(
55+
"max_page_size",
5656
anonymous=20,
5757
authenticated=50,
5858
# Max out privileged page size at the maximum authenticated
@@ -61,14 +61,14 @@ def choices(cls):
6161
privileged=240,
6262
)
6363

64-
QUERY_DEPTH: RestrictedFeature[int] = RestrictedFeature(
65-
"query_depth",
64+
MAX_RESULT_COUNT: RestrictedFeature[int] = RestrictedFeature(
65+
"max_result_count",
6666
# 12 pages of 20 results
6767
# Both anon and authed are limited to the same depth
6868
# authed users can request bigger pages, but still only the same
6969
# overall number of results available
70-
anonymous=12 * PAGE_SIZE.anonymous,
71-
authenticated=12 * PAGE_SIZE.anonymous,
70+
anonymous=12 * MAX_PAGE_SIZE.anonymous,
71+
authenticated=12 * MAX_PAGE_SIZE.anonymous,
7272
# 20 pages of maxed out page sizes for privileged apps
73-
privileged=20 * PAGE_SIZE.privileged,
73+
privileged=20 * MAX_PAGE_SIZE.privileged,
7474
)

api/api/migrations/0064_throttledapplication_privileges.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Generated by Django 4.2.11 on 2024-05-23 01:41
1+
# Generated by Django 4.2.11 on 2024-05-29 01:23
22

33
import django.contrib.postgres.fields
44
from django.db import migrations, models
@@ -14,6 +14,6 @@ class Migration(migrations.Migration):
1414
migrations.AddField(
1515
model_name='throttledapplication',
1616
name='privileges',
17-
field=django.contrib.postgres.fields.ArrayField(base_field=models.CharField(choices=[('page_size', 'page_size'), ('pagination_depth', 'pagination_depth')]), default=list, help_text='Privileges granted to applications upon approved request to Openverse maintainers. Maintainers review requests for increased privileges on a per-case basis. Distinct from ``rate_limit_model`` which only affects access rates rather than privileges.', size=None),
17+
field=django.contrib.postgres.fields.ArrayField(base_field=models.CharField(choices=[('max_page_size', 'max_page_size'), ('max_result_count', 'max_result_count')]), default=list, help_text='Restricted features to which this application has been granted privileged access', size=None),
1818
),
1919
]

api/api/serializers/media_serializers.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class PaginatedRequestSerializer(serializers.Serializer):
5858
label="page_size",
5959
help_text=f"Number of results to return per page. {_SUBJECT_TO_PAGINATION_LIMITS}",
6060
required=False,
61-
default=restricted_features.PAGE_SIZE.anonymous,
61+
default=restricted_features.MAX_PAGE_SIZE.anonymous,
6262
min_value=1,
6363
)
6464
page = serializers.IntegerField(
@@ -70,7 +70,7 @@ class PaginatedRequestSerializer(serializers.Serializer):
7070
)
7171

7272
def validate_page_size(self, value):
73-
level, max_value = restricted_features.PAGE_SIZE.request_level(
73+
level, max_value = restricted_features.MAX_PAGE_SIZE.request_level(
7474
self.context.get("request")
7575
)
7676

@@ -95,7 +95,7 @@ def validate_page_size(self, value):
9595
return value
9696

9797
def clamp_result_count(self, real_result_count):
98-
_, max_depth = restricted_features.QUERY_DEPTH.request_level(
98+
_, max_depth = restricted_features.MAX_RESULT_COUNT.request_level(
9999
self.context.get("request")
100100
)
101101

@@ -105,7 +105,7 @@ def clamp_result_count(self, real_result_count):
105105
return real_result_count
106106

107107
def clamp_page_count(self, real_page_count):
108-
_, max_depth = restricted_features.QUERY_DEPTH.request_level(
108+
_, max_depth = restricted_features.MAX_RESULT_COUNT.request_level(
109109
self.context.get("request")
110110
)
111111

@@ -122,21 +122,21 @@ def validate(self, data):
122122

123123
# pagination depth is validated as a combination of page and page size,
124124
# and so cannot be validated in the individual field validation methods
125-
level, max_depth = restricted_features.QUERY_DEPTH.request_level(
125+
level, max_depth = restricted_features.MAX_RESULT_COUNT.request_level(
126126
self.context.get("request")
127127
)
128128

129-
requested_query_depth = data["page"] * data["page_size"]
129+
requested_result_depth = data["page"] * data["page_size"]
130130

131-
query_depth_validator = MaxValueValidator(
131+
result_depth_validator = MaxValueValidator(
132132
max_depth,
133133
message=serializers.IntegerField.default_error_messages["max_value"].format(
134134
max_value=max_depth
135135
),
136136
)
137137

138138
try:
139-
query_depth_validator(requested_query_depth)
139+
result_depth_validator(requested_result_depth)
140140
except (ValidationError, DjangoValidationError) as e:
141141
if level == restricted_features.PRIVILEGED:
142142
raise

api/test/integration/test_auth.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,12 @@ def test_page_size_privileges(
300300
application = AccessToken.objects.get(token=token).application
301301

302302
if level == restricted_features.PRIVILEGED:
303-
application.privileges.append(restricted_features.PAGE_SIZE.slug)
303+
application.privileges.append(restricted_features.MAX_PAGE_SIZE.slug)
304304

305305
application.save()
306306
authorization = f"Bearer {token}"
307307

308-
limit = getattr(restricted_features.PAGE_SIZE, level)
308+
limit = getattr(restricted_features.MAX_PAGE_SIZE, level)
309309

310310
res = api_client.get(
311311
"/v1/images/",
@@ -346,14 +346,14 @@ def test_pagination_depth_privileges(
346346
application = AccessToken.objects.get(token=token).application
347347

348348
if level == restricted_features.PRIVILEGED:
349-
application.privileges.append(restricted_features.QUERY_DEPTH.slug)
349+
application.privileges.append(restricted_features.MAX_RESULT_COUNT.slug)
350350

351351
application.save()
352352
authorization = f"Bearer {token}"
353353

354-
depth_limit = getattr(restricted_features.QUERY_DEPTH, level)
354+
depth_limit = getattr(restricted_features.MAX_RESULT_COUNT, level)
355355

356-
page_size_limit = restricted_features.PAGE_SIZE.anonymous
356+
page_size_limit = restricted_features.MAX_PAGE_SIZE.anonymous
357357

358358
last_page = int(depth_limit / page_size_limit)
359359

api/test/integration/test_dead_link_filter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def test_page_consistency_removing_dead_links(search_without_dead_links):
179179
"""Test that results in consecutive pages don't repeat when filtering dead links."""
180180

181181
page_size = 5
182-
total_pages = int(restricted_features.QUERY_DEPTH.anonymous / page_size)
182+
total_pages = int(restricted_features.MAX_RESULT_COUNT.anonymous / page_size)
183183

184184
page_results = []
185185
for page in range(1, total_pages + 1):

0 commit comments

Comments
 (0)