lms.djangoapps.discussion.django_comment_client package

Contents

lms.djangoapps.discussion.django_comment_client package#

Subpackages#

Submodules#

lms.djangoapps.discussion.django_comment_client.constants module#

Constants for Discussions

lms.djangoapps.discussion.django_comment_client.middleware module#

class lms.djangoapps.discussion.django_comment_client.middleware.AjaxExceptionMiddleware(get_response)#

Bases: MiddlewareMixin

Middleware that captures CommentClientRequestErrors during ajax requests and tranforms them into json responses

process_exception(request, exception)#

Processes CommentClientRequestErrors in ajax requests. If the request is an ajax request, returns a http response that encodes the error as json

lms.djangoapps.discussion.django_comment_client.models module#

lms.djangoapps.discussion.django_comment_client.permissions module#

Module for checking permissions with the comment_client backend

lms.djangoapps.discussion.django_comment_client.permissions.check_permissions_by_view(user, course_id, content, name, group_id=None, content_user_group=None)#
lms.djangoapps.discussion.django_comment_client.permissions.get_team(commentable_id)#

Returns the team that the commentable_id belongs to if it exists. Returns None otherwise.

lms.djangoapps.discussion.django_comment_client.settings module#

lms.djangoapps.discussion.django_comment_client.urls module#

Urls for the django_comment_client.

lms.djangoapps.discussion.django_comment_client.utils module#

exception lms.djangoapps.discussion.django_comment_client.utils.DiscussionIdMapIsNotCached#

Bases: Exception

Thrown when the discussion id map is not cached for this course, but an attempt was made to access it.

class lms.djangoapps.discussion.django_comment_client.utils.HtmlResponse(html='')#

Bases: HttpResponse

Django response object delivering HTML representations

class lms.djangoapps.discussion.django_comment_client.utils.JsonError(error_messages=[], status=400)#

Bases: HttpResponse

Django response object delivering JSON exceptions

class lms.djangoapps.discussion.django_comment_client.utils.JsonResponse(data=None)#

Bases: HttpResponse

Django response object delivering JSON representations

class lms.djangoapps.discussion.django_comment_client.utils.QueryCountDebugMiddleware(get_response)#

Bases: MiddlewareMixin

This middleware will log the number of queries run and the total time taken for each request (with a status code of 200). It does not currently support multi-db setups.

process_response(request, response)#

Log information for 200 OK responses as part of the outbound pipeline

class lms.djangoapps.discussion.django_comment_client.utils.ViewNameMiddleware(get_response)#

Bases: MiddlewareMixin

Django middleware object to inject view name into request context

process_view(request, view_func, view_args, view_kwargs)#

Injects the view name value into the request context

lms.djangoapps.discussion.django_comment_client.utils.add_courseware_context(content_list, course, user, id_map=None)#

Decorates content_list with courseware metadata using the discussion id map cache if available.

lms.djangoapps.discussion.django_comment_client.utils.convert_a_to_markdown(text)#

Convert <a> tags to Markdown format

lms.djangoapps.discussion.django_comment_client.utils.convert_html_to_markdown(text)#

Convert HTML to Markdown

lms.djangoapps.discussion.django_comment_client.utils.convert_img_to_markdown(text)#

Convert <img> tags to Markdown format

lms.djangoapps.discussion.django_comment_client.utils.convert_p_to_markdown(text)#
lms.djangoapps.discussion.django_comment_client.utils.course_discussion_division_enabled(course_discussion_settings)#

Are discussions divided for the course represented by this instance of course_discussion_settings? This method looks both at course_discussion_settings.division_scheme, and information about the course state itself (For example, are cohorts enabled? And are there multiple enrollment tracks?).

Parameters:

course_discussion_settings – CourseDiscussionSettings model instance

Returns: True if discussion division is enabled for the course, else False

lms.djangoapps.discussion.django_comment_client.utils.discussion_category_id_access(course, user, discussion_id, xblock=None)#

Returns True iff the given discussion_id is accessible for user in course. Assumes that the commentable identified by discussion_id has a null or ‘course’ context. Uses the discussion id cache if available, falling back to get_discussion_categories_ids if there is no cache.

lms.djangoapps.discussion.django_comment_client.utils.extend_content(content)#
lms.djangoapps.discussion.django_comment_client.utils.extract(dic, keys)#

Returns a subset of keys from the provided dictionary

lms.djangoapps.discussion.django_comment_client.utils.get_ability(course_id, content, user)#

Return a dictionary of forums-oriented actions and the user’s permission to perform them

lms.djangoapps.discussion.django_comment_client.utils.get_annotated_content_info(course_id, content, user, user_info)#

