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.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | c524713c7f9f773724b2bb389940c4fe26176a69b4855a9eac3076f96df1dcf9 |
|
MD5 | 73a25577314582205d2bcbfd1c68b454 |
|
BLAKE2b-256 | 2677bc878d82dc91ccf5211f7c1e48be877f1e6b7525466a2a09d8dd11881fa0 |
Close
Hashes for django_token_bucket-0.2.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b9e99f819927c28d753e66a6f3b5794b1d6f41a1050794690baaed50ca64f727 |
|
MD5 | d0520aebc85ad704dbf35620e7975d41 |
|
BLAKE2b-256 | e1c63c197a53577cb2bb3ebdbc51ae4b51e18dbce31633a2947cecbbf08e8e7d |