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'

"fetch_object_or_None" Decorator

Usage

To use the fetch_object_or_None decorator, import it from the package and apply it to a view function. The decorator accepts three parameters: model, url_kwarg, var_name.

Example:

from django_happy_decorators.decorators.fetch_object_or_None import fetch_object_or_None

@fetch_object_or_None(MyModel, 'my_model_id', 'object')
def my_view(request, my_model_id, object):
    # View logic goes here
    # object could have a value, or could be None
    if object:
        # do something

"fetch_object_or_404" Decorator

Usage

To use the fetch_object_or_404 decorator, import it from the package and apply it to a view function. The decorator accepts three parameters: model, url_kwarg, var_name.

Example:

from django_happy_decorators.decorators.fetch_object_or_404 import fetch_object_or_404

@fetch_object_or_404(Article, 'article_id', 'article')
def my_view(request, my_model_id, object):
    # View logic goes here
    # article is always has a value
    if article:  # <-- Allways True
        # do something

"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.11.tar.gz (9.4 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.11-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for django-happy-decorators-0.2.11.tar.gz
Algorithm Hash digest
SHA256 7d36686ece6c711764451b66dc8fa6e9018d9f5661d77df4230ceef4c69480de
MD5 194e650f6ef57934f17f32f0cbfce24f
BLAKE2b-256 859a5d8d59a532bea9cf00225fa9a9ec5f947db94fd43ad069982568aa35df01

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_happy_decorators-0.2.11-py3-none-any.whl
Algorithm Hash digest
SHA256 449da462e92da2523328f03691df2e3e9889ed5af71e792f380f81b78f5bec03
MD5 fbbe4626b8bbb047e5c004ce888b8ff0
BLAKE2b-256 b44b9634345472b006e019dccac75f3b357009e71d488d0273fa2498fc7bd118

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