Source

django-selectable / selectable / decorators.py

"Decorators for additional lookup functionality."

from functools import wraps

from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseForbidden


__all__ = (
    'ajax_required',
    'login_required',
    'staff_member_required',
)


def results_decorator(func):
    """
    Helper for constructing simple decorators around Lookup.results.

    func is a function which takes a request as the first parameter. If func
    returns an HttpReponse it is returned otherwise the original Lookup.results
    is returned.
    """
    # Wrap function to maintian the original doc string, etc
    @wraps(func)
    def decorator(lookup_cls):
        # Construct a class decorator from the original function
        original = lookup_cls.results
        def inner(self, request):
            # Wrap lookup_cls.results by first calling func and checking the result
            result = func(request)
            if isinstance(result, HttpResponse):
                return result
            return original(self, request)
        # Replace original lookup_cls.results with wrapped version
        lookup_cls.results = inner
        return lookup_cls
    # Return the constructed decorator
    return decorator


@results_decorator
def ajax_required(request):    
    "Lookup decorator to require AJAX calls to the lookup view."
    if not request.is_ajax():
        return HttpResponseBadRequest()


@results_decorator
def login_required(request):
    "Lookup decorator to require the user to be authenticated."
    user = getattr(request, 'user', None)
    if user is None or not user.is_authenticated():
        return HttpResponse(status=401) # Unauthorized


@results_decorator
def staff_member_required(request):
    "Lookup decorator to require the user is a staff member."
    user = getattr(request, 'user', None)
    if user is None or not user.is_authenticated():
        return HttpResponse(status=401) # Unauthorized
    elif not user.is_staff:
        return HttpResponseForbidden()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.