Skip to content

Commit

Permalink
add CBV support (#2)
Browse files Browse the repository at this point in the history
* add CBV support

* add cbv test

* add readme
  • Loading branch information
Roman Tomjak authored Nov 1, 2017
1 parent 6341988 commit ea2a1d9
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
.tox/
drr/
*.egg-info
__pycache__
__pycache__
build/
dist/
README
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ A fixed window rate limiting based on Redis

---

## Requirements

- Python >= 3.6
- Django >= 1.11
- Redis

## Installation

To install django-redis-ratelimit, simply:
Expand Down Expand Up @@ -53,3 +59,7 @@ For this example we will assume that each key takes up roughly 250 bytes and eac
## Notes

- [Redis Rate Limiting Pattern #2](https://redis.io/commands/INCR#pattern-rate-limiter-2)

## License

MIT
7 changes: 6 additions & 1 deletion redis_ratelimit/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import redis
from django.conf import settings
from django.http import HttpRequest

from redis_ratelimit.exceptions import RateLimited
from redis_ratelimit.utils import parse_rate, build_redis_key
Expand Down Expand Up @@ -34,7 +35,11 @@ def ratelimit(rate=None):
def decorator(f):
@wraps(f)
def decorated_function(*args, **kwargs):
request = args[0]
# CBV support
if isinstance(args[0], HttpRequest):
request = args[0]
else:
request = args[1]
if is_rate_limited(request, rate=rate):
raise RateLimited("Too Many Requests")
return f(*args, **kwargs)
Expand Down
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
here = path.abspath(path.dirname(__file__))

# Get the long description from the README file
with open(path.join(here, 'README.md'), encoding='utf-8') as f:
with open(path.join(here, 'README'), encoding='utf-8') as f:
long_description = f.read()

setup(
name='django-redis-ratelimit',
version='0.0.1',
description='A sliding window rate limiting based on Redis',
version='0.1.1',
description='A fixed window rate limiting based on Redis',
long_description=long_description,
url='https://github.com/r00m/django-redis-ratelimit',
author='Roman Tomjak',
Expand All @@ -27,7 +27,7 @@
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3.6',
],
keywords='django redis rate-limit',
keywords='django redis rate-limit ratelimit',
packages=find_packages(exclude=['contrib', 'docs', 'tests']),
install_requires=['Django >= 1.11', 'redis'],
)
19 changes: 19 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.conf.urls import url
from django.test import RequestFactory, TestCase
from django.test.utils import override_settings
from django.views import View

from redis_ratelimit import ratelimit
from redis_ratelimit.exceptions import RateLimited
Expand Down Expand Up @@ -51,3 +52,21 @@ class DynamicUrlPattern:
with self.assertRaises(RateLimited):
req = factory.get('/')
view(req)

def test_cbv_decorator(self):
class Cbv(View):
@ratelimit(rate='5/s')
def get(self, request):
return True

class DynamicUrlPattern:
urlpatterns = [url(r'', Cbv.as_view())]

with override_settings(ROOT_URLCONF=DynamicUrlPattern):
for _ in range(5):
req = factory.get('/')
Cbv.as_view()(req)

with self.assertRaises(RateLimited):
req = factory.get('/')
Cbv.as_view()(req)

0 comments on commit ea2a1d9

Please sign in to comment.