website/thaliawebsite/api/throttling.py
from rest_framework.throttling import SimpleRateThrottle
class AnonRateThrottle(SimpleRateThrottle):
"""Limits the rate of API calls that may be made by a anonymous users.
The IP address of the request will be used as the unique cache key.
"""
scope = "anon"
def get_cache_key(self, request, view):
if request.user and request.user.is_authenticated:
return None # Only throttle unauthenticated requests.
return self.cache_format % {
"scope": self.scope,
"ident": self.get_ident(request),
}
class UserRateThrottle(SimpleRateThrottle):
"""Limits the rate of API calls that may be made by a given user.
The user id will be used as a unique cache key if the user is
authenticated. For anonymous requests, the IP address of the request will
be used.
"""
scope = "user"
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}