def remove_orphans_and_recalculate_progress(course_key, detached_categories):
    from contentstore.views.item import _delete_orphans
    from xmodule.modulestore import ModuleStoreEnum
    from .signals import is_valid_progress_module

    _delete_orphans(course_key, ModuleStoreEnum.UserID.mgmt_command, True)

    users = CourseEnrollment.objects.users_enrolled_in(course_key)

    detached_categories_list = [
        Q(content_id__contains=item.strip()) for item in detached_categories
    ]
    detached_categories_list = reduce(lambda a, b: a | b,
                                      detached_categories_list)

    for user in users:
        completions = CourseModuleCompletion.objects.filter(course_id=course_key, user_id=user.id)\
            .exclude(detached_categories_list).values_list('content_id', flat=True).distinct()

        num_completions = sum([
            is_valid_progress_module(content_id=content_id)
            for content_id in completions
        ])
        try:
            progress_record = StudentProgress.objects.get(user=user,
                                                          course_id=course_key)

            if progress_record.completions != num_completions:
                progress_record.completions = num_completions
                progress_record.save()

        except StudentProgress.DoesNotExist:
            pass
Beispiel #2
0
    def handle(self, *args, **options):
        if len(args) not in {1, 2}:
            raise CommandError("delete_orphans requires one or more arguments: <course_id> |commit|")

        try:
            course_key = CourseKey.from_string(args[0])
        except InvalidKeyError:
            raise CommandError("Invalid course key.")

        commit = False
        if len(args) == 2:
            commit = args[1] == 'commit'

        if commit:
            print 'Deleting orphans from the course:'
            deleted_items = _delete_orphans(
                course_key, ModuleStoreEnum.UserID.mgmt_command, commit
            )
            print "Success! Deleted the following orphans from the course:"
            print "\n".join(deleted_items)
        else:
            print 'Dry run. The following orphans would have been deleted from the course:'
            deleted_items = _delete_orphans(
                course_key, ModuleStoreEnum.UserID.mgmt_command, commit
            )
            print "\n".join(deleted_items)
def remove_orphans(course_key, detached_categories):
    from contentstore.views.item import _delete_orphans
    from xmodule.modulestore import ModuleStoreEnum

    _delete_orphans(
        course_key, ModuleStoreEnum.UserID.mgmt_command, True
    )
    def handle(self, *args, **options):
        try:
            course_key = CourseKey.from_string(options['course_id'])
        except InvalidKeyError:
            raise CommandError("Invalid course key.")

        if options['commit']:
            print 'Deleting orphans from the course:'
            deleted_items = _delete_orphans(
                course_key, ModuleStoreEnum.UserID.mgmt_command, options['commit']
            )
            print "Success! Deleted the following orphans from the course:"
            print "\n".join(deleted_items)
        else:
            print 'Dry run. The following orphans would have been deleted from the course:'
            deleted_items = _delete_orphans(
                course_key, ModuleStoreEnum.UserID.mgmt_command, options['commit']
            )
            print "\n".join(deleted_items)
Beispiel #5
0
    def handle(self, *args, **options):
        try:
            course_key = CourseKey.from_string(options['course_id'])
        except InvalidKeyError:
            raise CommandError("Invalid course key.")

        if options['commit']:
            print 'Deleting orphans from the course:'
            deleted_items = _delete_orphans(
                course_key, ModuleStoreEnum.UserID.mgmt_command,
                options['commit'])
            print "Success! Deleted the following orphans from the course:"
            print "\n".join(deleted_items)
        else:
            print 'Dry run. The following orphans would have been deleted from the course:'
            deleted_items = _delete_orphans(
                course_key, ModuleStoreEnum.UserID.mgmt_command,
                options['commit'])
            print "\n".join(deleted_items)