'Generate a certificate for the student. If `generate_pdf` is True,
sends a request to XQueue.'

def _generate_cert(self, cert, course, student, grade_contents, template_pdf, generate_pdf):

    course_id = unicode(course.id)
    key = make_hashkey(random.random())
    cert.key = key
    contents = {'action': 'create', 'username': student.username, 'course_id': course_id, 'course_name': (course.display_name or course_id), 'name': cert.name, 'grade': grade_contents, 'template_pdf': template_pdf}
    if generate_pdf:
        cert.status = status.generating
    else:
        cert.status = status.downloadable
        cert.verify_uuid = uuid4().hex
    cert.save()
    if generate_pdf:
        try:
            self._send_to_xqueue(contents, key)
        except XQueueAddToQueueError as exc:
            cert.status = ExampleCertificate.STATUS_ERROR
            cert.error_reason = unicode(exc)
            cert.save()
            LOGGER.critical(u"Could    not    add    certificate    task    to    XQueue.        The    course    was    '%s'    and    the    student    was    '%s'.The    certificate    task    status    has    been    marked    as    'error'    and    can    be    re-submitted    with    a    management    command.", course_id, student.id)
        else:
            LOGGER.info(u"The    certificate    status    has    been    set    to    '%s'.        Sent    a    certificate    grading    task    to    the    XQueue    with    the    key    '%s'.    ", cert.status, key)
    return cert
