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',
ref_object=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(e.get_message())
return cleaned_data
the TokensExceeded.get_message
function takes the Timezone to give the retry time in as optional parameter.
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.2.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | e5c9c7b8a29db9c8c5f8335310f6ad709f75ac790a8de449072b7c69fa7462f3 |
|
MD5 | 79d8d6d0ac874f0da39a0c8a1c238321 |
|
BLAKE2b-256 | daf831e5fc963838afbab56efaf2fabb23d6b5738e09c39931764ea9af5b4c46 |
Close
Hashes for django_token_bucket-0.2.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e518550cdd390caa56c5c38e485c896e2de2d600c853c6c24ce651a98b0cd6d9 |
|
MD5 | 5835ab4f50eb9bec6c51fcd8d2a2d235 |
|
BLAKE2b-256 | 7ff485bb71454e24ff2ecd279a56e56fc254690197d8a1bc8e770566130f1cab |