Mark Lavin avatar Mark Lavin committed 15a360d

Reorganize lookup decorators and tests into their own module.

Comments (0)

Files changed (5)

selectable/base.py

-import functools
+"Base classes for lookup creation."
+
 import operator
 import re
 
-from django import http
 from django.conf import settings
 from django.core.paginator import Paginator, InvalidPage, EmptyPage
 from django.core.urlresolvers import reverse
 from django.core.serializers.json import DjangoJSONEncoder
+from django.http import HttpResponse
 from django.db.models import Q
 from django.utils import simplejson as json
 from django.utils.encoding import smart_unicode
 __all__ = (
     'LookupBase',
     'ModelLookup',
-    'ajax_required',
-    'login_required',
-    'staff_member_required',
 )
 
 
                     'page': page_data.next_page_number()
                 })        
         content = json.dumps(data, cls=DjangoJSONEncoder, ensure_ascii=False)
-        return http.HttpResponse(content, content_type='application/json')    
+        return HttpResponse(content, content_type='application/json')    
 
 
 class ModelLookup(LookupBase):
             if field_name:
                 data = {field_name: value}
         return self.model(**data)
-
-
-def ajax_required(lookup_cls):
-    "Lookup decorator to require AJAX calls to the lookup view."
-
-    func = lookup_cls.results
-
-    @functools.wraps(func)
-    def wrapper(self, request):
-        "Wrapped results function."
-        if not request.is_ajax():
-            return http.HttpResponseBadRequest()
-        return func(self, request)
-
-    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
-
-    @functools.wraps(func)
-    def wrapper(self, request):
-        "Wrapped results function."
-        user = getattr(request, 'user', None)
-        if user is None or not user.is_authenticated():
-            return http.HttpResponse(status=401) # Unauthorized
-        return func(self, request)
-
-    lookup_cls.results = wrapper
-    return lookup_cls
-
-
-def staff_member_required(lookup_cls):
-    "Lookup decorator to require the user is a staff member."
-    func = lookup_cls.results
-
-    @functools.wraps(func)
-    def wrapper(self, request):
-        "Wrapped results function."
-        user = getattr(request, 'user', None)
-        if user is None or not user.is_authenticated():
-            return http.HttpResponse(status=401) # Unauthorized
-        elif not user.is_staff:
-            return http.HttpResponseForbidden()
-        return func(self, request)
-
-    lookup_cls.results = wrapper
-    return lookup_cls

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 ajax_required(lookup_cls):
+    "Lookup decorator to require AJAX calls to the lookup view."
+
+    func = lookup_cls.results
+
+    @wraps(func)
+    def wrapper(self, request):
+        "Wrapped results function."
+        if not request.is_ajax():
+            return HttpResponseBadRequest()
+        return func(self, request)
+
+    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):
+    "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

selectable/tests/__init__.py

 
 
 from selectable.tests.base import *
+from selectable.tests.decorators import *
 from selectable.tests.fields import *
 from selectable.tests.functests import *
 from selectable.tests.forms import *

selectable/tests/base.py

 from django.core.urlresolvers import reverse
 from django.test import TestCase
 
-from mock import Mock
 from selectable.base import ModelLookup
-from selectable.base import ajax_required, login_required, staff_member_required
 from selectable.tests import Thing
 
 __all__ = (
     'ModelLookupTestCase',
     'MultiFieldLookupTestCase',
-    'AjaxRequiredLookupTestCase',
-    'LoginRequiredLookupTestCase',
-    'StaffRequiredLookupTestCase',
 )
 
 
         qs = lookup.get_query(request=None, term='other')
         self.assertTrue(thing.pk not in qs.values_list('id', flat=True))
         self.assertTrue(other_thing.pk in qs.values_list('id', flat=True))
