Example #1
0
 def _redirect_from_referrer(self, request, wiki_path):
     """
     redirect to course wiki url if the referrer is from a course page
     """
     course_id = course_id_from_url(request.META.get('HTTP_REFERER'))
     if course_id:
         # See if we are able to view the course. If we are, redirect to it
         try:
             get_course_overview_with_access(request.user, 'load',
                                             course_id)
             return redirect("/courses/{course_id}/wiki/{path}".format(course_id=str(course_id), path=wiki_path))  # lint-amnesty, pylint: disable=line-too-long
         except Http404:
             # Even though we came from the course, we can't see it. So don't worry about it.
             pass
def course_detail(request, username, course_key):
    """
    Return a single course identified by `course_key`.

    The course must be visible to the user identified by `username` and the
    logged-in user should have permission to view courses available to that
    user.

    Arguments:
        request (HTTPRequest):
            Used to identify the logged-in user and to instantiate the course
            module to retrieve the course about description
        username (string):
            The name of the user `requesting_user would like to be identified as.
        course_key (CourseKey): Identifies the course of interest

    Return value:
        `CourseOverview` object representing the requested course
    """
    user = get_effective_user(request.user, username)
    overview = get_course_overview_with_access(
        user,
        get_permission_for_course_about(),
        course_key,
    )
    overview.effective_user = user
    return overview
Example #3
0
def has_instructor_access_for_class(user, course_id):
    """
    Returns true if the `user` is an instructor for the course.
    """

    course = get_course_overview_with_access(user, 'staff', course_id)
    return bool(has_access(user, 'staff', course))
Example #4
0
    def render_to_fragment(self, request, course_id=None, **kwargs):
        """
        Renders the course's home page as a fragment.
        """
        course_key = CourseKey.from_string(course_id)
        course = get_course_overview_with_access(request.user,
                                                 'load',
                                                 course_key,
                                                 check_if_enrolled=True)
        course_url_name = default_course_url_name(course.id)
        course_url = reverse(course_url_name,
                             kwargs={'course_id': six.text_type(course.id)})

        # Render the course home fragment
        context = {
            'csrf': csrf(request)['csrf_token'],
            'course': course,
            'course_key': course_key,
            'course_url': course_url,
            'query': request.GET.get('query', ''),
            'disable_courseware_js': True,
            'uses_bootstrap': True,
        }
        html = render_to_string('course_search/course-search-fragment.html',
                                context)
        return Fragment(html)
Example #5
0
def course_detail(request, username, course_key):
    """
    Return a single course identified by `course_key`.

    The course must be visible to the user identified by `username` and the
    logged-in user should have permission to view courses available to that
    user.

    Arguments:
        request (HTTPRequest):
            Used to identify the logged-in user and to instantiate the course
            module to retrieve the course about description
        username (string):
            The name of the user `requesting_user would like to be identified as.
        course_key (CourseKey): Identifies the course of interest

    Return value:
        `CourseOverview` object representing the requested course
    """
    user = get_effective_user(request.user, username)
    return get_course_overview_with_access(
        user,
        get_permission_for_course_about(),
        course_key,
    )
Example #6
0
    def render_to_fragment(self,
                           request,
                           course_id,
                           user_is_enrolled=True,
                           **kwargs):  # pylint: disable=arguments-differ
        """
        Renders the course outline as a fragment.
        """
        course_key = CourseKey.from_string(course_id)
        course_overview = get_course_overview_with_access(
            request.user,
            'load',
            course_key,
            check_if_enrolled=user_is_enrolled)
        course = modulestore().get_course(course_key)

        course_block_tree = get_course_outline_block_tree(
            request, course_id, request.user if user_is_enrolled else None)
        if not course_block_tree:
            return None

        context = {
            'csrf':
            csrf(request)['csrf_token'],
            'course':
            course_overview,
            'due_date_display_format':
            course.due_date_display_format,
            'blocks':
            course_block_tree,
            'enable_links':
            user_is_enrolled
            or course.course_visibility == COURSE_VISIBILITY_PUBLIC,
        }

        resume_block = get_resume_block(
            course_block_tree) if user_is_enrolled else None

        if not resume_block:
            self.mark_first_unit_to_resume(course_block_tree)

        xblock_display_names = self.create_xblock_id_and_name_dict(
            course_block_tree)
        gated_content = self.get_content_milestones(request, course_key)

        context['gated_content'] = gated_content
        context['xblock_display_names'] = xblock_display_names

        page_context = kwargs.get('page_context', None)
        if page_context:
            context['self_paced'] = page_context.get(
                'pacing_type', 'instructor_paced') == 'self_paced'

        # We're using this flag to prevent old self-paced dates from leaking out on courses not
        # managed by edx-when.
        context['in_edx_when'] = edx_when_api.is_enabled_for_course(course_key)

        html = render_to_string(
            'course_experience/course-outline-fragment.html', context)
        return Fragment(html)
    def render_to_fragment(self, request, course_id, user_is_enrolled=True, **kwargs):  # pylint: disable=arguments-differ
        """
        Renders the course outline as a fragment.
        """
        from lms.urls import RESET_COURSE_DEADLINES_NAME

        course_key = CourseKey.from_string(course_id)
        course_overview = get_course_overview_with_access(
            request.user, 'load', course_key, check_if_enrolled=user_is_enrolled
        )
        course = modulestore().get_course(course_key)

        course_block_tree = get_course_outline_block_tree(
            request, course_id, request.user if user_is_enrolled else None
        )
        if not course_block_tree:
            return None

        resume_block = get_resume_block(course_block_tree) if user_is_enrolled else None

        if not resume_block:
            self.mark_first_unit_to_resume(course_block_tree)

        xblock_display_names = self.create_xblock_id_and_name_dict(course_block_tree)
        gated_content = self.get_content_milestones(request, course_key)

        missed_deadlines, missed_gated_content = dates_banner_should_display(course_key, request.user)

        reset_deadlines_url = reverse(RESET_COURSE_DEADLINES_NAME)

        context = {
            'csrf': csrf(request)['csrf_token'],
            'course': course_overview,
            'due_date_display_format': course.due_date_display_format,
            'blocks': course_block_tree,
            'enable_links': user_is_enrolled or course.course_visibility == COURSE_VISIBILITY_PUBLIC,
            'course_key': course_key,
            'gated_content': gated_content,
            'xblock_display_names': xblock_display_names,
            'self_paced': course.self_paced,

            # We're using this flag to prevent old self-paced dates from leaking out on courses not
            # managed by edx-when.
            'in_edx_when': edx_when_api.is_enabled_for_course(course_key),
            'reset_deadlines_url': reset_deadlines_url,
            'verified_upgrade_link': verified_upgrade_deadline_link(request.user, course=course),
            'on_course_outline_page': True,
            'missed_deadlines': missed_deadlines,
            'missed_gated_content': missed_gated_content,
            'has_ended': course.has_ended(),
        }

        html = render_to_string('course_experience/course-outline-fragment.html', context)
        return Fragment(html)
