Skip to content

Commit

Permalink
feat(ratelimit): Allow for custom handler429
Browse files Browse the repository at this point in the history
  • Loading branch information
pennersr committed Apr 12, 2024
1 parent 30dfb4f commit fb13bc5
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions allauth/core/ratelimit.py
Expand Up @@ -2,10 +2,13 @@
import time
from collections import namedtuple

from django.conf import settings
from django.core.cache import cache
from django.core.exceptions import ImproperlyConfigured
from django.shortcuts import render

from allauth.utils import import_callable


Rate = namedtuple("Rate", "amount duration per")

Expand Down Expand Up @@ -115,8 +118,17 @@ def _consume_rate(request, *, action, rate, key=None, user=None):
return allowed


def consume_or_429(request, *args, **kwargs):
def _handler429(request):
from allauth.account import app_settings

return render(request, "429." + app_settings.TEMPLATE_EXTENSION, status=429)


def consume_or_429(request, *args, **kwargs):
if not consume(request, *args, **kwargs):
return render(request, "429." + app_settings.TEMPLATE_EXTENSION, status=429)
try:
handler429 = import_callable(settings.ROOT_URLCONF + ".handler429")
handler429 = import_callable(handler429)
except (ImportError, AttributeError):
handler429 = _handler429
return handler429(request)

0 comments on commit fb13bc5

Please sign in to comment.