@@ -209,9 +209,18 @@ def get(self, request, *args, **kwargs): # pylint: disable=too-many-statements
209209 )
210210
211211 user_is_masquerading = is_masquerading (request .user , course_key , course_masquerade = masquerade_object )
212+ user = request .user
213+ # Check if the user is masquerading as a student and get the masqueraded user object
214+ if user_is_masquerading and masquerade_object .role == 'student' :
215+ try :
216+ # If the masqueraded user does not exist, we will continue with the original user object.
217+ username = masquerade_object .user_name or 'audit'
218+ user = User .objects .get (username = username )
219+ except User .DoesNotExist :
220+ pass
212221
213222 course_overview = get_course_overview_or_404 (course_key )
214- enrollment = CourseEnrollment .get_enrollment (request . user , course_key )
223+ enrollment = CourseEnrollment .get_enrollment (user , course_key )
215224 enrollment_mode = getattr (enrollment , 'mode' , None )
216225 allow_anonymous = COURSE_ENABLE_UNENROLLED_ACCESS_FLAG .is_enabled (course_key )
217226 allow_public = allow_anonymous and course .course_visibility == COURSE_VISIBILITY_PUBLIC
@@ -254,35 +263,35 @@ def get(self, request, *args, **kwargs): # pylint: disable=too-many-statements
254263 show_enrolled = is_enrolled or is_staff
255264 enable_proctored_exams = False
256265 if show_enrolled :
257- course_blocks = get_course_outline_block_tree (request , course_key_string , request . user )
258- date_blocks = get_course_date_blocks (course , request . user , request , num_assignments = 1 )
266+ course_blocks = get_course_outline_block_tree (request , course_key_string , user )
267+ date_blocks = get_course_date_blocks (course , user , request , num_assignments = 1 )
259268 course_date_blocks = (
260269 [block for block in date_blocks if not isinstance (block , TodaysDate )]
261- if DatesTab .is_enabled (course , request . user )
270+ if DatesTab .is_enabled (course , user )
262271 else []
263272 )
264273 dates_widget ['course_date_blocks' ] = course_date_blocks
265274
266- handouts_html = get_course_info_section (request , request . user , course , 'handouts' )
275+ handouts_html = get_course_info_section (request , user , course , 'handouts' )
267276 welcome_message_html = get_current_update_for_user (request , course )
268277
269- offer_data = generate_offer_data (request . user , course_overview )
270- access_expiration = get_access_expiration_data (request . user , course_overview )
271- cert_data = get_cert_data (request . user , course , enrollment .mode ) if is_enrolled else None
278+ offer_data = generate_offer_data (user , course_overview )
279+ access_expiration = get_access_expiration_data (user , course_overview )
280+ cert_data = get_cert_data (user , course , enrollment .mode ) if is_enrolled else None
272281
273282 enable_proctored_exams = course_overview .enable_proctored_exams
274283
275284 if (is_enrolled and ENABLE_COURSE_GOALS .is_enabled (course_key )):
276285 course_goals ['weekly_learning_goal_enabled' ] = True
277- selected_goal = get_course_goal (request . user , course_key )
286+ selected_goal = get_course_goal (user , course_key )
278287 if selected_goal :
279288 course_goals ['selected_goal' ] = {
280289 'days_per_week' : selected_goal .days_per_week ,
281290 'subscribed_to_reminders' : selected_goal .subscribed_to_reminders ,
282291 }
283292
284293 try :
285- resume_block = get_key_to_last_completed_block (request . user , course .id )
294+ resume_block = get_key_to_last_completed_block (user , course .id )
286295 resume_course ['has_visited_course' ] = True
287296 resume_path = reverse ('jump_to' , kwargs = {
288297 'course_id' : course_key_string ,
@@ -296,7 +305,7 @@ def get(self, request, *args, **kwargs): # pylint: disable=too-many-statements
296305 elif allow_public_outline or allow_public or user_is_masquerading :
297306 course_blocks = get_course_outline_block_tree (request , course_key_string , None )
298307 if allow_public or user_is_masquerading :
299- handouts_html = get_course_info_section (request , request . user , course , 'handouts' )
308+ handouts_html = get_course_info_section (request , user , course , 'handouts' )
300309
301310 if not is_enrolled :
302311 if CourseMode .is_masters_only (course_key ):
@@ -305,7 +314,7 @@ def get(self, request, *args, **kwargs): # pylint: disable=too-many-statements
305314 'Please contact your degree administrator or '
306315 '{platform_name} Support if you have questions.'
307316 ).format (platform_name = settings .PLATFORM_NAME )
308- elif CourseEnrollment .is_enrollment_closed (request . user , course_overview ):
317+ elif CourseEnrollment .is_enrollment_closed (user , course_overview ):
309318 enroll_alert ['can_enroll' ] = False
310319 elif CourseEnrollment .objects .is_course_full (course_overview ):
311320 enroll_alert ['can_enroll' ] = False
@@ -321,7 +330,7 @@ def get(self, request, *args, **kwargs): # pylint: disable=too-many-statements
321330 # so this is a tiny first step in that migration.
322331 if course_blocks :
323332 user_course_outline = get_user_course_outline (
324- course_key , request . user , datetime .now (tz = timezone .utc ) # noqa: UP017
333+ course_key , user , datetime .now (tz = timezone .utc ) # noqa: UP017
325334 )
326335 available_seq_ids = {str (usage_key ) for usage_key in user_course_outline .sequences }
327336
@@ -352,8 +361,8 @@ def get(self, request, *args, **kwargs): # pylint: disable=too-many-statements
352361 ] if 'children' in chapter_data else []
353362
354363 user_has_passing_grade = False
355- if not request . user .is_anonymous :
356- user_grade = CourseGradeFactory ().read (request . user , course )
364+ if not user .is_anonymous :
365+ user_grade = CourseGradeFactory ().read (user , course )
357366 if user_grade :
358367 user_has_passing_grade = user_grade .passed
359368
0 commit comments