Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_token_bucket-0.2.4.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

django_token_bucket-0.2.4-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file django_token_bucket-0.2.4.tar.gz.

File metadata

  • Download URL: django_token_bucket-0.2.4.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.4

File hashes

Hashes for django_token_bucket-0.2.4.tar.gz
Algorithm Hash digest
SHA256 8648f8568d8e5c8d0847f4fafeacede593cf2c2149367356c849bd8647abbc24
MD5 2621475964fbef4ae9a9db0cd8ef8d82
BLAKE2b-256 6838b940b6660f89efa969cce6ef5a43ea96cfa92ba57baf97a11c8dda87e413

See more details on using hashes here.

File details

Details for the file django_token_bucket-0.2.4-py3-none-any.whl.

File metadata

File hashes

Hashes for django_token_bucket-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 0e51fc913cae3fa67a2ed441e6e2095c043edc8348b1987ba4c39cdf35c8bc26
MD5 953d3aa478b5d4aa2d4466b77663b5f5
BLAKE2b-256 203847c84a291c32213f1348e20cf9e1a3aa1e71b373a0618097b9a4b2cce4ab

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page