Get metadata for an individual content (thread or comment)

lms.djangoapps.discussion.django_comment_client.utils.get_annotated_content_infos(course_id, thread, user, user_info)#

Get metadata for a thread and its children

lms.djangoapps.discussion.django_comment_client.utils.get_cached_discussion_id_map(course, discussion_ids, user)#

Returns a dict mapping discussion_ids to respective discussion xblock metadata if it is cached and visible to the user. If not, returns the result of get_discussion_id_map

lms.djangoapps.discussion.django_comment_client.utils.get_cached_discussion_id_map_by_course_id(course_id, discussion_ids, user)#

Returns a dict mapping discussion_ids to respective discussion xblock metadata if it is cached and visible to the user. If not, returns the result of get_discussion_id_map

lms.djangoapps.discussion.django_comment_client.utils.get_cached_discussion_key(course_id, discussion_id)#

Returns the usage key of the discussion xblock associated with discussion_id if it is cached. If the discussion id map is cached but does not contain discussion_id, returns None. If the discussion id map is not cached for course, raises a DiscussionIdMapIsNotCached exception.

lms.djangoapps.discussion.django_comment_client.utils.get_discussion_category_map(course, user, divided_only_if_explicit=False, exclude_unstarted=True)#

Transform the list of this course’s discussion xblocks into a recursive dictionary structure. This is used to render the discussion category map in the discussion tab sidebar for a given user.

Parameters:
  • course – Course for which to get the ids.

  • user – User to check for access.

  • divided_only_if_explicit (bool) – If True, inline topics are marked is_divided only if they are explicitly listed in CourseDiscussionSettings.discussion_topics.

Example

>>> example = {
>>>               "entries": {
>>>                   "General": {
>>>                       "sort_key": "General",
>>>                       "is_divided": True,
>>>                       "id": "i4x-edx-eiorguegnru-course-foobarbaz"
>>>                   }
>>>               },
>>>               "children": [
>>>                     ["General", "entry"],
>>>                     ["Getting Started", "subcategory"]
>>>               ],
>>>               "subcategories": {
>>>                   "Getting Started": {
>>>                       "subcategories": {},
>>>                       "children": [
>>>                           ["Working with Videos", "entry"],
>>>                           ["Videos on edX", "entry"]
>>>                       ],
>>>                       "entries": {
>>>                           "Working with Videos": {
>>>                               "sort_key": None,
>>>                               "is_divided": False,
>>>                               "id": "d9f970a42067413cbb633f81cfb12604"
>>>                           },
>>>                           "Videos on edX": {
>>>                               "sort_key": None,
>>>                               "is_divided": False,
>>>                               "id": "98d8feb5971041a085512ae22b398613"
>>>                           }
>>>                       }
>>>                   }
>>>               }
>>>          }
lms.djangoapps.discussion.django_comment_client.utils.get_discussion_id_map(course, user)#

Transform the list of this course’s discussion xblocks (visible to a given user) into a dictionary of metadata keyed by discussion_id.

lms.djangoapps.discussion.django_comment_client.utils.get_discussion_id_map_by_course_id(course_id, user)#

Transform the list of this course’s discussion xblocks (visible to a given user) into a dictionary of metadata keyed by discussion_id.

lms.djangoapps.discussion.django_comment_client.utils.get_discussion_id_map_entry(xblock)#

Returns a tuple of (discussion_id, metadata) suitable for inclusion in the results of get_discussion_id_map().

lms.djangoapps.discussion.django_comment_client.utils.get_group_id_for_comments_service(request, course_key, commentable_id=None)#

Given a user requesting content within a commentable_id, determine the group_id which should be passed to the comments service.

Returns:

the group_id to pass to the comments service or None if nothing should be passed

Return type:

int

Raises:

ValueError if the requested group_id is invalid

lms.djangoapps.discussion.django_comment_client.utils.get_group_id_for_user(user, course_discussion_settings)#

Given a user, return the group_id for that user according to the course_discussion_settings. If discussions are not divided, this method will return None. It will also return None if the user is in no group within the specified division_scheme.

lms.djangoapps.discussion.django_comment_client.utils.get_group_id_for_user_from_cache(user, course_id)#

Caches the results of get_group_id_for_user, but serializes the course_id instead of the course_discussions_settings object as cache keys.

lms.djangoapps.discussion.django_comment_client.utils.get_group_name(group_id, course_discussion_settings)#

Given a specified comments_service group_id, returns the learner-facing name of the Group. If no such Group exists for the specified group_id (taking into account the division_scheme and course specified by course_discussion_settings), returns None. :param group_id: the group_id as used by the comments_service code :param course_discussion_settings: CourseDiscussionSettings model instance

