<commit_before>"""
Views to render a learner's achievements.
"""


from django.template.loader import render_to_string
from web_fragments.fragment import Fragment

from lms.djangoapps.certificates import api as certificate_api
from openedx.core.djangoapps.certificates.api import certificates_viewable_for_course
from openedx.core.djangoapps.content.course_overviews.models import CourseOverview
from openedx.core.djangoapps.plugin_api.views import EdxFragmentView


class LearnerAchievementsFragmentView(EdxFragmentView):
    """
    A fragment to render a learner's achievements.
    """
    def render_to_fragment(self, request, username=None, own_profile=False, **kwargs):
        """
        Renders the current learner's achievements.
        """
        course_certificates = self._get_ordered_certificates_for_user(request, username)
        context = {
            'course_certificates': course_certificates,
            'own_profile': own_profile,
            'disable_courseware_js': True,
        }
        if course_certificates or own_profile:
            html = render_to_string('learner_profile/learner-achievements-fragment.html', context)
            return Fragment(html)
        else:
            return None

    def _get_ordered_certificates_for_user(self, request, username):
        """
        Returns a user's certificates sorted by course name.
        """
        course_certificates = certificate_api.get_certificates_for_user(username)
        passing_certificates = []
        for course_certificate in course_certificates:
            if course_certificate.get('is_passing', False):
                course_key = course_certificate['course_key']
                try:<commit_msg>Remove pattern library of learner_achievements. (#24369)<commit_after>"""
Views to render a learner's achievements.
"""


from django.template.loader import render_to_string
from web_fragments.fragment import Fragment

from lms.djangoapps.certificates import api as certificate_api
from openedx.core.djangoapps.certificates.api import certificates_viewable_for_course
from openedx.core.djangoapps.content.course_overviews.models import CourseOverview
from openedx.core.djangoapps.plugin_api.views import EdxFragmentView


class LearnerAchievementsFragmentView(EdxFragmentView):
    """
    A fragment to render a learner's achievements.
    """
    _uses_pattern_library = False

    def render_to_fragment(self, request, username=None, own_profile=False, **kwargs):
        """
        Renders the current learner's achievements.
        """
        course_certificates = self._get_ordered_certificates_for_user(request, username)
        context = {
            'course_certificates': course_certificates,
            'own_profile': own_profile,
            'disable_courseware_js': True,
        }
        if course_certificates or own_profile:
            html = render_to_string('learner_profile/learner-achievements-fragment.html', context)
            return Fragment(html)
        else:
            return None

    def _get_ordered_certificates_for_user(self, request, username):
        """
        Returns a user's certificates sorted by course name.
        """
        course_certificates = certificate_api.get_certificates_for_user(username)
        passing_certificates = []
        for course_certificate in course_certificates:
            if course_certificate.get('is_passing', False):
                course_key = course_certificate['course_key']
                try:
{'fraction_non_alphanumeric': 0.07408466819221968, 'fraction_numerical': 0.0014302059496567505, 'mean_word_length': 3.3119605425400738, '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': '6986324', 'n_tokens_mistral': 948, 'n_tokens_neox': 917, 'n_words': 206}