Beispiel #1
0
def regenerate_certificate_for_user(request):
    """
    Regenerate certificates for a user.

    This is meant to be used by support staff through the UI in lms/djangoapps/support

    Arguments:
        request (HttpRequest): The request object

    Returns:
        HttpResponse

    Example Usage:

        POST /certificates/regenerate
            * username: "******"
            * course_key: "edX/DemoX/Demo_Course"

        Response: 200 OK

    """
    # Check the POST parameters, returning a 400 response if they're not valid.
    params, response = _validate_post_params(request.POST)
    if response is not None:
        return response

    # Check that the course exists
    course = modulestore().get_course(params["course_key"])
    if course is None:
        msg = _("The course {course_key} does not exist").format(course_key=params["course_key"])
        return HttpResponseBadRequest(msg)

    # Check that the user is enrolled in the course
    if not CourseEnrollment.is_enrolled(params["user"], params["course_key"]):
        msg = _("User {username} is not enrolled in the course {course_key}").format(
            username=params["user"].username,
            course_key=params["course_key"]
        )
        return HttpResponseBadRequest(msg)

    # Attempt to regenerate certificates
    try:
        api.regenerate_user_certificates(params["user"], params["course_key"], course=course)
    except:  # pylint: disable=bare-except
        # We are pessimistic about the kinds of errors that might get thrown by the
        # certificates API.  This may be overkill, but we're logging everything so we can
        # track down unexpected errors.
        log.exception(
            "Could not regenerate certificates for user %s in course %s",
            params["user"].id,
            params["course_key"]
        )
        return HttpResponseServerError(_("An unexpected error occurred while regenerating certificates."))

    log.info(
        "Started regenerating certificates for user %s in course %s from the support page.",
        params["user"].id, params["course_key"]
    )
    return HttpResponse(200)
    def handle(self, *args, **options):

        # Scrub the username from the log message
        cleaned_options = copy.copy(options)
        if 'username' in cleaned_options:
            cleaned_options['username'] = '******'
        LOGGER.info((u"Starting to create tasks to regenerate certificates "
                     u"with arguments %s and options %s"), unicode(args),
                    unicode(cleaned_options))

        if options['course']:
            # try to parse out the course from the serialized form
            course_id = CourseKey.from_string(options['course'])
        else:
            raise CommandError("You must specify a course")

        user = options['username']
        if not (course_id and user):
            raise CommandError(
                'both course id and student username are required')

        student = None
        if '@' in user:
            student = User.objects.get(email=user,
                                       courseenrollment__course_id=course_id)
        else:
            student = User.objects.get(username=user,
                                       courseenrollment__course_id=course_id)

        course = modulestore().get_course(course_id, depth=2)

        if not options['noop']:
            LOGGER.info(
                (u"Adding task to the XQueue to generate a certificate "
                 u"for student %s in course '%s'."), student.id, course_id)

            if course.issue_badges:
                badge_class = get_completion_badge(course_id, student)
                badge = badge_class.get_for_user(student)

                if badge:
                    badge.delete()
                    LOGGER.info(u"Cleared badge for student %s.", student.id)

            # Add the certificate request to the queue
            ret = regenerate_user_certificates(
                student,
                course_id,
                course=course,
                forced_grade=options['grade_value'],
                template_file=options['template_file'],
                insecure=options['insecure'])

            LOGGER.info(
                (u"Added a certificate regeneration task to the XQueue "
                 u"for student %s in course '%s'. "
                 u"The new certificate status is '%s'."), student.id,
                unicode(course_id), ret)

        else:
            LOGGER.info((u"Skipping certificate generation for "
                         u"student %s in course '%s' "
                         u"because the noop flag is set."), student.id,
                        unicode(course_id))

        LOGGER.info((u"Finished regenerating certificates command for "
                     u"user %s and course '%s'."), student.id,
                    unicode(course_id))
