-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add page size and pagination depth privileges
- Loading branch information
1 parent
010b494
commit 53b2423
Showing
20 changed files
with
499 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import typing | ||
from dataclasses import dataclass | ||
|
||
from rest_framework.request import Request | ||
|
||
|
||
ANONYMOUS: typing.Literal["anonymous"] = "anonymous" | ||
AUTHENTICATED: typing.Literal["authenticated"] = "authenticated" | ||
PRIVILEGED: typing.Literal["privileged"] = "privileged" | ||
|
||
Level = typing.Literal[ANONYMOUS, AUTHENTICATED, PRIVILEGED] | ||
LEVELS = typing.get_args(Level) | ||
|
||
|
||
_PRIVILEGES = {} | ||
|
||
|
||
@dataclass | ||
class Privilege: | ||
""" | ||
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. | ||
""" | ||
|
||
slug: str | ||
anonymous: typing.Any | ||
authenticated: typing.Any | ||
privileged: typing.Any | ||
|
||
def __post_init__(self): | ||
_PRIVILEGES[self.slug] = self | ||
|
||
def request_level(self, request: None | Request) -> tuple[Level, typing.Any]: | ||
"""Retrieve the level of any request in relation to the privilege.""" | ||
if request is None or request.auth is None: | ||
return ANONYMOUS, self.anonymous | ||
|
||
if self.slug in request.auth.application.privileges: | ||
return PRIVILEGED, self.privileged | ||
|
||
return AUTHENTICATED, self.authenticated | ||
|
||
@classmethod | ||
@property | ||
def choices(cls): | ||
return ((slug,) * 2 for slug in _PRIVILEGES) | ||
|
||
|
||
PAGE_SIZE = Privilege( | ||
"page_size", | ||
anonymous=20, | ||
authenticated=50, | ||
# Max out privileged page size at the maximum authenticated | ||
# pagination depth, otherwise privileged page size limit can | ||
# contradict pagination depth limit. | ||
privileged=240, | ||
) | ||
|
||
PAGINATION_DEPTH = Privilege( | ||
"pagination_depth", | ||
# 12 pages of 20 results | ||
# Both anon and authed are limited to the same depth | ||
# authed users can request bigger pages, but still only the same | ||
# overall number of results available | ||
anonymous=12 * PAGE_SIZE.anonymous, | ||
authenticated=12 * PAGE_SIZE.anonymous, | ||
# 20 pages of maxed out page sizes for privileged apps | ||
privileged=20 * PAGE_SIZE.privileged, | ||
) |
19 changes: 19 additions & 0 deletions
19
api/api/migrations/0064_throttledapplication_privileges.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Generated by Django 4.2.11 on 2024-05-23 01:41 | ||
|
||
import django.contrib.postgres.fields | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('api', '0063_merge_20240521_0843'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='throttledapplication', | ||
name='privileges', | ||
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), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.