utils.py 1.24 KB
Newer Older
1 2 3 4
"""
Common methods for cms commands to use
"""
from django.contrib.auth.models import User
5
from opaque_keys.edx.keys import CourseKey
6

7
from xmodule.modulestore.django import modulestore
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22


def user_from_str(identifier):
    """
    Return a user identified by the given string. The string could be an email
    address, or a stringified integer corresponding to the ID of the user in
    the database. If no user could be found, a User.DoesNotExist exception
    will be raised.
    """
    try:
        user_id = int(identifier)
    except ValueError:
        return User.objects.get(email=identifier)

    return User.objects.get(id=user_id)
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40


def get_course_versions(course_key):
    """
    Fetches the latest course versions
    :param course_key:
    :return: { 'draft-branch' : value1, 'published-branch' : value2}
    """
    course_locator = CourseKey.from_string(course_key)
    store = modulestore()._get_modulestore_for_courselike(course_locator)  # pylint: disable=protected-access
    index_entry = store.get_course_index(course_locator)
    if index_entry is not None:
        return {
            'draft-branch': index_entry['versions']['draft-branch'],
            'published-branch': index_entry['versions']['published-branch']
        }

    return None