Skip to main content

Happy decorators for Django is a set of decorators for Django apps and views to make your life easier.

Project description

Django Happy Decorators

Build Status Coverage Status PyPI version Documentation Status License: MIT

Overview

Happy decorators for Django is a set of decorators for Django apps and views to make your life easier.

Decorators

  • rate_limit: Rate limit a view function based on IP address, user or all requests.

Installation

You can install Django Rate Limit using pip:

pip install django_happy_decorators

Rate Limit

Usage

To use the rate_limit decorator, import it from the package and apply it to a view function. The decorator accepts three parameters: num_requests, time_frame and redirect_url.

from django_happy_decorators.decorators.rate_limit import rate_limit

# Only add this line 👇 to limit the number of requests to the view
@rate_limit(num_requests=100, time_frame=60, redirect_url='/rate_limit_exceeded', mode='ip')
def my_view(request):
    # View logic goes here

The previous example will limit the number of requests to the view to 100 per hour. If the limit is reached, the user will be redirected to '/rate_limit_exceeded'.

Other Example:

from django_happy_decorators.decorators.rate_limit import rate_limit


@rate_limit(num_requests=10000, time_frame=1, redirect_url='/rate_limit_exceeded', mode='all')
def my_view(request):
    # View logic goes here

The previous example will limit the number of requests to the view to 10k/min (10000 requests per minute). If the limit is reached, the user will be redirected to '/rate_limit_exceeded'. This could be useful if you know your server limits and want to prevent it from crashing."

Parameters

  • num_requests: the number of requests allowed per time_frame
  • time_minutes: the time frame in which the number of requests is allowed
  • redirect_url: the url to redirect the user to when the limit is reached
  • mode: the mode to use to limit the number of requests. It can be one of the following values: ip, user or all. Default is 'all'.
  • error_message: the error message to return with the response when the limit is reached. Default is 'You have exceeded the maximum number of requests allowed.' Note that this apply only if redirect_url is not set.

Modes:

  • ip: limits the number of requests per IP address
  • user: limits the number of requests per user
  • all: limits the number of requests

In the example above, the view will only allow 100 requests per hour and when the limit is reached it will redirect the user to '/rate_limit_exceeded'

"If Not" Decorator

Usage

To use the if_not decorator, import it from the package and apply it to a view function. The decorator accepts three parameters: condition, redirect_url and error_message.

Example:

from django_happy_decorators.decorators.if_not import if_not

@if_not(parameter_name="mobile", raise_error_code=400, error_message="Mobile number is required")
def send_sms_view(request):
    # View logic goes here

The previous example will check if the request has a parameter called 'mobile'. If it doesn't, it will raise an error with the message 'Mobile number is required' and the status code 400.

Parameters

  • parameter_name: the name of the parameter to check
  • raise_error_code: the status code to return with the response when the condition is not met. Default is 400.
  • error_message: the error message to return with the response when the condition is not met. Default is 'The parameter {parameter_name} is required.'

Request Types:

The decorator can be used with these types of requests (GET, POST, PUT, DELETE). It will check the request data based on the request type.

  • GET: it will check the query parameters (request.GET)
  • POST: it will check the form data (request.POST)
  • PUT: it will check the form data (request.POST)
  • DELETE: it will check the query parameters (request.GET)

Contributing

We welcome contributions to Django Rate Limit. If you want to contribute, please read our contributing guidelines for more information.

License

Django Rate Limit is released under the MIT License.

This is just a sample and you can adjust the content as per your package and also add more details like screenshots, sample code etc.

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-happy-decorators-0.2.7.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_happy_decorators-0.2.7-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file django-happy-decorators-0.2.7.tar.gz.

File metadata

  • Download URL: django-happy-decorators-0.2.7.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.9

File hashes

Hashes for django-happy-decorators-0.2.7.tar.gz
Algorithm Hash digest
SHA256 486ceefb8d76db1040d69a4283bc8f6489b00ed771ef8360830e7baf1f8d13a6
MD5 664a2e0fb387d648e48de937b134b05b
BLAKE2b-256 6d6c4817a444afa8960390e9a65bbd7ec6030bfa66562654b15eb3921c44b31a

See more details on using hashes here.

File details

Details for the file django_happy_decorators-0.2.7-py3-none-any.whl.

File metadata

File hashes

Hashes for django_happy_decorators-0.2.7-py3-none-any.whl
Algorithm Hash digest
SHA256 3e727df04cfb0a571f1f2c70f9b11098918437a221d2ee14161c979f0d20cff1
MD5 09a19c38900e73a8fad6b7f22331ddb8
BLAKE2b-256 c6957c4009e3277527f57eb9bab406e724ad39559465bccf42a799970710a5b4

See more details on using hashes here.

Supported by

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