Commits

Mark Lavin committed a1679d4

Abstract the creation of lookup class decorators which wrap the results function.

Comments (0)

Files changed (1)

selectable/decorators.py

 )
 
 
-def ajax_required(lookup_cls):
+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()
 
-    func = lookup_cls.results
 
-    @wraps(func)
-    def wrapper(self, request):
-        "Wrapped results function."
-        if not request.is_ajax():
-            return HttpResponseBadRequest()
-        return func(self, request)
+@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
 
-    lookup_cls.results = wrapper
-    return lookup_cls
 
-
-def login_required(lookup_cls):
-    "Lookup decorator to require the user to be authenticated."
-
-    func = lookup_cls.results
-
-    @wraps(func)
-    def wrapper(self, request):
-        "Wrapped results function."
-        user = getattr(request, 'user', None)
-        if user is None or not user.is_authenticated():
-            return HttpResponse(status=401) # Unauthorized
-        return func(self, request)
-
-    lookup_cls.results = wrapper
-    return lookup_cls
-
-
-def staff_member_required(lookup_cls):
+@results_decorator
+def staff_member_required(request):
     "Lookup decorator to require the user is a staff member."
-    func = lookup_cls.results
-
-    @wraps(func)
-    def wrapper(self, request):
-        "Wrapped results function."
-        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()
-        return func(self, request)
-
-    lookup_cls.results = wrapper
-    return lookup_cls
+    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.