-
-
-class AjaxRequiredLookupTestCase(BaseSelectableTestCase):
-
-    def setUp(self):
-        self.lookup = ajax_required(SimpleModelLookup)()
-
-    def test_ajax_call(self):
-        "Ajax call should yield a successful response."
-        request = Mock()
-        request.is_ajax = lambda: True
-        response = self.lookup.results(request)
-        self.assertTrue(response.status_code, 200)
-
-    def test_non_ajax_call(self):
-        "Non-Ajax call should yield a bad request response."
-        request = Mock()
-        request.is_ajax = lambda: False
-        response = self.lookup.results(request)
-        self.assertEqual(response.status_code, 400)
-
-
-class LoginRequiredLookupTestCase(BaseSelectableTestCase):
-
-    def setUp(self):
-        self.lookup = login_required(SimpleModelLookup)()
-    
-    def test_authenicated_call(self):
-        "Authenicated call should yield a successful response."
-        request = Mock()
-        user = Mock()
-        user.is_authenticated = lambda: True
-        request.user = user
-        response = self.lookup.results(request)
-        self.assertTrue(response.status_code, 200)
-
-    def test_non_authenicated_call(self):
-        "Non-Authenicated call should yield an unauthorized response."
-        request = Mock()
-        user = Mock()
-        user.is_authenticated = lambda: False
-        request.user = user
-        response = self.lookup.results(request)
-        self.assertEqual(response.status_code, 401)
-
-
-class StaffRequiredLookupTestCase(BaseSelectableTestCase):
-
-    def setUp(self):
-        self.lookup = staff_member_required(SimpleModelLookup)()
-
-    def test_staff_member_call(self):
-        "Staff member call should yield a successful response."
-        request = Mock()
-        user = Mock()
-        user.is_authenticated = lambda: True
-        user.is_staff = True
-        request.user = user
-        response = self.lookup.results(request)
-        self.assertTrue(response.status_code, 200)
-
-    def test_authenicated_but_not_staff(self):
-        "Authenicated but non staff call should yield a forbidden response."
-        request = Mock()
-        user = Mock()
-        user.is_authenticated = lambda: True
-        user.is_staff = False
-        request.user = user
-        response = self.lookup.results(request)
-        self.assertTrue(response.status_code, 403)
-
-    def test_non_authenicated_call(self):
-        "Non-Authenicated call should yield an unauthorized response."
-        request = Mock()
-        user = Mock()
-        user.is_authenticated = lambda: False
-        user.is_staff = False
-        request.user = user
-        response = self.lookup.results(request)
-        self.assertEqual(response.status_code, 401)

selectable/tests/decorators.py

+from mock import Mock
+
+from selectable.decorators import ajax_required, login_required, staff_member_required
+from selectable.tests.base import BaseSelectableTestCase, SimpleModelLookup
+
+
+__all__ = (
+    'AjaxRequiredLookupTestCase',
+    'LoginRequiredLookupTestCase',
+    'StaffRequiredLookupTestCase',
+)
+
+
+class AjaxRequiredLookupTestCase(BaseSelectableTestCase):
+
+    def setUp(self):
+        self.lookup = ajax_required(SimpleModelLookup)()
+
+    def test_ajax_call(self):
+        "Ajax call should yield a successful response."
+        request = Mock()
+        request.is_ajax = lambda: True
+        response = self.lookup.results(request)
+        self.assertTrue(response.status_code, 200)
+
+    def test_non_ajax_call(self):
+        "Non-Ajax call should yield a bad request response."
+        request = Mock()
+        request.is_ajax = lambda: False
+        response = self.lookup.results(request)
+        self.assertEqual(response.status_code, 400)
+
+
+class LoginRequiredLookupTestCase(BaseSelectableTestCase):
+
+    def setUp(self):
+        self.lookup = login_required(SimpleModelLookup)()
+    
+    def test_authenicated_call(self):
+        "Authenicated call should yield a successful response."
+        request = Mock()
+        user = Mock()
+        user.is_authenticated = lambda: True
+        request.user = user
+        response = self.lookup.results(request)
+        self.assertTrue(response.status_code, 200)
+
+    def test_non_authenicated_call(self):
+        "Non-Authenicated call should yield an unauthorized response."
+        request = Mock()
+        user = Mock()
+        user.is_authenticated = lambda: False
+        request.user = user
+        response = self.lookup.results(request)
+        self.assertEqual(response.status_code, 401)
+
+
+class StaffRequiredLookupTestCase(BaseSelectableTestCase):
+
+    def setUp(self):
+        self.lookup = staff_member_required(SimpleModelLookup)()
+
+    def test_staff_member_call(self):
+        "Staff member call should yield a successful response."
+        request = Mock()
+        user = Mock()
+        user.is_authenticated = lambda: True
+        user.is_staff = True
+        request.user = user
+        response = self.lookup.results(request)
+        self.assertTrue(response.status_code, 200)
+
+    def test_authenicated_but_not_staff(self):
+        "Authenicated but non staff call should yield a forbidden response."
+        request = Mock()
+        user = Mock()
+        user.is_authenticated = lambda: True
+        user.is_staff = False
+        request.user = user
+        response = self.lookup.results(request)
+        self.assertTrue(response.status_code, 403)
+
+    def test_non_authenicated_call(self):
+        "Non-Authenicated call should yield an unauthorized response."
+        request = Mock()
+        user = Mock()
+        user.is_authenticated = lambda: False
+        user.is_staff = False
+        request.user = user
+        response = self.lookup.results(request)
+        self.assertEqual(response.status_code, 401)
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.