Returns: learner-facing name of the Group, or None if no such group exists

lms.djangoapps.discussion.django_comment_client.utils.get_metadata_for_threads(course_id, threads, user, user_info)#

Returns annotated content information for the specified course, threads, and user information

lms.djangoapps.discussion.django_comment_client.utils.get_role_ids(course_id)#

Returns a dictionary having role names as keys and a list of users as values

lms.djangoapps.discussion.django_comment_client.utils.get_user_group_ids(course_id, content, user=None)#

Given a user, course ID, and the content of the thread or comment, returns the group ID for the current user and the user that posted the thread/comment.

lms.djangoapps.discussion.django_comment_client.utils.get_user_role_names(user: User, course_key: CourseKey) Set[str]#

Get a set of discussion roles a user has for the specified course.

Parameters:
  • user (User) – a user

  • course_key (CourseKey) – a course key

Returns:

(Set[str]) a set of role names that the user has.

lms.djangoapps.discussion.django_comment_client.utils.get_users_with_moderator_roles(context)#

Get all users within the course with moderator roles

lms.djangoapps.discussion.django_comment_client.utils.get_users_with_roles(roles, course_id)#

Get all users with specified roles for a course

lms.djangoapps.discussion.django_comment_client.utils.has_discussion_privileges(user, course_id)#

Returns True if the user is privileged in teams discussions for this course. The user must be one of Discussion Admin, Moderator, or Community TA.

Parameters:
  • user (User) – The user to check privileges for.

  • course_id (CourseKey) – A key for the course to check privileges for.

Returns:

bool

lms.djangoapps.discussion.django_comment_client.utils.has_forum_access(uname, course_id, rolename)#

Boolean operation which tests a user’s role-based permissions (not actually forums-specific)

lms.djangoapps.discussion.django_comment_client.utils.is_comment_too_deep(parent)#

Determine whether a comment with the given parent violates MAX_COMMENT_DEPTH

parent can be None to determine whether root comments are allowed

lms.djangoapps.discussion.django_comment_client.utils.is_commentable_divided(course_key, commentable_id, course_discussion_settings=None)#
Parameters:
  • course_key – CourseKey

  • commentable_id – string

  • course_discussion_settings – CourseDiscussionSettings model instance (optional). If not supplied, it will be retrieved via the course_key.

Returns:

is this commentable divided, meaning that learners are divided into groups (either Cohorts or Enrollment Tracks) and only see posts within their group?

Return type:

Bool

Raises:

Http404 if the course doesn't exist.

lms.djangoapps.discussion.django_comment_client.utils.is_content_authored_by(content, user)#

Return True if the author is this content is the passed user, else False

lms.djangoapps.discussion.django_comment_client.utils.is_discussion_enabled(course_id)#

Return True if discussions are enabled; else False

lms.djangoapps.discussion.django_comment_client.utils.is_user_community_ta(user, course_id)#

Boolean operation to check whether a user’s role is Community TA or not

lms.djangoapps.discussion.django_comment_client.utils.prepare_content(content, course_key, is_staff=False, is_community_ta=False, discussion_division_enabled=None, group_names_by_id=None)#

This function is used to pre-process thread and comment models in various ways before adding them to the HTTP response. This includes fixing empty attribute fields, enforcing author anonymity, and enriching metadata around group ownership and response endorsement.

@TODO: not all response pre-processing steps are currently integrated into this function.

Parameters:
  • content (dict) – A thread or comment.

  • course_key (CourseKey) – The course key of the course.

  • is_staff (bool) – Whether the user is a staff member.

  • is_community_ta (bool) – Whether the user is a TA (community or grpup community TA).

  • discussion_division_enabled (bool) – Whether division of course discussions is enabled. Note that callers of this method do not need to provide this value (it defaults to None)– it is calculated and then passed to recursive calls of this method.

lms.djangoapps.discussion.django_comment_client.utils.sanitize_body(body)#

Return a sanitized version of the body with dangerous Markdown removed.

This is possibly overly broad, and might tamper with legitimate posts that contain this code in fenced code blocks. As far as we can tell, this is an extra layer of protection, and current handling in the front end and using nh3 for HTML rendering on the server side should cover these cases.

lms.djangoapps.discussion.django_comment_client.utils.strip_blank(dic)#

Returns a dictionary stripped of any ‘blank’ (empty) keys

lms.djangoapps.discussion.django_comment_client.utils.strip_none(dic)#

Returns a dictionary stripped of any keys having values of None

Module contents#