LORE API Docs

For convenient reference in development, here are the LORE API docs.

LearningResources

Helper functions for using the models, so external apps don’t tie functionality to internal implementation.

exception learningresources.api.LearningResourceException[source]

Bases: exceptions.Exception

Base class for our custom exceptions.

class learningresources.api.MissingTitle[source]

Bases: object

Class to describe the missing title for importer and for the description path

for_desc_path_field = u'...'
for_title_field = u'Missing Title'
exception learningresources.api.NotFound[source]

Bases: learningresources.api.LearningResourceException

Raised by the API when the item requested does not exist.

exception learningresources.api.PermissionDenied[source]

Bases: learningresources.api.LearningResourceException

Raised by the API when the requested item exists, but the user is not allowed to access it.

learningresources.api.create_course(org, repo_id, course_number, run, user_id)[source]

Add a course to the database.

Parameters:
  • org (unicode) – Organization
  • repo_id (int) – Repository id
  • course_number (unicode) – Course number
  • run (unicode) – Run
  • user_id (int) – Primary key of user creating the course
Raises:

ValueError – Duplicate course

Returns:

course – The created course

Return type:

learningresources.models.Course

learningresources.api.create_repo(name, description, user_id)[source]

Create a new repository.

Parameters:
  • name (unicode) – Repository name
  • description (unicode) – Repository description
  • user_id (int) – User ID of repository creator
Returns:

repo – Newly-created repository

Return type:

learningresources.Repository

learningresources.api.create_resource(course, parent, resource_type, title, content_xml, mpath, url_name, dpath)[source]

Create a learning resource.

Parameters:
  • course (learningresources.models.Course) – Course
  • parent (learningresources.models.LearningResource) – Parent LearningResource
  • resource_type (unicode) – Name of LearningResourceType
  • title (unicode) – Title of resource
  • content_xml (unicode) – XML
  • mpath (unicode) – Materialized path
  • url_name (unicode) – Resource identifier
  • dpath (unicode) – Description path
Returns:

resource – New LearningResource

Return type:

learningresources.models.LearningResource

learningresources.api.create_static_asset(course_id, handle)[source]

Create a static asset. :param course_id: learningresources.models.Course pk :type course_id: int :param handle: file handle :type handle: django.core.files.File

Returns:learningresources.models.StaticAsset
learningresources.api.get_repo(repo_slug, user_id)[source]

Get repository for a user if s/he can access it. Returns a repository object if it exists or * raises a 404 if the object does not exist * raises a 403 if the object exists but the user doesn’t have permission

Parameters:
  • repo_slug (unicode) – Repository slug
  • user_id (int) – Primary key of user
Returns:

repo – Repository

Return type:

learningresource.Repository

learningresources.api.get_repos(user_id)[source]

Get all repositories a user may access.

Parameters:user_id (int) – Primary key of user
Returns:repos – Repositories
Return type:query set of learningresource.Repository
learningresources.api.get_resource(resource_id, user_id)[source]

Get single resource.

Parameters:
  • resource_id (int) – Primary key of the LearningResource
  • user_id (int) – Primary key of the user requesting the resource
Returns:

resource – Resource May be None if the resource does not exist or the user does not have permissions.

Return type:

learningresources.LearningResource

learningresources.api.get_resources(repo_id)[source]

Get resources from a repository ordered by title.

Parameters:repo_id (int) – Primary key of the repository
Returns:list – List of resources
Return type:list of learningresources.LearningResource
learningresources.api.get_video_sub(xml)[source]

Get subtitle IDs from <video> XML.

Parameters:xml (lxml.etree) – xml for a LearningResource
Returns:subtitle string
Return type:sub string
learningresources.api.import_static_assets(course, path)[source]

Upload all assets and create model records of them for a given course and path.

Parameters:
  • course (learningresources.models.Course) – Course to add assets to.
  • path (unicode) – course specific path to extracted OLX tree.
Returns:

None

learningresources.api.join_description_paths(*args)[source]

Helper function to format the description path. :param args: description path :type args: unicode

Returns:Formatted dpath
Return type:unicode
learningresources.api.type_id_by_name(name)[source]

Get or create a LearningResourceType by name.

This would do fewer queries if it did all the lookups up front, but this is simpler to read and understand and still prevents most lookups. Also, it can’t prevent inserts, so it’s never guaranteed to be just a single query.

Parameters:name (unicode) – LearningResourceType.name
Returns:type_id – Primary key of learningresources.LearningResourceType
Return type:int
learningresources.api.update_description_path(resource, force_parent_update=False)[source]

Updates the specified learning resource description path based on the current title and the parent’s description path :param resource: LearningResource :type resource: learningresources.models.LearningResource :param force_parent_update: force parent update :type force_parent_update: boolean

Returns:None
learningresources.api.update_xanalytics(data)[source]

Update xanalytics fields for a LearningResource. :param data: dict from JSON file from xanalytics :type data: dict

