def setup_masquerade(request, course_key, staff_access=False, reset_masquerade_data=False):
 if ((request.user is None) or (not settings.FEATURES.get('ENABLE_MASQUERADE', False)) or (not staff_access)):
  return (None, request.user)
 if reset_masquerade_data:
  request.session.pop(MASQUERADE_DATA_KEY, None)
 masquerade_settings = request.session.setdefault(MASQUERADE_SETTINGS_KEY, {})
 request.user.masquerade_settings = masquerade_settings
 course_masquerade = masquerade_settings.get(course_key, None)
 masquerade_user = None
 if (course_masquerade and course_masquerade.user_name):
  try:
   masquerade_user = CourseEnrollment.objects.users_enrolled_in(course_key).get(username=course_masquerade.user_name)
  except User.DoesNotExist:
   course_masquerade = None
   del masquerade_settings[course_key]
   request.session.modified = True
  else:
   masquerade_user.masquerade_settings = request.user.masquerade_settings
   masquerade_user.real_user = request.user
 return (course_masquerade, (masquerade_user or request.user))

def setup_masquerade ( request , course_key , staff_access = False , reset_masquerade_data = False ) : if ( ( request . user is None ) or ( not settings . FEATURES . get ( 'ENABLE_MASQUERADE' , False ) ) or ( not staff_access ) ) : return ( None , request . user ) if reset_masquerade_data : request . session . pop ( MASQUERADE_DATA_KEY , None ) masquerade_settings = request . session . setdefault ( MASQUERADE_SETTINGS_KEY , { } ) request . user . masquerade_settings = masquerade_settings course_masquerade = masquerade_settings . get ( course_key , None ) masquerade_user = None if ( course_masquerade and course_masquerade . user_name ) : try : masquerade_user = CourseEnrollment . objects . users_enrolled_in ( course_key ) . get ( username = course_masquerade . user_name ) except User . DoesNotExist : course_masquerade = None del masquerade_settings [ course_key ] request . session . modified = True else : masquerade_user . masquerade_settings = request . user . masquerade_settings masquerade_user . real_user = request . user return ( course_masquerade , ( masquerade_user or request . user ) )
sets up masquerading for the current user within the current request .