Beispiel #3
0
    def handle(self, *args, **options):

        # Scrub the username from the log message
        cleaned_options = copy.copy(options)
        if 'username' in cleaned_options:
            cleaned_options['username'] = '******'
        LOGGER.info(
            (
                u"Starting to create tasks to regenerate certificates "
                u"with arguments %s and options %s"
            ),
            unicode(args),
            unicode(cleaned_options)
        )

        if options['course']:
            # try to parse out the course from the serialized form
            try:
                course_id = CourseKey.from_string(options['course'])
            except InvalidKeyError:
                LOGGER.warning(
                    (
                        u"Course id %s could not be parsed as a CourseKey; "
                        u"falling back to SlashSeparatedCourseKey.from_deprecated_string()"
                    ),
                    options['course']
                )
                course_id = SlashSeparatedCourseKey.from_deprecated_string(options['course'])
        else:
            raise CommandError("You must specify a course")

        user = options['username']
        if not (course_id and user):
            raise CommandError('both course id and student username are required')

        student = None
        if '@' in user:
            student = User.objects.get(email=user, courseenrollment__course_id=course_id)
        else:
            student = User.objects.get(username=user, courseenrollment__course_id=course_id)

        course = modulestore().get_course(course_id, depth=2)

        designation = options['designation']

        if 'openedx.stanford.djangoapps.register_cme' in settings.INSTALLED_APPS:
            designation = options['designation'] or ExtraInfo.lookup_professional_designation(student)

        if not options['noop']:
            LOGGER.info(
                (
                    u"Adding task to the XQueue to generate a certificate "
                    u"for student %s in course '%s'."
                ),
                student.id,
                course_id
            )

            if badges_enabled() and course.issue_badges:
                badge_class = get_completion_badge(course_id, student)
                badge = badge_class.get_for_user(student)

                if badge:
                    badge.delete()
                    LOGGER.info(u"Cleared badge for student %s.", student.id)

            # Add the certificate request to the queue
            ret = regenerate_user_certificates(
                student, course_id, course=course,
                designation=designation,
                forced_grade=options['grade_value'],
                template_file=options['template_file'],
                insecure=options['insecure']
            )

            LOGGER.info(
                (
                    u"Added a certificate regeneration task to the XQueue "
                    u"for student %s in course '%s'. "
                    u"The new certificate status is '%s'."
                ),
                student.id,
                unicode(course_id),
                ret
            )

        else:
            LOGGER.info(
                (
                    u"Skipping certificate generation for "
                    u"student %s in course '%s' "
                    u"because the noop flag is set."
                ),
                student.id,
                unicode(course_id)
            )

        LOGGER.info(
            (
                u"Finished regenerating certificates command for "
                u"user %s and course '%s'."
            ),
            student.id,
            unicode(course_id)
        )
    def handle(self, *args, **options):

        # Scrub the username from the log message
        cleaned_options = copy.copy(options)
        if "username" in cleaned_options:
            cleaned_options["username"] = "******"
        LOGGER.info(
            (u"Starting to create tasks to regenerate certificates " u"with arguments %s and options %s"),
            unicode(args),
            unicode(cleaned_options),
        )

        if options["course"]:
            # try to parse out the course from the serialized form
            try:
                course_id = CourseKey.from_string(options["course"])
            except InvalidKeyError:
                LOGGER.warning(
                    (
                        u"Course id %s could not be parsed as a CourseKey; "
                        u"falling back to SlashSeparatedCourseKey.from_deprecated_string()"
                    ),
                    options["course"],
                )
                course_id = SlashSeparatedCourseKey.from_deprecated_string(options["course"])
        else:
            raise CommandError("You must specify a course")

        user = options["username"]
        if not (course_id and user):
            raise CommandError("both course id and student username are required")

        student = None
        if "@" in user:
            student = User.objects.get(email=user, courseenrollment__course_id=course_id)
        else:
            student = User.objects.get(username=user, courseenrollment__course_id=course_id)

        course = modulestore().get_course(course_id, depth=2)

        if not options["noop"]:
            LOGGER.info(
                (u"Adding task to the XQueue to generate a certificate " u"for student %s in course '%s'."),
                student.id,
                course_id,
            )

            if course.issue_badges:
                badge_class = get_completion_badge(course_id, student)
                badge = badge_class.get_for_user(student)

                if badge:
                    badge.delete()
                    LOGGER.info(u"Cleared badge for student %s.", student.id)

            # Add the certificate request to the queue
            ret = regenerate_user_certificates(
                student,
                course_id,
                course=course,
                forced_grade=options["grade_value"],
                template_file=options["template_file"],
                insecure=options["insecure"],
            )

            LOGGER.info(
                (
                    u"Added a certificate regeneration task to the XQueue "
                    u"for student %s in course '%s'. "
                    u"The new certificate status is '%s'."
                ),
                student.id,
                unicode(course_id),
                ret,
            )

        else:
            LOGGER.info(
                (
                    u"Skipping certificate generation for "
                    u"student %s in course '%s' "
                    u"because the noop flag is set."
                ),
                student.id,
                unicode(course_id),
            )

        LOGGER.info(
            (u"Finished regenerating certificates command for " u"user %s and course '%s'."),
            student.id,
            unicode(course_id),
        )