Example #8
0
def users(request, course_id):
    """
    Given a `username` query parameter, find matches for users in the forum for this course.

    Only exact matches are supported here, so the length of the result set will either be 0 or 1.
    """

    course_key = CourseKey.from_string(course_id)
    try:
        get_course_overview_with_access(request.user,
                                        'load',
                                        course_key,
                                        check_if_enrolled=True)
    except Http404:
        # course didn't exist, or requesting user does not have access to it.
        return JsonError(status=404)
    except CourseAccessRedirect:
        # user does not have access to the course.
        return JsonError(status=404)

    try:
        username = request.GET['username']
    except KeyError:
        # 400 is default status for JsonError
        return JsonError(["username parameter is required"])

    user_objs = []
    try:
        matched_user = User.objects.get(username=username)
        cc_user = cc.User.from_django_user(matched_user)
        cc_user.course_id = course_key
        cc_user.retrieve(complete=False)
        if (cc_user['threads_count'] + cc_user['comments_count']) > 0:
            user_objs.append({
                'id': matched_user.id,
                'username': matched_user.username,
            })
    except User.DoesNotExist:
        pass
    return JsonResponse({"users": user_objs})
    def render_to_fragment(self,
                           request,
                           course_id,
                           user_is_enrolled=True,
                           **kwargs):  # pylint: disable=arguments-differ
        """
        Renders the course outline as a fragment.
        """
        from lms.urls import RESET_COURSE_DEADLINES_NAME
        from openedx.features.course_experience.urls import COURSE_HOME_VIEW_NAME

        course_key = CourseKey.from_string(course_id)
        course_overview = get_course_overview_with_access(
            request.user,
            'load',
            course_key,
            check_if_enrolled=user_is_enrolled)
        course = modulestore().get_course(course_key)

        course_block_tree = get_course_outline_block_tree(
            request, course_id, request.user if user_is_enrolled else None)
        if not course_block_tree:
            return None

        context = {
            'csrf': csrf(request)['csrf_token'],
            'course': course_overview,
            'due_date_display_format': course.due_date_display_format,
            'blocks': course_block_tree,
            'enable_links': user_is_enrolled
            or course.course_visibility == COURSE_VISIBILITY_PUBLIC,
            'course_key': course_key,
        }

        resume_block = get_resume_block(
            course_block_tree) if user_is_enrolled else None

        if not resume_block:
            self.mark_first_unit_to_resume(course_block_tree)

        xblock_display_names = self.create_xblock_id_and_name_dict(
            course_block_tree)
        gated_content = self.get_content_milestones(request, course_key)

        context['gated_content'] = gated_content
        context['xblock_display_names'] = xblock_display_names

        page_context = kwargs.get('page_context', None)
        if page_context:
            context['self_paced'] = page_context.get(
                'pacing_type', 'instructor_paced') == 'self_paced'

        # We're using this flag to prevent old self-paced dates from leaking out on courses not
        # managed by edx-when.
        context['in_edx_when'] = edx_when_api.is_enabled_for_course(course_key)

        reset_deadlines_url = reverse(RESET_COURSE_DEADLINES_NAME)
        reset_deadlines_redirect_url_base = COURSE_HOME_VIEW_NAME

        course_enrollment = None
        if not request.user.is_anonymous:
            course_enrollment = CourseEnrollment.objects.filter(
                course=course_overview, user=request.user).filter(
                    Q(mode=CourseMode.AUDIT)
                    | Q(mode=CourseMode.VERIFIED)).first()

        context['reset_deadlines_url'] = reset_deadlines_url
        context[
            'reset_deadlines_redirect_url_base'] = reset_deadlines_redirect_url_base
        context['reset_deadlines_redirect_url_id_dict'] = {
            'course_id': str(course.id)
        }
        context['enrollment_mode'] = getattr(course_enrollment, 'mode', None)
        context['verified_upgrade_link'] = verified_upgrade_deadline_link(
            request.user, course=course),
        context['on_course_outline_page'] = True,

        html = render_to_string(
            'course_experience/course-outline-fragment.html', context)
        return Fragment(html)
Example #10
0
def course_detail(user, course_key):
    return get_course_overview_with_access(
        user,
        get_permission_for_course_about(),
        course_key,
    )