url_helpers.py 2.13 KB
Newer Older
1 2 3
"""
Module to define url helpers functions
"""
Jonathan Piacenti committed
4
from urllib import urlencode
5 6 7 8 9
from xmodule.modulestore.search import path_to_location, navigation_index
from xmodule.modulestore.django import modulestore
from django.core.urlresolvers import reverse


10
def get_redirect_url(course_key, usage_key):
11 12 13 14 15 16 17 18 19 20 21 22 23
    """ Returns the redirect url back to courseware

    Args:
        course_id(str): Course Id string
        location(str): The location id of course component

    Raises:
        ItemNotFoundError if no data at the location or NoPathToItem if location not in any class

    Returns:
        Redirect url string
    """

24 25 26 27
    (
        course_key, chapter, section, vertical_unused,
        position, final_target_id
    ) = path_to_location(modulestore(), usage_key)
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

    # choose the appropriate view (and provide the necessary args) based on the
    # args provided by the redirect.
    # Rely on index to do all error handling and access control.
    if chapter is None:
        redirect_url = reverse('courseware', args=(unicode(course_key), ))
    elif section is None:
        redirect_url = reverse('courseware_chapter', args=(unicode(course_key), chapter))
    elif position is None:
        redirect_url = reverse(
            'courseware_section',
            args=(unicode(course_key), chapter, section)
        )
    else:
        # Here we use the navigation_index from the position returned from
        # path_to_location - we can only navigate to the topmost vertical at the
        # moment
        redirect_url = reverse(
            'courseware_position',
            args=(unicode(course_key), chapter, section, navigation_index(position))
        )
Jonathan Piacenti committed
49
    redirect_url += "?{}".format(urlencode({'activate_block_id': unicode(final_target_id)}))
50
    return redirect_url
51 52


attiyaishaque committed
53
def get_redirect_url_for_global_staff(course_key, _next):
54
    """
attiyaishaque committed
55
    Returns the redirect url for staff enrollment
56 57 58

    Args:
        course_key(str): Course key string
attiyaishaque committed
59
        _next(str): Redirect url of course component
60
    """
attiyaishaque committed
61
    redirect_url = ("{url}?next={redirect}".format(
62
        url=reverse('enroll_staff', args=[unicode(course_key)]),
attiyaishaque committed
63
        redirect=_next))
64
    return redirect_url