Skip to main content

A collection of decorator mixins for Django views.

Project description

A collection of class-based view mixins for Django. Django-Decoratormixins also includes a function for converting view decorators into class-based view mixins.

Usage

If you have a decorator, usage is as simple as calling DecoratorMixin and passing the decorator as the argument.

from decoratormixins import DecoratorMixin

def my_decorator(f):
    # ...

MyDecMixin = DecoratorMixin(my_decorator)

Decorator Factories

Many of Django’s built in decorators are actually decorator factories. In this case, it is necessary to get a decorator out of them before it is possible to create a mixin from it.

Here we have a decorator factory that modifies a view to require a certain parameter in a get request:

def require_get_param(param):
    def actual_decorator(view_func):
        @wraps(view_func)
        def _wrapped_view_func(request, *args, **kwargs):
            if request.GET.get(param, None) is not None:
                response = view_func(request, *args, **kwargs)
                response.content += param
                return response
            else:
                return HttpResponseNotFound("{} is not present.".format(param))

        return _wrapped_view_func
    return actual_decorator

In order to use it, we must specify the parameter we are requiring:

require_foo_decorator = require_get_param('foo')

And now we can call DecoratorMixin on that decorator:

RequireFooMixin = DecoratorMixin(require_foo_decorator)

Applying Mixins

Once you have the mixin you reqire, mix it in with a class-based view.

class TestView(View):
    def get(self, request):
        return HttpResponse("OK")

class TestViewFoo(RequireFooMixin, TestView):
    pass

TestViewFoo.as_view() will return a view method that is usable in your urls.py.

Composing Mixins

It is possible to use multiple mixins for a single class, but order matters. The leftmost mixin in a class definition will be the outermost decorator.

from decoratormixins.auth import LoginRequiredMixin
from decoratormixins.http import RequireGetMixin

# TestView from above

class LoggedInGetRequestView(LoginRequiredMixin,
                             RequireGetMixin,
                             TestView):
    pass

Included Mixins

Here is a list of all of the included mixins, and the modules in which they can be found.

  • decoratormixins.auth

    • LoginRequiredMixin

  • decoratormixins.csrf

    • CsrfProtectMixin

    • EnsureCsrfCookieMixin

    • CsrfExemptMixin

  • decoratormixins.http

    • ConditionalPageMixin

    • RequireGetMixin

    • RequirePostMixin

    • RequireSafeMixin

    • EtagMixin

    • LastModifiedMixin

  • decoratormixins.cache

    • NeverCacheMixin

  • decoratormixins.clickjacking

    • XFrameOptionsDenyMixin

    • XFrameOptionsDenySameoriginMixin

    • XFrameOptionsDenyExemptMixin

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-decoratormixins-1.0.0.tar.gz (4.0 kB view details)

Uploaded Source

File details

Details for the file django-decoratormixins-1.0.0.tar.gz.

File metadata

File hashes

Hashes for django-decoratormixins-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e2bfd247abed08b0e2245da362a0b43b7149ca2f132da8231019416bf9bb59bf
MD5 63faf8ff70469c2f678ee6c84ecee3ca
BLAKE2b-256 0923e379e4b9ee77fcf8f97e27c0a469189bf807e08d850623c086ccfbb68af4

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