Commits

Chris Vigelius  committed c92635e

fix docstring for search_users view
fix pep8 E501, W291, W293

  • Participants
  • Parent commits 8fe4287

Comments (0)

Files changed (3)

     # in your view
     <a href="/impersonate/list/?next=/some/url/">switch user</a>
 
-To return always to the current page after impersonating a user, use 
+To return always to the current page after impersonating a user, use request.path: 
 
     <a href="/impersonate/list/?next={{request.path}}">switch user</a>
 

File impersonate/helpers.py

 from django.core.paginator import Paginator, EmptyPage
 from django.utils.safestring import mark_safe
 
- 
+
 def get_redir_path(request=None):
     next = None
-    redirect_field_name = getattr(settings,'IMPERSONATE_REDIRECT_FIELD_NAME', None)
+    redirect_field_name = getattr(settings,
+                                  'IMPERSONATE_REDIRECT_FIELD_NAME', None)
     if request and redirect_field_name:
         next = request.GET.get(redirect_field_name, None)
     return next or getattr(
         settings,
         'IMPERSONATE_REDIRECT_URL',
         getattr(settings, 'LOGIN_REDIRECT_URL', '/'),
-    )       
+    )
+
 
 def get_redir_arg(request):
-    redirect_field_name = getattr(settings,'IMPERSONATE_REDIRECT_FIELD_NAME', None)
+    redirect_field_name = getattr(settings,
+                                  'IMPERSONATE_REDIRECT_FIELD_NAME', None)
     if redirect_field_name:
         next = request.GET.get(redirect_field_name, None)
         if next:
             return '?%s=%s' % (redirect_field_name, next)
-    return '' 
+    return ''
+
 
 def get_redir_field(request):
-    redirect_field_name = getattr(settings,'IMPERSONATE_REDIRECT_FIELD_NAME', None)
+    redirect_field_name = getattr(settings,
+                                  'IMPERSONATE_REDIRECT_FIELD_NAME', None)
     if redirect_field_name:
         next = request.GET.get(redirect_field_name, None)
         if next:
-            return mark_safe('<input type="hidden" name="%s" value="%s"/>' % (redirect_field_name,next))
-    return '' 
-    
+            return mark_safe('<input type="hidden" name="%s" value="%s"/>' %
+                             (redirect_field_name, next))
+    return ''
+
+
 def get_paginator(request, qs):
     try:
         page_number = int(request.GET.get('page', 1))
     paginator = Paginator(
         qs,
         int(getattr(settings, 'IMPERSONATE_PAGINATE_COUNT', 20)),
-    )   
+    )
     try:
         page = paginator.page(page_number)
     except EmptyPage:
 
 
 def users_impersonable(request):
-    ''' Returns a QuerySet of users that this user can impersonate. 
-        Uses the IMPERSONATE_CUSTOM_USER_QUERYSET if set, else, it 
+    ''' Returns a QuerySet of users that this user can impersonate.
+        Uses the IMPERSONATE_CUSTOM_USER_QUERYSET if set, else, it
         returns all users
     '''
     if hasattr(settings, 'IMPERSONATE_CUSTOM_USER_QUERYSET'):
     '''
     if check_allow_impersonate(request):
         # start user can impersonate
-        # Can impersonate anyone who's not a superuser and who is in your 
+        # Can impersonate anyone who's not a superuser and who is in your
         # queryset of 'who i can impersonate'
         upk = end_user.pk
         return (
-            not end_user.is_superuser and \
+            not end_user.is_superuser and
             users_impersonable(request).filter(pk=upk).count()
         )
 
 
 
 def import_func_from_string(string_name):
-    ''' Given a string like 'mod.mod2.funcname' which refers to a function, 
+    ''' Given a string like 'mod.mod2.funcname' which refers to a function,
         return that function so it can be called
     '''
     mod_name, func_name = string_name.rsplit('.', 1)
 
 def check_allow_impersonate(request):
     ''' Returns True if this request is allowed to do any impersonation.
-        Uses the IMPERSONATE_CUSTOM_ALLOW function if required, else 
+        Uses the IMPERSONATE_CUSTOM_ALLOW function if required, else
         looks at superuser/staff status and IMPERSONATE_REQUIRE_SUPERUSER
     '''
     if hasattr(settings, 'IMPERSONATE_CUSTOM_ALLOW'):

File impersonate/views.py

 from django.contrib.auth.models import User
 from django.shortcuts import get_object_or_404, redirect, render_to_response
 from decorators import allowed_user_required
-from impersonate.helpers import get_redir_path, get_redir_arg, get_redir_field,\
-                                get_paginator, check_allow_for_user, users_impersonable
+from impersonate.helpers import get_redir_path, get_redir_arg, get_paginator,\
+                                get_redir_field, check_allow_for_user,\
+                                users_impersonable
 
 
 @allowed_user_required
 def impersonate(request, uid):
     ''' Takes in the UID of the user to impersonate.
-        View will fetch the User instance and store it 
+        View will fetch the User instance and store it
         in the request.session under the '_impersonate' key.
-        
-        The middleware will then pick up on it and adjust the 
+
+        The middleware will then pick up on it and adjust the
         request object as needed.
     '''
     new_user = get_object_or_404(User, pk=uid)
     users = users_impersonable(request)
 
     paginator, page, page_number = get_paginator(request, users)
-    
+
     return render_to_response(template, {
         'users': users,
         'paginator': paginator,
 @allowed_user_required
 def search_users(request, template):
     ''' Simple search through the users.
-        Will add 5 items to the context.
+        Will add 7 items to the context.
           * users - All users that match the query passed.
           * paginator - Django Paginator instance
           * page - Current page of objects (from Paginator)
           * page_number - Current page number, defaults to 1
           * query - The search query that was entered
+          * redirect - arg for redirect target, e.g. "?next=/foo/bar"
+          * redirect_field - hidden input field with redirect argument,
+                              put this inside search form
     '''
     query = request.GET.get('q', '')
     search_q = Q(username__icontains=query) | \