Package: django-happy-decorators
Django Happy DecoratorsOverviewHappy decorators for Django is a set of decorators for Django apps and views to make your life easier.Decoratorsrate_limit: Rate limit a view function based on IP address, user or all requests.InstallationYou can install Django Rate Limit using pip:pip install django_happy_decoratorsRate LimitUsageTo 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.fromdjango_happy_decorators.decorators.rate_limitimportrate_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')defmy_view(request):# View logic goes hereThe 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:fromdjango_happy_decorators.decorators.rate_limitimportrate_limit@rate_limit(num_requests=10000,time_frame=1,redirect_url='/rate_limit_exceeded',mode='all')defmy_view(request):# View logic goes hereThe 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."Parametersnum_requests: the number of requests allowed per time_frametime_minutes: the time frame in which the number of requests is allowedredirect_url: the url to redirect the user to when the limit is reachedmode: 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 addressuser: limits the number of requests per userall: limits the number of requestsIn 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" DecoratorUsageTo 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:fromdjango_happy_decorators.decorators.fetch_object_or_Noneimportfetch_object_or_None@fetch_object_or_None(MyModel,'my_model_id','object')defmy_view(request,my_model_id,object):# View logic goes here# object could have a value, or could be Noneifobject:# do something"fetch_object_or_404" DecoratorUsageTo 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:fromdjango_happy_decorators.decorators.fetch_object_or_404importfetch_object_or_404@fetch_object_or_404(Article,'article_id','article')defmy_view(request,my_model_id,object):# View logic goes here# article is always has a valueifarticle:# <-- Allways True# do something"If Not" DecoratorUsageTo 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:fromdjango_happy_decorators.decorators.if_notimportif_not@if_not(parameter_name="mobile",raise_error_code=400,error_message="Mobile number is required")defsend_sms_view(request):# View logic goes hereThe 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.Parametersparameter_name: the name of the parameter to checkraise_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)ContributingWe welcome contributions to Django Rate Limit. If you want to contribute, please read our contributing guidelines for more information.LicenseDjango 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.