<commit_before>"""
Views to show a course outline.
"""
import re
import datetime
import logging

from completion import waffle as completion_waffle
from django.contrib.auth.models import User
from django.template.context_processors import csrf
from django.template.loader import render_to_string
from opaque_keys.edx.keys import CourseKey
from pytz import UTC
from waffle.models import Switch
from web_fragments.fragment import Fragment

from courseware.courses import get_course_overview_with_access
from openedx.core.djangoapps.plugin_api.views import EdxFragmentView
from student.models import CourseEnrollment
from credo_modules.models import Organization

from util.milestones_helpers import get_course_content_milestones
from xmodule.modulestore.django import modulestore
from ..utils import get_course_outline_block_tree, get_resume_block
from rest_framework.reverse import reverse


DEFAULT_COMPLETION_TRACKING_START = datetime.datetime(2018, 1, 24, tzinfo=UTC)

log = logging.getLogger(__name__)


class CourseOutlineFragmentView(EdxFragmentView):
    """
    Course outline fragment to be shown in the unified course view.
    """

    def _convert_complete_status(self, status):
        if status == 'not_started':
            return 'begin', 'Begin'
        elif status == 'in_progress':
            return 'in-progress', 'In progress'
        elif status == 'finished':
            return 'complete', 'Completed!'
        return None, None

    def render_to_fragment(self, request, course_id=None, page_context=None, **kwargs):
        """
        Renders the course outline as a fragment.
        """
        course_key = CourseKey.from_string(course_id)
        course_overview = get_course_overview_with_access(request.user, 'load', course_key, check_if_enrolled=True)
        course = modulestore().get_course(course_key)
        template = 'course_experience/course-outline-fragment.html'

        course_block_tree = get_course_outline_block_tree(request, course_id)
        if not course_block_tree:
            return None

        enable_new_carousel_view = False
        try:
            org = Organization.objects.get(org=course.org)
            if org.org_type is not None:
                enable_new_carousel_view = org.org_type.enable_new_carousel_view
        except Organization.DoesNotExist:
            pass

        highlighted_blocks = []
        if enable_new_carousel_view:
            status_map = {}
            for num_sub, sub in enumerate(course_block_tree['children'], 1):
                num_completed = 0
                sub['num_children'] = len(sub['children'])
                sub['jump_to'] = reverse(
                        'jump_to',
                        kwargs={'course_id': unicode(course_key), 'location': sub['id']},
                    )
                for i in sub['children']:
                    status_map[i['id']] = i['complete_status']
                    if i['complete_status'] == 'finished':
                        num_completed += 1
                    i['jump_to'] = reverse(<commit_msg>fix "children" issue<commit_after>"""
Views to show a course outline.
"""
import re
import datetime
import logging

from completion import waffle as completion_waffle
from django.contrib.auth.models import User
from django.template.context_processors import csrf
from django.template.loader import render_to_string
from opaque_keys.edx.keys import CourseKey
from pytz import UTC
from waffle.models import Switch
from web_fragments.fragment import Fragment

from courseware.courses import get_course_overview_with_access
from openedx.core.djangoapps.plugin_api.views import EdxFragmentView
from student.models import CourseEnrollment
from credo_modules.models import Organization

from util.milestones_helpers import get_course_content_milestones
from xmodule.modulestore.django import modulestore
from ..utils import get_course_outline_block_tree, get_resume_block
from rest_framework.reverse import reverse


DEFAULT_COMPLETION_TRACKING_START = datetime.datetime(2018, 1, 24, tzinfo=UTC)

log = logging.getLogger(__name__)


class CourseOutlineFragmentView(EdxFragmentView):
    """
    Course outline fragment to be shown in the unified course view.
    """

    def _convert_complete_status(self, status):
        if status == 'not_started':
            return 'begin', 'Begin'
        elif status == 'in_progress':
            return 'in-progress', 'In progress'
        elif status == 'finished':
            return 'complete', 'Completed!'
        return None, None

    def render_to_fragment(self, request, course_id=None, page_context=None, **kwargs):
        """
        Renders the course outline as a fragment.
        """
        course_key = CourseKey.from_string(course_id)
        course_overview = get_course_overview_with_access(request.user, 'load', course_key, check_if_enrolled=True)
        course = modulestore().get_course(course_key)
        template = 'course_experience/course-outline-fragment.html'

        course_block_tree = get_course_outline_block_tree(request, course_id)
        if not course_block_tree:
            return None

        enable_new_carousel_view = False
        try:
            org = Organization.objects.get(org=course.org)
            if org.org_type is not None:
                enable_new_carousel_view = org.org_type.enable_new_carousel_view
        except Organization.DoesNotExist:
            pass

        highlighted_blocks = []
        if enable_new_carousel_view:
            status_map = {}
            for num_sub, sub in enumerate(course_block_tree.get('children', []), 1):
                num_completed = 0
                sub['num_children'] = len(sub.get('children', []))
                sub['jump_to'] = reverse(
                        'jump_to',
                        kwargs={'course_id': unicode(course_key), 'location': sub['id']},
                    )
                for i in sub.get('children', []):
                    status_map[i['id']] = i['complete_status']
                    if i['complete_status'] == 'finished':
                        num_completed += 1
                    i['jump_to'] = reverse(
{'fraction_non_alphanumeric': 0.08294031549845503, 'fraction_numerical': 0.0032525613920962758, 'mean_word_length': 2.841349156777014, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4552742', 'n_tokens_mistral': 1740, 'n_tokens_neox': 1656, 'n_words': 413}