Permalink
Browse files

Make HTTP status code configurable in settings

  • Loading branch information...
1 parent cecf558 commit b7181ee5d1992105ed76f3f60c1cc3bbaf369735 @gmcquillan committed Feb 25, 2014
Showing with 9 additions and 4 deletions.
  1. +7 −3 brake/decorators.py
  2. +2 −1 test_settings.py
View
@@ -5,7 +5,7 @@
from django.http import HttpResponse
class HttpResponseTooManyRequests(HttpResponse):
- status_code = 429
+ status_code = getattr(settings, 'RATELIMIT_STATUS_CODE', 403)
def _method_match(request, method=None):
if method is None:
@@ -51,7 +51,9 @@ def get_class_by_path(path):
_backend = get_class_by_path(_backend_class)()
-def ratelimit(ip=True, block=False, method=None, field=None, rate='5/m', increment=None):
+def ratelimit(
+ ip=True, block=False, method=None, field=None, rate='5/m', increment=None
+):
def decorator(fn):
func_name = fn.__name__
count, period = _split_rate(rate)
@@ -75,7 +77,9 @@ def _wrapped(request, *args, **kw):
response = fn(request, *args, **kw)
if _method_match(request, method) and \
- (increment is None or (callable(increment) and increment(request, response))):
+ (increment is None or (callable(increment) and increment(
+ request, response
+ ))):
_backend.count(func_name, request, ip, field, period)
return response
View
@@ -20,6 +20,7 @@
}
RATELIMIT_CACHE_BACKEND = 'brake.tests.custom_backend.MyBrake'
+RATELIMIT_STATUS_CODE = 429 # The HTTP Response code to return.
# point to ourselves as the root urlconf, define no patterns (see below)
ROOT_URLCONF = 'test_settings'
@@ -28,6 +29,6 @@
SECRET_KEY = 'HAHAHA ratelimits!'
# turn this file into a pseudo-urls.py.
-from django.conf.urls.defaults import *
+from django.conf.urls import patterns
urlpatterns = patterns('',)

0 comments on commit b7181ee

Please sign in to comment.