From 23de7e52bcbc4f0b35ddf98e2059b87d1347813a Mon Sep 17 00:00:00 2001 From: lebaudantoine Date: Mon, 9 Feb 2026 14:44:39 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F(backend)=20extract=20throttl?= =?UTF-8?q?ing=20classes=20into=20a=20module?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Extract throttling classes into a dedicated Python module, following the structure of suitenumerique/docs. This is a preparatory refactor to ease upcoming changes to the throttling implementation. No functional behavior change is introduced in this commit. --- src/backend/core/api/throttling.py | 14 ++++++++++++++ src/backend/core/api/viewsets.py | 20 ++++---------------- 2 files changed, 18 insertions(+), 16 deletions(-) create mode 100644 src/backend/core/api/throttling.py diff --git a/src/backend/core/api/throttling.py b/src/backend/core/api/throttling.py new file mode 100644 index 00000000..2738bfc8 --- /dev/null +++ b/src/backend/core/api/throttling.py @@ -0,0 +1,14 @@ +"""Throttling modules for the API.""" + +from rest_framework import throttling + +class RequestEntryAnonRateThrottle(throttling.AnonRateThrottle): + """Throttle Anonymous user requesting room entry""" + + scope = "request_entry" + + +class CreationCallbackAnonRateThrottle(throttling.AnonRateThrottle): + """Throttle Anonymous user requesting room generation callback""" + + scope = "creation_callback" diff --git a/src/backend/core/api/viewsets.py b/src/backend/core/api/viewsets.py index 0d1349ed..15159770 100644 --- a/src/backend/core/api/viewsets.py +++ b/src/backend/core/api/viewsets.py @@ -10,7 +10,7 @@ from django.http import Http404 from django.shortcuts import get_object_or_404 from django.utils.text import slugify -from rest_framework import decorators, mixins, pagination, throttling, viewsets +from rest_framework import decorators, mixins, pagination, viewsets from rest_framework import ( exceptions as drf_exceptions, ) @@ -58,7 +58,7 @@ from core.services.room_creation import RoomCreation from core.services.subtitle import SubtitleException, SubtitleService from ..authentication.livekit import LiveKitTokenAuthentication -from . import permissions, serializers +from . import permissions, serializers, throttling from .feature_flag import FeatureFlag # pylint: disable=too-many-ancestors @@ -191,18 +191,6 @@ class UserViewSet( ) -class RequestEntryAnonRateThrottle(throttling.AnonRateThrottle): - """Throttle Anonymous user requesting room entry""" - - scope = "request_entry" - - -class CreationCallbackAnonRateThrottle(throttling.AnonRateThrottle): - """Throttle Anonymous user requesting room generation callback""" - - scope = "creation_callback" - - class RoomViewSet( mixins.CreateModelMixin, mixins.DestroyModelMixin, @@ -379,7 +367,7 @@ class RoomViewSet( methods=["post"], url_path="request-entry", permission_classes=[], - throttle_classes=[RequestEntryAnonRateThrottle], + throttle_classes=[throttling.RequestEntryAnonRateThrottle], ) def request_entry(self, request, pk=None): # pylint: disable=unused-argument """Request entry to a room""" @@ -489,7 +477,7 @@ class RoomViewSet( methods=["post"], url_path="creation-callback", permission_classes=[], - throttle_classes=[CreationCallbackAnonRateThrottle], + throttle_classes=[throttling.CreationCallbackAnonRateThrottle], ) def creation_callback(self, request): """Retrieve cached room data via an unauthenticated request with a unique ID.