Django Tocken Bucket support.
Project description
A token bucket implementation for Django to implement rate limiting
on individual user actions, for example submitting a form.
Installation
############
Insatall:
pip install django-token-bucket
add it to your installed apps:
INSTALLED_APPS = [
'...',
'django_token_bucket'
]
run migrations:
./manage.py migrate django_token_bucket
Examples
########
example for consuming a token on Form validation:
INVITATION_MAX_TOKENS = 5
INVITATION_FILL_RATE = 300 # a token each 300 seconds
def clean(self):
cleaned_data = super(InvitationForm, self).clean()
bucket = TokenBucket.get(identifier='invitations_sent',
user=self.user,
max_tokens=INVITATION_MAX_TOKENS,
fill_rate=INVITATION_FILL_RATE,
whatfor='invitations')
try:
bucket.consume(1)
except bucket.TokensExceeded as e:
raise forms.ValidationError(str(e))
return cleaned_data
on individual user actions, for example submitting a form.
Installation
############
Insatall:
pip install django-token-bucket
add it to your installed apps:
INSTALLED_APPS = [
'...',
'django_token_bucket'
]
run migrations:
./manage.py migrate django_token_bucket
Examples
########
example for consuming a token on Form validation:
INVITATION_MAX_TOKENS = 5
INVITATION_FILL_RATE = 300 # a token each 300 seconds
def clean(self):
cleaned_data = super(InvitationForm, self).clean()
bucket = TokenBucket.get(identifier='invitations_sent',
user=self.user,
max_tokens=INVITATION_MAX_TOKENS,
fill_rate=INVITATION_FILL_RATE,
whatfor='invitations')
try:
bucket.consume(1)
except bucket.TokensExceeded as e:
raise forms.ValidationError(str(e))
return cleaned_data
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for django_token_bucket-0.1.dev2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | d79a043801e324cfc1bba4c3b98c8379a40d9522f070f8e5d01b19a95d38f7d3 |
|
MD5 | f7f6bc17399fbd84bf3746c5e915785e |
|
BLAKE2b-256 | 802ec9315bf5c1106cd21ad31a7050d9cf8a9c2f3ed38f8b97c7b7663bddb9e9 |
Close
Hashes for django_token_bucket-0.1.dev2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 460f00747c9170ed799e97c51124da604b8a96a6c641c191111ba9bfe890bed1 |
|
MD5 | 9e4eb64f33ac20c3449c448454b2c5a6 |
|
BLAKE2b-256 | 2395270be14a438380d32693c6809141fa4c88f7fa91bb3af5ba6b04a84c12fd |