@require_POST
@login_required
@expect_json
def handle_ajax(request, course_key_string):
 course_key = CourseKey.from_string(course_key_string)
 masquerade_settings = request.session.get(MASQUERADE_SETTINGS_KEY, {})
 request_json = request.json
 role = request_json.get('role', 'student')
 group_id = request_json.get('group_id', None)
 user_partition_id = (request_json.get('user_partition_id', None) if (group_id is not None) else None)
 user_name = request_json.get('user_name', None)
 if user_name:
  users_in_course = CourseEnrollment.objects.users_enrolled_in(course_key)
  try:
   if ('@' in user_name):
    user_name = users_in_course.get(email=user_name).username
   else:
    users_in_course.get(username=user_name)
  except User.DoesNotExist:
   return JsonResponse({'success': False, 'error': _('There is no user with the username or email address {user_name} enrolled in this course.').format(user_name=user_name)})
 masquerade_settings[course_key] = CourseMasquerade(course_key, role=role, user_partition_id=user_partition_id, group_id=group_id, user_name=user_name)
 request.session[MASQUERADE_SETTINGS_KEY] = masquerade_settings
 return JsonResponse({'success': True})

@ require_POST @ login_required @ expect_json def handle_ajax ( request , course_key_string ) : course_key = CourseKey . from_string ( course_key_string ) masquerade_settings = request . session . get ( MASQUERADE_SETTINGS_KEY , { } ) request_json = request . json role = request_json . get ( 'role' , 'student' ) group_id = request_json . get ( 'group_id' , None ) user_partition_id = ( request_json . get ( 'user_partition_id' , None ) if ( group_id is not None ) else None ) user_name = request_json . get ( 'user_name' , None ) if user_name : users_in_course = CourseEnrollment . objects . users_enrolled_in ( course_key ) try : if ( '@' in user_name ) : user_name = users_in_course . get ( email = user_name ) . username else : users_in_course . get ( username = user_name ) except User . DoesNotExist : return JsonResponse ( { 'success' : False , 'error' : _ ( 'There is no user with the username or email address {user_name} enrolled in this course.' ) . format ( user_name = user_name ) } ) masquerade_settings [ course_key ] = CourseMasquerade ( course_key , role = role , user_partition_id = user_partition_id , group_id = group_id , user_name = user_name ) request . session [ MASQUERADE_SETTINGS_KEY ] = masquerade_settings return JsonResponse ( { 'success' : True } )
handle ajax posts to update the current users masquerade for the specified course .