Returns:count – number of records updated
Return type:int

Importer

Import OLX data into LORE.

importer.api.import_children(course, element, parent, parent_dpath)[source]

Create LearningResource instances for each element of an XML tree.

Parameters:
  • course (learningresources.models.Course) – Course
  • element (lxml.etree) – XML element within xbundle
  • parent (learningresources.models.LearningResource) – Parent LearningResource
  • parent_dpath (unicode) – parent description path
Returns:

None

importer.api.import_course(bundle, repo_id, user_id, static_dir)[source]

Import a course from an XBundle object.

Parameters:
  • bundle (xbundle.XBundle) – Course as xbundle XML
  • repo_id (int) – Primary key of repository course belongs to
  • user_id (int) – Primary key of Django user doing the import
  • static_dir (unicode) – location of static files
Returns:

learningresources.models.Course

importer.api.import_course_from_file(filename, repo_id, user_id)[source]

Import OLX archive from .zip or tar.gz.

Imports from a file and then deletes that file. A valid OLX archive has a single occurrence of the file course.xml in its root directory, or no course.xml in its root and a single occurrence of course.xml in one or more of the root directory’s children.

Parameters:
  • filename (unicode) – Path to archive file (zip or .tar.gz)
  • repo_id (int) – Primary key of repository course belongs to
  • user_id (int) – Primary key of user importing the course
Returns:

None

Raises:

ValueError – Unable to extract or read archive contents.

importer.api.import_course_from_path(path, repo_id, user_id)[source]

Import course from an OLX directory.

Parameters:
  • path (unicode) – Path to extracted OLX tree
  • repo_id (int) – Primary key of repository course belongs to
  • user_id (int) – Primary key of Django user doing the import
Returns:

course (learningresources.Course)

importer.api.is_leaf_tag(tag)[source]

Should we look for resources within elements with this tag?

Parameters:tag (unicode) – Element tag
Returns:Whether tag is leaf tag
Return type:bool

Taxonomy

APIs for lore taxonomy application

taxonomy.api.get_term(repo_slug, user_id, vocab_slug, term_slug)[source]

Get Term with existing slug, validating ownership for repo_slug and vocab_slug.

Parameters:term_id (int) – Term slug
Returns:Term – The Term with the id
Return type:Term
taxonomy.api.get_vocabulary(repo_slug, user_id, vocab_slug)[source]

Lookup vocabulary given its slug, using repo_slug to validate ownership.

Parameters:
  • repo_id (int) – Repository id
  • user_id (int) – User id
  • vocab_slug (unicode) – Vocabulary slug
Returns:

Vocabulary – The vocabulary from the database

Return type:

Vocabulary

Membership

Functions for handling roles.

roles.api.assign_user_to_repo_group(user, repo, group_type)[source]

Assign an user to a repo specific group type.

Parameters:
  • user (django.contrib.auth.models.User) – user
  • repo (learningresources.models.Repository) – repository used to extract the right group to use
  • group_type (roles.permissions.GroupTypes) – group string to be used to construct the group name
Returns:

None

roles.api.is_last_admin_in_repo(user, repo)[source]

Check if user is the last administrator in the repository. It does not check if the user is an actual administrator and in that case it will simply return False

Parameters:
  • user (django.contrib.auth.models.User) – user
  • repo (learningresources.models.Repository) – repository used to extract the right group to use
Returns:

bool

roles.api.list_users_in_repo(repo, base_group_type=None)[source]

List all the users in the repository groups. If the group type is specified, the list is limited to that group.

Parameters:
  • repo (learningresources.models.Repository) – repository used to extract the right group to use
  • base_group_type (unicode) – group type from roles.permissions.BaseGroupTypes
Returns:

list – list of users in one or all the repository groups

Return type:

list of roles.user_models.UserGroup

roles.api.remove_user_from_repo_group(user, repo, group_type)[source]

Remove an user from a repository specific group type.

Parameters:
  • user (django.contrib.auth.models.User) – user
  • repo (learningresources.models.Repository) – repository used to extract the right group to use
  • group_type (roles.permissions.GroupTypes) – group string to be used to construct the group name
Returns:

None

roles.api.roles_clear_repo_permissions(repo)[source]

Remove all the permissions a group has on a repository. :param repo: repository :type repo: learningresources.models.Repository

Returns:None
roles.api.roles_init_new_repo(repo)[source]

Create new groups for the repository.

It assumes that there are only 3 types of users:
  • administrator
  • curator
  • author
Parameters:repo (learningresources.models.Repository) – repository used to create groups and assign permissions to them
Returns:None
roles.api.roles_update_repo(repo, old_slug)[source]

Update the groups names for the repository.

Parameters:
  • repo (learningresources.models.Repository) – repository used to update groups and assign permissions to them
  • old_slug (unicode) – old slug string used to retrieve the groups that need to be renamed
Returns:

None