Login

Internal view decorator

Author:
gsakkis
Posted:
July 22, 2010
Language:
Python
Version:
1.2
Score:
3 (after 3 ratings)

The internal_view decorator makes a view accessible only from INTERNAL_IPS. Handy for exposing internal APIs.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import functools
from django.conf import settings
from django.http import HttpResponseForbidden
from django.views.decorators.csrf import csrf_exempt


def internal_view(view):
    '''Decorates a view as accessible from INTERNAL_IPS only.

    As a convenience, the view is also decorated with ``csrf_exempt``.
    '''
    @functools.wraps(view)
    def wrapper_view(request, *args, **kwds):
        remote_addr = request.META.get('HTTP_X_REAL_IP',
                                       request.META.get('REMOTE_ADDR', None))
        if not (settings.DEBUG or remote_addr in settings.INTERNAL_IPS):
            return HttpResponseForbidden('Internal view')
        return view(request, *args, **kwds)
    return csrf_exempt(wrapper_view)

More like this

  1. Django Settings Assignment Expressions aka Walrus Operator example by webology 3 weeks, 3 days ago
  2. codigo alto nivel by MrRocklion 2 months, 3 weeks ago
  3. Load template from specific app by Krzysiek555 3 months, 1 week ago
  4. PostgreSQL JSON subqueries by dolamroth 3 months, 2 weeks ago
  5. "Magic Link" Management Command by webology 8 months, 2 weeks ago

Comments

Please login first before commenting.