示例#1
0
 def test_slug(self):
     """
     Verify slug generation is working as expected. If this test fails, the algorithm has changed, and it will cause
     the handler to lose track of all badges it made in the past.
     """
     assert course_complete.course_slug(self.course_key, 'honor') ==\
            'course-v1edxcourse_testtest_run_honor_2055051'
     assert course_complete.course_slug(self.course_key, 'verified') ==\
            'course-v1edxcourse_testtest_run_verified_d550ad7'
 def test_slug(self):
     """
     Verify slug generation is working as expected. If this test fails, the algorithm has changed, and it will cause
     the handler to lose track of all badges it made in the past.
     """
     self.assertEqual(course_complete.course_slug(self.course_key, 'honor'),
                      'edxcourse_testtest_run_honor_fc5519b')
     self.assertEqual(
         course_complete.course_slug(self.course_key, 'verified'),
         'edxcourse_testtest_run_verified_a199ec0')
def forwards(apps, schema_editor):
    """
    Migrate the initial badge classes, assertions, and course image configurations from lms.djangoapps.certificates.
    """
    from django.core.files.base import ContentFile
    from xmodule.modulestore.django import modulestore
    from lms.djangoapps.badges.events import course_complete
    db_alias = schema_editor.connection.alias
    # This will need to be changed if badges/certificates get moved out of the default db for some reason.
    if db_alias != 'default':
        return
    classes = {}
    OldBadgeAssertion = apps.get_model("certificates", "BadgeAssertion")
    BadgeImageConfiguration = apps.get_model("certificates",
                                             "BadgeImageConfiguration")
    BadgeAssertion = apps.get_model("badges", "BadgeAssertion")
    BadgeClass = apps.get_model("badges", "BadgeClass")
    CourseCompleteImageConfiguration = apps.get_model(
        "badges", "CourseCompleteImageConfiguration")
    for badge in OldBadgeAssertion.objects.all():
        if (badge.course_id, badge.mode) not in classes:
            course = modulestore().get_course(badge.course_id)
            image_config = BadgeImageConfiguration.objects.get(mode=badge.mode)
            icon = image_config.icon
            badge_class = BadgeClass(
                display_name=course.display_name,
                criteria=course_complete.evidence_url(badge.user_id,
                                                      badge.course_id),
                description=course_complete.badge_description(
                    course, badge.mode),
                slug=course_complete.course_slug(badge.course_id, badge.mode),
                mode=image_config.mode,
                course_id=badge.course_id,
            )
            badge_class._meta.get_field('image').generate_filename = \
                lambda inst, fn: os.path.join('badge_classes', fn)
            badge_class.image.name = icon.name
            badge_class.save()
            classes[(badge.course_id, badge.mode)] = badge_class
        if isinstance(badge.data, six.string_types):
            data = badge.data
        else:
            data = json.dumps(badge.data)
        assertion = BadgeAssertion(
            user_id=badge.user_id,
            badge_class=classes[(badge.course_id, badge.mode)],
            data=data,
            backend='BadgrBackend',
            image_url=badge.data['image'],
            assertion_url=badge.data['json']['id'],
        )
        assertion.save()
        # Would be overwritten by the first save.
        assertion.created = datetime.fromtimestamp(
            # Later versions of badgr include microseconds, but they aren't certain to be there.
            time.mktime(
                time.strptime(badge.data['created_at'].split('.')[0],
                              "%Y-%m-%dT%H:%M:%S")))
        assertion.save()

    for configuration in BadgeImageConfiguration.objects.all():
        new_conf = CourseCompleteImageConfiguration(
            default=configuration.default,
            mode=configuration.mode,
        )
        new_conf.icon.name = configuration.icon.name
        new_conf.save()