'Request a new certificate for a student.
Arguments:
student   - User.object
course_id - courseenrollment.course_id (CourseKey)
forced_grade - a string indicating a grade parameter to pass with
the certificate request. If this is given, grading
will be skipped.
generate_pdf - Boolean should a message be sent in queue to generate certificate PDF
Will change the certificate status to \'generating\' or
`downloadable` in case of web view certificates.
The course must not be a CCX.
Certificate must be in the \'unavailable\', \'error\',
\'deleted\' or \'generating\' state.
If a student has a passing grade or is in the whitelist
table for the course a request will be made for a new cert.
If a student has allow_certificate set to False in the
userprofile table the status will change to \'restricted\'
If a student does not have a passing grade the status
will change to status.notpassing
Returns the newly created certificate instance'

def add_cert(self, student, course_id, course=None, forced_grade=None, template_file=None, generate_pdf=True):

    if hasattr(course_id, 'ccx'):
        LOGGER.warning(u"Cannot    create    certificate    generation    task    for    user    %s    in    the    course    '%s';    certificates    are    not    allowed    for    CCX    courses.", student.id, unicode(course_id))
        return None
    valid_statuses = [status.generating, status.unavailable, status.deleted, status.error, status.notpassing, status.downloadable, status.auditing, status.audit_passing, status.audit_notpassing]
    cert_status = certificate_status_for_student(student, course_id)['status']
    cert = None
    if (cert_status not in valid_statuses):
        LOGGER.warning(u"Cannot    create    certificate    generation    task    for    user    %s    in    the    course    '%s';    the    certificate    status    '%s'    is    not    one    of    %s.", student.id, unicode(course_id), cert_status, unicode(valid_statuses))
        return None
    if (course is None):
        course = modulestore().get_course(course_id, depth=0)
    profile = UserProfile.objects.get(user=student)
    profile_name = profile.name
    self.request.user = student
    self.request.session = {}
    is_whitelisted = self.whitelist.filter(user=student, course_id=course_id, whitelist=True).exists()
    course_grade = CourseGradeFactory().create(student, course)
    (enrollment_mode, __) = CourseEnrollment.enrollment_mode_for_user(student, course_id)
    mode_is_verified = (enrollment_mode in GeneratedCertificate.VERIFIED_CERTS_MODES)
    user_is_verified = SoftwareSecurePhotoVerification.user_is_verified(student)
    cert_mode = enrollment_mode
    is_eligible_for_certificate = (is_whitelisted or CourseMode.is_eligible_for_certificate(enrollment_mode))
    unverified = False
    if (cert_mode == CourseMode.CREDIT_MODE):
        cert_mode = CourseMode.VERIFIED
    if (template_file is not None):
        template_pdf = template_file
    elif (mode_is_verified and user_is_verified):
        template_pdf = 'certificate-template-{id.org}-{id.course}-verified.pdf'.format(id=course_id)
    elif (mode_is_verified and (not user_is_verified)):
        template_pdf = 'certificate-template-{id.org}-{id.course}.pdf'.format(id=course_id)
        if CourseMode.mode_for_course(course_id, CourseMode.HONOR):
            cert_mode = GeneratedCertificate.MODES.honor
        else:
            unverified = True
    else:
        template_pdf = 'certificate-template-{id.org}-{id.course}.pdf'.format(id=course_id)
    LOGGER.info(u'Certificate    generated    for    student    %s    in    the    course:    %s    with    template:    %s.    given    template:    %s,    user    is    verified:    %s,    mode    is    verified:    %s', student.username, unicode(course_id), template_pdf, template_file, user_is_verified, mode_is_verified)
    (cert, created) = GeneratedCertificate.objects.get_or_create(user=student, course_id=course_id)
    cert.mode = cert_mode
    cert.user = student
    cert.grade = course_grade.percent
    cert.course_id = course_id
    cert.name = profile_name
    cert.download_url = ''
    grade_contents = (forced_grade or course_grade.letter_grade)
    try:
        grade_contents = lxml.html.fromstring(grade_contents).text_content()
        passing = True
    except (TypeError, XMLSyntaxError, ParserError) as exc:
        LOGGER.info(u"Could    not    retrieve    grade    for    student    %s    in    the    course    '%s'    because    an    exception    occurred    while    parsing    the    grade    contents    '%s'    as    HTML.    The    exception    was:    '%s'", student.id, unicode(course_id), grade_contents, unicode(exc))
        if is_whitelisted:
            LOGGER.info(u"Student    %s    is    whitelisted    in    '%s'", student.id, unicode(course_id))
            passing = True
        else:
            passing = False
    cutoff = settings.AUDIT_CERT_CUTOFF_DATE
    if ((cutoff and (cert.created_date >= cutoff)) and (not is_eligible_for_certificate)):
        cert.status = (CertificateStatuses.audit_passing if passing else CertificateStatuses.audit_notpassing)
        cert.save()
        LOGGER.info(u'Student    %s    with    enrollment    mode    %s    is    not    eligible    for    a    certificate.', student.id, enrollment_mode)
        return cert
    elif (not passing):
        cert.status = status.notpassing
        cert.save()
        LOGGER.info(u"Student    %s    does    not    have    a    grade    for    '%s',    so    their    certificate    status    has    been    set    to    '%s'.    No    certificate    generation    task    was    sent    to    the    XQueue.", student.id, unicode(course_id), cert.status)
        return cert
    if self.restricted.filter(user=student).exists():
        cert.status = status.restricted
        cert.save()
        LOGGER.info(u"Student    %s    is    in    the    embargoed    country    restricted    list,    so    their    certificate    status    has    been    set    to    '%s'    for    the    course    '%s'.    No    certificate    generation    task    was    sent    to    the    XQueue.", student.id, cert.status, unicode(course_id))
        return cert
    if unverified:
        cert.status = status.unverified
        cert.save()
        LOGGER.info(u'User    %s    has    a    verified    enrollment    in    course    %s    but    is    missing    ID    verification.    Certificate    status    has    been    set    to    unverified', student.id, unicode(course_id))
        return cert
    return self._generate_cert(cert, course, student, grade_contents, template_pdf, generate_pdf)
