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: 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"), text_type(args), text_type(cleaned_options)) # try to parse out the course from the serialized form course_id = CourseKey.from_string(options['course']) user = options['username'] 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 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 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'."), student.id, text_type(course_id)) else: LOGGER.info((u"Skipping certificate generation for " u"student %s in course '%s' " u"because the noop flag is set."), student.id, text_type(course_id)) LOGGER.info((u"Finished regenerating certificates command for " u"user %s and course '%s'."), student.id, text_type(course_id))
def test_regenerate_certificate_for_honor_mode(self, mock_generate_cert): """Test web certificate regeneration for the users who have earned the certificate in honor mode """ self.cert.mode = 'honor' self.cert.download_url = '' self.cert.save() with mock_passing_grade(percent=0.75): with patch('common.djangoapps.course_modes.models.CourseMode.mode_for_course') as mock_mode_for_course: mock_mode_for_course.return_value = 'honor' regenerate_user_certificates(self.student, self.course_key, course=self.course) mock_generate_cert.assert_called()
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" ), text_type(args), text_type(cleaned_options) ) # try to parse out the course from the serialized form course_id = CourseKey.from_string(options['course']) user = options['username'] 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 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, 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, text_type(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, text_type(course_id) ) LOGGER.info( ( u"Finished regenerating certificates command for " u"user %s and course '%s'." ), student.id, text_type(course_id) )
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: certificate = 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.")) # Deactivate certificate invalidation by setting active to False. _deactivate_invalidation(certificate) log.info( "Started regenerating certificates for user %s in course %s from the support page.", params["user"].id, params["course_key"] ) return HttpResponse(200)