add throttle to request otp APIview

This commit is contained in:
Sayyid Hamid Mahdavi 2025-07-08 17:48:15 +03:30
parent 5faac7e63d
commit 224331ef5b
3 changed files with 33 additions and 1 deletions

View file

@ -113,6 +113,13 @@ REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': PAGE_SIZE,
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
# 'DEFAULT_THROTTLE_CLASSES': [
# 'rest_framework.throttling.AnonRateThrottle',
# ],
'DEFAULT_THROTTLE_RATES': {
'otp_min': '1/min',
'otp_day': '5/day',
}
}
SPECTACULAR_SETTINGS = {

View file

@ -19,6 +19,7 @@ from apps.users.models import User
from apps.users.provinces_and_cities import State
from apps.users.serializers import PublicUserSerializer, AccountSerializer, RequestOTPSerializer, RequestOTTSerializer, \
ChangePasswordSerializer, UserInquirySerializer
from utils.throttles import RequestOTPDayRateThrottle, RequestOTPMinRateThrottle
UserModel = get_user_model()
@ -84,7 +85,7 @@ class RequestOTPView(generics.CreateAPIView):
permission_classes = []
serializer_class = RequestOTPSerializer
required_scopes = []
throttle_classes = [RequestOTPMinRateThrottle, RequestOTPDayRateThrottle]
class RequestOTTView(generics.CreateAPIView):
permission_classes = [IsAuthenticatedOrTokenHasScope]

24
utils/throttles.py Normal file
View file

@ -0,0 +1,24 @@
from rest_framework.throttling import SimpleRateThrottle
class RequestOTPDayRateThrottle(SimpleRateThrottle):
scope = 'otp_day'
def get_cache_key(self, request, view):
if request.user and request.user.is_authenticated:
ident = request.user.pk
else:
ident = self.get_ident(request)
return self.cache_format % {
'scope': self.scope,
'ident': ident
}
class RequestOTPMinRateThrottle(RequestOTPDayRateThrottle):
scope = 'otp_min'