Metadata-Version: 2.1
Name: django-token-bucket
Version: 0.2.4
Summary: Django Tocken Bucket support.
Home-page: https://github.com/fsinfuhh/django_token_bucket
Author: Nils Rokita, Henning Pridöhl
Author-email: github@rokita.it
Maintainer: Nils Rokita
Maintainer-email: github@rokita.it
License: License :: OSI Approved :: MIT License
Platform: UNKNOWN
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Web Environment
Classifier: Programming Language :: Python
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.4
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: Implementation :: PyPy
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Framework :: Django
Description-Content-Type: text/markdown
License-File: LICENSE

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.



