Permalink
Browse files

Fixing how custom Backends are pickedup

  • Loading branch information...
1 parent f32d643 commit eefcd7ea53c1d0e696b8bae87bdb4a7a78c93064 @gmcquillan committed Feb 1, 2013
Showing with 33 additions and 17 deletions.
  1. +5 −1 README.rst
  2. +1 −0 brake/__init__.py
  3. +14 −4 brake/decorators.py
  4. +1 −1 brake/tests/__init__.py
  5. +8 −0 brake/tests/custom_backend.py
  6. +1 −0 setup.py
  7. +2 −1 test.sh
  8. +1 −10 test_settings.py
View
@@ -160,7 +160,11 @@ and override.
# Now in your settings.py:
- RATELIMIT_CACHE_BACKEND = MyBrake
+ RATELIMIT_CACHE_BACKEND = 'path.to.module.MyBrake'
+
+
+.. note:: RATELIMIT_CACHE_BACKEND is now a string of the path to a
+ class. The class itself should be the last in the chain.
View
@@ -1 +1,2 @@
+from . import *
VERSION = (1, 2, 1)
View
@@ -4,8 +4,6 @@
from django.conf import settings
from django.http import HttpResponse
-from brake.backends.cachebe import CacheBackend
-
class HttpResponseTooManyRequests(HttpResponse):
status_code = 429
@@ -36,9 +34,21 @@ def _split_rate(rate):
return count, time
+def get_class_by_path(path):
+ mod = __import__('.'.join(path.split('.')[:-1]))
+ components = path.split('.')
+ for comp in components[1:]:
+ mod = getattr(mod, comp)
+
+ return mod
+
# Allows you to override the CacheBackend in your settings.py
-_backend_class = getattr(settings, 'RATELIMIT_CACHE_BACKEND', CacheBackend)
-_backend = _backend_class()
+_backend_class = getattr(
+ settings,
+ 'RATELIMIT_CACHE_BACKEND',
+ 'brake.backends.cachebe.CacheBackend'
+)
+_backend = get_class_by_path(_backend_class)()
def ratelimit(ip=True, block=False, method=None, field=None, rate='5/m'):
View
@@ -1 +1 @@
-from .tests import *
+
@@ -0,0 +1,8 @@
+from brake.backends import cachebe
+
+class MyBrake(cachebe.CacheBackend):
+ def get_ip(self, request):
+ return request.META.get(
+ 'HTTP_TRUE_CLIENT_IP',
+ request.META.get('REMOTE_ADDR')
+ )
View
@@ -16,6 +16,7 @@
package_data = { '': ['README.rst'] },
install_requires=[
'django',
+ 'nose',
],
classifiers=[
'Development Status :: 4 - Beta',
View
@@ -1,3 +1,4 @@
#!/bin/bash
-PYTHONPATH=.:$PYTHONPATH django-admin.py test brake --settings=test_settings
+#PYTHONPATH=.:$PYTHONPATH django-admin.py test brake --settings=test_settings
+DJANGO_SETTINGS_MODULE='test_settings' nosetests
View
@@ -19,16 +19,7 @@
}
}
-from brake.backends import cachebe
-
-class MyBrake(cachebe.CacheBackend):
- def get_ip(self, request):
- return request.META.get(
- 'HTTP_TRUE_CLIENT_IP',
- request.META.get('REMOTE_ADDR')
- )
-
-RATELIMIT_CACHE_BACKEND = MyBrake
+RATELIMIT_CACHE_BACKEND = 'brake.tests.custom_backend.MyBrake'
# point to ourselves as the root urlconf, define no patterns (see below)
ROOT_URLCONF = 'test_settings'

0 comments on commit eefcd7e

Please sign in to comment.