def retire_user_comments(user): """ Retire the user's discussion comments """ try: CCUser.from_django_user(user).retire('Deleted user') except CommentClientRequestError as e: # Ignore error if discussion user does not exist if e.status_code != 404: raise
def get_context(course, request, thread=None): """ Returns a context appropriate for use with ThreadSerializer or (if thread is provided) CommentSerializer. """ # TODO: cache staff_user_ids and ta_user_ids if we need to improve perf staff_user_ids = { user.id for role in Role.objects.filter( name__in=[FORUM_ROLE_ADMINISTRATOR, FORUM_ROLE_MODERATOR], course_id=course.id ) for user in role.users.all() } ta_user_ids = { user.id for role in Role.objects.filter(name=FORUM_ROLE_COMMUNITY_TA, course_id=course.id) for user in role.users.all() } requester = request.user cc_requester = CommentClientUser.from_django_user(requester).retrieve() cc_requester["course_id"] = course.id course_discussion_settings = get_course_discussion_settings(course.id) return { "course": course, "request": request, "thread": thread, "discussion_division_enabled": course_discussion_division_enabled(course_discussion_settings), "group_ids_to_names": get_group_names_by_id(course_discussion_settings), "is_requester_privileged": requester.id in staff_user_ids or requester.id in ta_user_ids, "staff_user_ids": staff_user_ids, "ta_user_ids": ta_user_ids, "cc_requester": cc_requester, }
def get_context(course, request, thread=None): """ Returns a context appropriate for use with ThreadSerializer or (if thread is provided) CommentSerializer. """ # TODO: cache staff_user_ids and ta_user_ids if we need to improve perf staff_user_ids = { user.id for role in Role.objects.filter( name__in=[FORUM_ROLE_ADMINISTRATOR, FORUM_ROLE_MODERATOR], course_id=course.id ) for user in role.users.all() } ta_user_ids = { user.id for role in Role.objects.filter(name=FORUM_ROLE_COMMUNITY_TA, course_id=course.id) for user in role.users.all() } requester = request.user cc_requester = CommentClientUser.from_django_user(requester).retrieve() cc_requester["course_id"] = course.id course_discussion_settings = get_course_discussion_settings(course.id) return { "course": course, "request": request, "thread": thread, "discussion_division_enabled": course_discussion_division_enabled(course_discussion_settings), "group_ids_to_names": get_group_names_by_id(course_discussion_settings), "is_requester_privileged": requester.id in staff_user_ids or requester.id in ta_user_ids, "staff_user_ids": staff_user_ids, "ta_user_ids": ta_user_ids, "cc_requester": cc_requester, }
def _get_context( requester_id, is_requester_privileged, is_cohorted=False, thread=None, allow_anonymous=True, allow_anonymous_to_peers=False, ): """Return a context suitable for testing the permissions module""" return { "cc_requester": User(id=requester_id), "is_requester_privileged": is_requester_privileged, "course": CourseFactory( cohort_config={"cohorted": is_cohorted}, allow_anonymous=allow_anonymous, allow_anonymous_to_peers=allow_anonymous_to_peers, ), "discussion_division_enabled": is_cohorted, "thread": thread, }
def _get_social_stats(self, course_key, end_date=None, thread_type=None, thread_ids=None): """ Gets social stats for course with specified filter parameters """ return { int(user_id): data for user_id, data in User.all_social_stats( str(course_key), end_date=end_date, thread_type=thread_type, thread_ids=thread_ids).items() }
def perform_username_updates(self, username_update_list): updated = [] failed = [] for username_update_dict in username_update_list: old_username = username_update_dict["old_username"] new_username = username_update_dict["new_username"] try: user = User.objects.get(username=new_username) comment_user = CommentUser.from_django_user(user) update_comment_user_username(comment_user, new_username) enrolled_course_ids = get_enrolled_course_ids(user) authored_items = get_authored_threads_and_comments(comment_user, enrolled_course_ids) update_count = 0 for authored_item in authored_items: item_id = authored_item["id"] item_type = str(authored_item.get("type")) if item_type == THREAD_TYPE: update_thread_username(item_id, new_username) update_count += 1 elif item_type == COMMENT_TYPE: update_comment_username(item_id, new_username) update_count += 1 updated.append({ "old_username": old_username, "new_username": new_username, "updated_item_count": update_count, }) except Exception as exc: failed.append({ "old_username": old_username, "new_username": new_username, "exception": str(exc) }) return { "updated": updated, "failed": failed, }