Skip to main content

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.

Metadata

Release files for django-token-bucket 0.2.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-token-bucket 0.2.4
File Size Uploaded Provenance Actions
django_token_bucket-0.2.4.tar.gz 6.3 kB Without provenance Details for django_token_bucket-0.2.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-token-bucket 0.2.4
File Interpreter ABI Platform Actions
django_token_bucket-0.2.4-py3-none-any.whl Python 3 none any Details for django_token_bucket-0.2.4-py3-none-any.whl

Total release size: 14.1 kB

Release files / django_token_bucket-0.2.4.tar.gz

Download URL django_token_bucket-0.2.4.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
8648f8568d8e5c8d0847f4fafeacede593cf2c2149367356c849bd8647abbc24
BLAKE2b-256 checksum
How to use checksums
6838b940b6660f89efa969cce6ef5a43ea96cfa92ba57baf97a11c8dda87e413
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.4

Release files / django_token_bucket-0.2.4-py3-none-any.whl

Download URL django_token_bucket-0.2.4-py3-none-any.whl
Size 7.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0e51fc913cae3fa67a2ed441e6e2095c043edc8348b1987ba4c39cdf35c8bc26
BLAKE2b-256 checksum
How to use checksums
203847c84a291c32213f1348e20cf9e1a3aa1e71b373a0618097b9a4b2cce4ab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.4

Security for django-token-bucket 0.2.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page