def __init__(self, course_key, request, username=''): self.request = request self.overview = course_detail( self.request, username or self.request.user.username, course_key, ) # We must compute course load access *before* setting up masquerading, # else course staff (who are not enrolled) will not be able view # their course from the perspective of a learner. self.load_access = check_course_access( self.overview, self.request.user, 'load', check_if_enrolled=True, check_if_authenticated=True, ) self.original_user_is_staff = has_access(self.request.user, 'staff', self.overview).has_access self.original_user_is_global_staff = self.request.user.is_staff self.course_key = course_key self.course = get_course_by_id(self.course_key) self.course_masquerade, self.effective_user = setup_masquerade( self.request, course_key, staff_access=self.original_user_is_staff, ) self.request.user = self.effective_user self.is_staff = has_access(self.effective_user, 'staff', self.overview).has_access self.enrollment_object = CourseEnrollment.get_enrollment(self.effective_user, self.course_key, select_related=['celebration', 'user__celebration']) self.can_view_legacy_courseware = courseware_legacy_is_visible( course_key=course_key, is_global_staff=self.original_user_is_global_staff, )
def __init__(self, course_key, request, username=''): self.request = request self.overview = course_detail( self.request, username or self.request.user.username, course_key, ) self.original_user_is_staff = has_access(self.request.user, 'staff', self.overview).has_access self.original_user_is_global_staff = self.request.user.is_staff self.course_key = course_key self.course = get_course_by_id(self.course_key) self.course_masquerade, self.effective_user = setup_masquerade( self.request, course_key, staff_access=self.original_user_is_staff, ) self.request.user = self.effective_user self.is_staff = has_access(self.effective_user, 'staff', self.overview).has_access self.enrollment_object = CourseEnrollment.get_enrollment( self.effective_user, self.course_key, select_related=['celebration', 'user__celebration']) self.can_view_legacy_courseware = courseware_legacy_is_visible( course_key=course_key, is_global_staff=self.original_user_is_global_staff, )
def get_object(self): """ Return the requested course object, if the user has appropriate permissions. """ overview = course_detail( self.request, self.request.user.username, CourseKey.from_string(self.kwargs['course_key_string']), ) if self.request.user.is_anonymous: mode = None is_active = False else: mode, is_active = CourseEnrollment.enrollment_mode_for_user( overview.effective_user, overview.id) overview.enrollment = {'mode': mode, 'is_active': is_active} if not is_active: user_has_access = allow_public_access(overview, [COURSE_VISIBILITY_PUBLIC]) else: user_has_access = True overview.user_has_access = user_has_access overview.user_has_staff_access = has_access(self.request.user, 'staff', overview).has_access return overview
def get(self, request, *args, **kwargs): course_key_string = kwargs.get('course_key_string') course_key = CourseKey.from_string(course_key_string) original_user_is_staff = has_access(request.user, 'staff', course_key).has_access _, request.user = setup_masquerade( request, course_key, staff_access=has_access(request.user, 'staff', course_key), reset_masquerade_data=True, ) course = course_detail(request, request.user.username, course_key) user_is_enrolled = CourseEnrollment.is_enrolled(request.user, course_key_string) data = { 'course_id': course.id, 'is_staff': has_access(request.user, 'staff', course_key).has_access, 'original_user_is_staff': original_user_is_staff, 'number': course.display_number_with_default, 'org': course.display_org_with_default, 'tabs': get_course_tab_list(request.user, course), 'title': course.display_name_with_default, 'is_self_paced': getattr(course, 'self_paced', False), 'is_enrolled': user_is_enrolled, } context = self.get_serializer_context() context['course'] = course serializer = self.get_serializer_class()(data, context=context) return Response(serializer.data)
def __init__(self, course_key, request, username=''): self.overview = course_detail( request, username or request.user.username, course_key, ) self.effective_user = self.overview.effective_user self.course_key = course_key
def get_object(self): """ Return the requested course object, if the user has appropriate permissions. """ return course_detail( self.request, self.request.user.username, CourseKey.from_string(self.kwargs['course_key_string']), )
def __init__(self, course_key, request, username=''): self.overview = course_detail( request, username or request.user.username, course_key, ) self.effective_user = self.overview.effective_user self.course_key = course_key self.enrollment_object = CourseEnrollment.get_enrollment( self.effective_user, self.course_key, select_related=['celebration'])
def get(self, request, *args, **kwargs): course_key_string = kwargs.get('course_key_string') course_key = CourseKey.from_string(course_key_string) original_user_is_staff = has_access(request.user, 'staff', course_key).has_access _, request.user = setup_masquerade( request, course_key, staff_access=has_access(request.user, 'staff', course_key), reset_masquerade_data=True, ) username = request.user.username if request.user.username else None course = course_detail(request, request.user.username, course_key) enrollment = CourseEnrollment.get_enrollment(request.user, course_key_string) user_is_enrolled = bool(enrollment and enrollment.is_active) courseware_meta = CoursewareMeta(course_key, request, request.user.username) can_load_courseware = courseware_meta.is_microfrontend_enabled_for_user( ) browser_timezone = self.request.query_params.get( 'browser_timezone', None) celebrations = get_celebrations_dict(request.user, enrollment, course, browser_timezone) data = { 'course_id': course.id, 'username': username, 'is_staff': has_access(request.user, 'staff', course_key).has_access, 'original_user_is_staff': original_user_is_staff, 'number': course.display_number_with_default, 'org': course.display_org_with_default, 'tabs': get_course_tab_list(request.user, course), 'title': course.display_name_with_default, 'is_self_paced': getattr(course, 'self_paced', False), 'is_enrolled': user_is_enrolled, 'can_load_courseware': can_load_courseware, 'celebrations': celebrations, } context = self.get_serializer_context() context['course'] = course context['course_overview'] = course context['enrollment'] = enrollment serializer = self.get_serializer_class()(data, context=context) return Response(serializer.data)
def get(self, request, *args, **kwargs): course_key_string = kwargs.get('course_key_string') course_key = CourseKey.from_string(course_key_string) course = course_detail(request, request.user.username, course_key) data = { 'course_id': course.id, 'is_staff': has_access(request.user, 'staff', course_key).has_access, 'number': course.display_number_with_default, 'org': course.display_org_with_default, 'tabs': get_course_tab_list(request.user, course), 'title': course.display_name_with_default, } context = self.get_serializer_context() context['course'] = course serializer = self.get_serializer_class()(data, context=context) return Response(serializer.data)
def __init__(self, course_key, request, username=''): self.request = request self.overview = course_detail( self.request, username or self.request.user.username, course_key, ) self.original_user_is_staff = has_access(self.request.user, 'staff', self.overview).has_access self.course_key = course_key self.course_masquerade, self.effective_user = setup_masquerade( self.request, course_key, staff_access=self.original_user_is_staff, ) self.request.user = self.effective_user self.is_staff = has_access(self.effective_user, 'staff', self.overview).has_access self.enrollment_object = CourseEnrollment.get_enrollment(self.effective_user, self.course_key, select_related=['celebration'])
def get_object(self): """ Return the requested course object, if the user has appropriate permissions. """ overview = course_detail( self.request, self.request.user.username, CourseKey.from_string(self.kwargs['course_key_string']), ) if self.request.user.is_anonymous: mode = None is_active = False else: mode, is_active = CourseEnrollment.enrollment_mode_for_user( overview.effective_user, overview.id) overview.enrollment = {'mode': mode, 'is_active': is_active} overview.is_staff = has_access(self.request.user, 'staff', overview).has_access overview.can_load_courseware = check_course_access( overview, self.request.user, 'load', check_if_enrolled=True, check_survey_complete=False, check_if_authenticated=True, ).to_json() # TODO: TNL-7185 Legacy: Refactor to return the expiration date and format the message in the MFE overview.course_expired_message = generate_course_expired_message( self.request.user, overview) # TODO: TNL-7185 Legacy: Refactor to return the offer data and format the message in the MFE overview.offer_html = generate_offer_html(self.request.user, overview) course_key = CourseKey.from_string(self.kwargs['course_key_string']) overview.content_type_gating_enabled = ContentTypeGatingConfig.enabled_for_enrollment( user=self.request.user, course_key=course_key, ) return overview
def __init__(self, course_key, request, username=''): self.overview = course_detail( request, username or request.user.username, course_key, ) self.effective_user = self.overview.effective_user # We need to memoize `is_staff` _before_ we configure masquerade. self.is_staff = has_access(self.effective_user, 'staff', self.overview).has_access self.course_key = course_key self.enrollment_object = CourseEnrollment.get_enrollment( self.effective_user, self.course_key, select_related=['celebration']) course_masquerade, _user = setup_masquerade( request, course_key, staff_access=self.is_staff, ) self.course_masquerade = course_masquerade
def get(self, request, *args, **kwargs): course_key_string = kwargs.get('course_key_string') course_key = CourseKey.from_string(course_key_string) original_user_is_staff = has_access(request.user, 'staff', course_key).has_access _, request.user = setup_masquerade( request, course_key, staff_access=has_access(request.user, 'staff', course_key), reset_masquerade_data=True, ) course = course_detail(request, request.user.username, course_key) user_is_enrolled = CourseEnrollment.is_enrolled(request.user, course_key_string) browser_timezone = request.query_params.get('browser_timezone', None) celebrations = { 'streak_length_to_celebrate': UserCelebration.perform_streak_updates( request.user, course_key, browser_timezone ) } data = { 'course_id': course.id, 'is_staff': has_access(request.user, 'staff', course_key).has_access, 'original_user_is_staff': original_user_is_staff, 'number': course.display_number_with_default, 'org': course.display_org_with_default, 'tabs': get_course_tab_list(request.user, course), 'title': course.display_name_with_default, 'is_self_paced': getattr(course, 'self_paced', False), 'is_enrolled': user_is_enrolled, 'celebrations': celebrations, } context = self.get_serializer_context() context['course'] = course serializer = self.get_serializer_class()(data, context=context) return Response(serializer.data)
def reset_course_deadlines(request): """ Set the start_date of a schedule to today, which in turn will adjust due dates for sequentials belonging to a self paced course Request Parameters: course_key: course key research_event_data: any data that should be included in the research tracking event Example: sending the location of where the reset deadlines banner (i.e. outline-tab) IMPORTANT NOTE: If updates are happening to the logic here, ALSO UPDATE the `reset_course_deadlines` function in common/djangoapps/util/views.py as well. """ course_key = request.data.get('course_key', None) research_event_data = request.data.get('research_event_data', {}) # If body doesnt contain 'course_key', return 400 to client. if not course_key: raise ParseError(_("'course_key' is required.")) try: course_key = CourseKey.from_string(course_key) course_masquerade, user = setup_masquerade( request, course_key, has_access(request.user, 'staff', course_key)) # We ignore the missed_deadlines because this endpoint is used in the Learning MFE for # learners who have remaining attempts on a problem and reset their due dates in order to # submit additional attempts. This can apply for 'completed' (submitted) content that would # not be marked as past_due _missed_deadlines, missed_gated_content = dates_banner_should_display( course_key, user) if not missed_gated_content: reset_self_paced_schedule(user, course_key) course_overview = course_detail(request, user.username, course_key) # For context here, research_event_data should already contain `location` indicating # the page/location dates were reset from and could also contain `block_id` if reset # within courseware. research_event_data.update({ 'courserun_key': str(course_key), 'is_masquerading': is_masquerading(user, course_key, course_masquerade), 'is_staff': has_access(user, 'staff', course_key).has_access, 'org_key': course_overview.display_org_with_default, 'user_id': user.id, }) tracker.emit('edx.ui.lms.reset_deadlines.clicked', research_event_data) if course_home_legacy_is_active(course_key): body_link = '{}{}'.format(settings.LMS_ROOT_URL, reverse('dates', args=[str(course_key)])) else: body_link = get_learning_mfe_home_url(course_key=str(course_key), view_name='dates') return Response({ 'body': format_html('<a href="{}">{}</a>', body_link, _('View all dates')), 'header': _('Your due dates have been successfully shifted to help you stay on track.' ), 'link': body_link, 'link_text': _('View all dates'), 'message': _('Deadlines successfully reset.'), }) except Exception as reset_deadlines_exception: log.exception('Error occurred while trying to reset deadlines!') raise UnableToResetDeadlines from reset_deadlines_exception
def get(self, request, *args, **kwargs): course_key_string = kwargs.get('course_key_string') course_key = CourseKey.from_string(course_key_string) original_user_is_global_staff = self.request.user.is_staff original_user_is_staff = has_access(request.user, 'staff', course_key).has_access course = course_detail(request, request.user.username, course_key) # We must compute course load access *before* setting up masquerading, # else course staff (who are not enrolled) will not be able view # their course from the perspective of a learner. load_access = check_course_access( course, request.user, 'load', check_if_enrolled=True, check_if_authenticated=True, ) _, request.user = setup_masquerade( request, course_key, staff_access=original_user_is_staff, reset_masquerade_data=True, ) username = request.user.username if request.user.username else None enrollment = CourseEnrollment.get_enrollment(request.user, course_key_string) user_is_enrolled = bool(enrollment and enrollment.is_active) can_load_courseware = courseware_mfe_is_visible( course_key=course_key, is_global_staff=original_user_is_global_staff, is_course_staff=original_user_is_staff) # User locale settings user_timezone_locale = user_timezone_locale_prefs(request) user_timezone = user_timezone_locale['user_timezone'] browser_timezone = self.request.query_params.get( 'browser_timezone', None) celebrations = get_celebrations_dict( request.user, enrollment, course, user_timezone if not None else browser_timezone) # Record course goals user activity for (web) learning mfe course tabs UserActivity.record_user_activity(request.user, course_key) data = { 'course_id': course.id, 'username': username, 'is_staff': has_access(request.user, 'staff', course_key).has_access, 'original_user_is_staff': original_user_is_staff, 'number': course.display_number_with_default, 'org': course.display_org_with_default, 'start': course.start, 'tabs': get_course_tab_list(request.user, course), 'title': course.display_name_with_default, 'is_self_paced': getattr(course, 'self_paced', False), 'is_enrolled': user_is_enrolled, 'course_access': load_access.to_json(), 'can_load_courseware': can_load_courseware, 'celebrations': celebrations, 'user_timezone': user_timezone, } context = self.get_serializer_context() context['course'] = course context['course_overview'] = course context['enrollment'] = enrollment serializer = self.get_serializer_class()(data, context=context) return Response(serializer.data)