def test_uncovered_unicode_cert_name(): """ Fail to generate a certificate for a user with unsupported unicode character in name """ course_id = settings.CERT_DATA.keys()[0] cert = CertificateGen(course_id) (download_uuid, verify_uuid, download_url) = cert.create_and_upload(u"Memphis \u0007", upload=False)
def test_cert_names(): """Generate certificates for all names in NAMES without saving or uploading""" # XXX: This is meant to catch unicode rendering problems, but does it? course_id = settings.CERT_DATA.keys()[0] for name in NAMES: cert = CertificateGen(course_id) (download_uuid, verify_uuid, download_url) = cert.create_and_upload(name, upload=False)
def test_cert_upload(): """Check here->S3->http round trip.""" if not settings.CERT_AWS_ID or not settings.CERT_AWS_KEY: raise SkipTest cert = CertificateGen(settings.CERT_DATA.keys()[0]) (download_uuid, verify_uuid, download_url) = cert.create_and_upload('John Smith') r = urllib2.urlopen(download_url) with tempfile.NamedTemporaryFile(delete=True) as f: f.write(r.read())
def test_cert_names(): """ Generates certificates for all names in NAMES Deletes them when finished, doesn't upload to S3 """ for course_id in settings.CERT_DATA.keys(): for name in NAMES: cert = CertificateGen(course_id) (download_uuid, verify_uuid, download_url) = cert.create_and_upload( name, upload=False)
def test_cert_upload(): """ Ensures that we can upload a certificate to S3 and that it can subsequently be downloaded via http """ skip_if_not_configured() cert = CertificateGen(settings.CERT_DATA.keys()[0], settings.CERT_AWS_ID, settings.CERT_AWS_KEY) (download_uuid, verify_uuid, download_url) = cert.create_and_upload( 'John Smith') r = urllib2.urlopen(download_url) with tempfile.NamedTemporaryFile(delete=True) as f: f.write(r.read())
def test_cert_upload(): """ Ensures that we can upload a certificate to S3 and that it can subsequently be downloaded via http """ cert = CertificateGen( settings.CERT_DATA.keys()[0], settings.CERT_AWS_ID, settings.CERT_AWS_KEY ) (download_uuid, verify_uuid, download_url) = cert.create_and_upload( 'John Smith') r = urllib2.urlopen(download_url) with tempfile.NamedTemporaryFile(delete=True) as f: f.write(r.read())
def test_cert_gen(): """Do end-to-end generation test (sans s3) for every course. For every course: * Generates a single dummy certificate * Verifies all file artificats are created * Verifies the pdf signature against the detached signature * Publishes the certificate to a temporary directory """ for course_id in settings.CERT_DATA.keys(): tmpdir = tempfile.mkdtemp() cert = CertificateGen(course_id) (download_uuid, verify_uuid, download_url) = cert.create_and_upload('John Smith', upload=False, copy_to_webroot=True, cert_web_root=tmpdir, cleanup=True) # If the settings we're testing have VERIFY turned off, skip those tests, too if settings.CERT_DATA[course_id].get('VERIFY', True) and verify_uuid: verify_files = os.listdir( os.path.join(tmpdir, S3_VERIFY_PATH, verify_uuid)) download_files = os.listdir( os.path.join(tmpdir, S3_CERT_PATH, download_uuid)) # All the verification files were created correctly assert_true(set(verify_files) == VERIFY_FILES) # The detached signature is valid pdf = os.path.join(tmpdir, S3_CERT_PATH, download_uuid, CERT_FILENAME) sig = os.path.join(tmpdir, S3_VERIFY_PATH, verify_uuid, CERT_FILESIG) gpg = gnupg.GPG(homedir=settings.CERT_GPG_DIR) with open(pdf) as f: v = gpg.verify_file(f, sig) assert_true(v is not None and v.trust_level >= v.TRUST_FULLY) # And of course we have a download file, right? assert_true(set(download_files) == DOWNLOAD_FILES) # Remove files if os.path.exists(tmpdir): shutil.rmtree(tmpdir)
def test_render_flair_function_false(): """ Make sure flair rendering function behaves properly when it is NOT given flair to render """ cert = CertificateGen(settings.CERT_DATA.keys()[0]) overlay_pdf_buffer = StringIO.StringIO() page = canvas.Canvas(overlay_pdf_buffer, pagesize=landscape(A4)) flair = [] assert_false(draw_flair(cert, flair, 'top', page, context=None))
def test_designation(): """ Generate a test certificate with designation text """ designations = ['PharmD', 'MD'] for course_id in settings.CERT_DATA.keys(): for designation in designations: tmpdir = tempfile.mkdtemp() cert = CertificateGen(course_id, designation=designation) for name in NAMES: (download_uuid, verify_uuid, download_url) = cert.create_and_upload( name, upload=False, copy_to_webroot=True, cert_web_root=tmpdir, cleanup=True, ) if os.path.exists(tmpdir): shutil.rmtree(tmpdir)
def test_cert_gen(): """ For every course: * Generates a single dummy certificate * Verifies all file artificats are created * Verifies the pdf signature against the detached signature * Publishes the certificate to a temporary directory """ for course_id in settings.CERT_DATA.keys(): tmpdir = tempfile.mkdtemp() cert = CertificateGen(course_id) (download_uuid, verify_uuid, download_url) = cert.create_and_upload( 'John Smith', upload=False, copy_to_webroot=True, cert_web_root=tmpdir, cleanup=True) verify_files = os.listdir( os.path.join(tmpdir, S3_VERIFY_PATH, verify_uuid)) download_files = os.listdir( os.path.join(tmpdir, S3_CERT_PATH, download_uuid)) # Verify that all files are generated assert_true(set(verify_files) == set(VERIFY_FILES)) assert_true(set(download_files) == set(DOWNLOAD_FILES)) # Verify that the detached signature is valid pdf = os.path.join( tmpdir, S3_CERT_PATH, download_uuid, 'Certificate.pdf' ) sig = os.path.join( tmpdir, S3_VERIFY_PATH, verify_uuid, 'Certificate.pdf.sig' ) gpg = gnupg.GPG(gnupghome=settings.CERT_GPG_DIR) with open(sig) as f: v = gpg.verify_file(f, pdf) assert_true(v is not None and v.trust_level >= v.TRUST_FULLY) # Remove files if os.path.exists(tmpdir): shutil.rmtree(tmpdir)
def test_subtemplates(self): """ Generates certficates with subtemplates """ tmpdir = tempfile.mkdtemp() for course_id in self.subtemplate_courses: cert_data = settings.CERT_DATA[course_id] subtemplates = cert_data['subtemplates'] for designation, course_id in subtemplates.iteritems(): cert = CertificateGen(course_id, designation=designation) for name in NAMES: (download_uuid, verify_uuid, download_url) = cert.create_and_upload( name, upload=False, copy_to_webroot=True, cert_web_root=tmpdir, cleanup=True, ) if os.path.exists(tmpdir): shutil.rmtree(tmpdir)
def test_designation(): """ Generate a test certificate with designation text """ designations = ['PharmD', 'MD'] for course_id in settings.CERT_DATA.keys(): for designation in designations: tmpdir = tempfile.mkdtemp() cert = CertificateGen(course_id, designation=designation) for name in NAMES: (download_uuid, verify_uuid, download_url) = cert.create_and_upload( name, upload=False, copy_to_webroot=True, cert_web_root=tmpdir, cleanup=True, ) try: shutil.rmtree(tmpdir) except: # pragma: no cover pass
def test_subtemplates(self): """ Generates certficates with subtemplates """ tmpdir = tempfile.mkdtemp() for course_id in self.subtemplate_courses: cert_data = settings.CERT_DATA[course_id] subtemplates = cert_data['subtemplates'] for designation, course_id in subtemplates.iteritems(): cert = CertificateGen(course_id, designation=designation) for name in NAMES: (download_uuid, verify_uuid, download_url) = cert.create_and_upload( name, upload=False, copy_to_webroot=True, cert_web_root=tmpdir, cleanup=True, ) try: shutil.rmtree(tmpdir) except: # pragma: no cover pass
def test_cert_gen(): """Do end-to-end generation test (sans s3) for every course. For every course: * Generates a single dummy certificate * Verifies all file artificats are created * Verifies the pdf signature against the detached signature * Publishes the certificate to a temporary directory """ for course_id in settings.CERT_DATA.keys(): tmpdir = tempfile.mkdtemp() cert = CertificateGen(course_id) (download_uuid, verify_uuid, download_url) = cert.create_and_upload( 'John Smith', upload=False, copy_to_webroot=True, cert_web_root=tmpdir, cleanup=True) # If the settings we're testing have VERIFY turned off, skip those tests, too if settings.CERT_DATA[course_id].get('VERIFY', True) and verify_uuid: verify_files = os.listdir(os.path.join(tmpdir, S3_VERIFY_PATH, verify_uuid)) download_files = os.listdir(os.path.join(tmpdir, S3_CERT_PATH, download_uuid)) # All the verification files were created correctly assert_true(set(verify_files) == VERIFY_FILES) # The detached signature is valid pdf = os.path.join(tmpdir, S3_CERT_PATH, download_uuid, CERT_FILENAME) sig = os.path.join(tmpdir, S3_VERIFY_PATH, verify_uuid, CERT_FILESIG) gpg = gnupg.GPG(gnupghome=settings.CERT_GPG_DIR) with open(sig) as f: v = gpg.verify_file(f, pdf) assert_true(v is not None and v.trust_level >= v.TRUST_FULLY) # And of course we have a download file, right? assert_true(set(download_files) == DOWNLOAD_FILES) # Remove files if os.path.exists(tmpdir): shutil.rmtree(tmpdir)
def test_cert_gen(): """ For every course: * Generates a single dummy certificate * Verifies all file artificats are created * Verifies the pdf signature against the detached signature """ skip_if_not_configured() for course_id in settings.CERT_DATA.keys(): cert = CertificateGen(course_id) (download_uuid, verify_uuid, download_url) = cert.create_and_upload( 'John Smith', upload=False, cleanup=False) verify_files = os.listdir( os.path.join(cert.dir_prefix, S3_VERIFY_PATH, verify_uuid)) download_files = os.listdir( os.path.join(cert.dir_prefix, S3_CERT_PATH, download_uuid)) # Verify that all files are generated assert_true(set(verify_files) == set(VERIFY_FILES)) assert_true(set(download_files) == set(DOWNLOAD_FILES)) # Verify that the detached signature is valid pdf = os.path.join(cert.dir_prefix, S3_CERT_PATH, download_uuid, 'Certificate.pdf') sig = os.path.join(cert.dir_prefix, S3_VERIFY_PATH, verify_uuid, 'Certificate.pdf.sig') gpg = gnupg.GPG(gnupghome=settings.CERT_GPG_DIR) with open(sig) as f: v = gpg.verify_file(f, pdf) assert_true(v is not None and v.trust_level >= v.TRUST_FULLY) # Remove files if os.path.exists(cert.dir_prefix): shutil.rmtree(cert.dir_prefix)
def test_creates_default_dir(gen_dir): """Make sure the certificate generator creates the default directory if it doesn't exist.""" gen = None try: assert_true(os.path.exists(gen_dir)) shutil.rmtree(gen_dir) assert_false(os.path.exists(gen_dir)) gen = CertificateGen(list(settings.CERT_DATA.keys())[0]) assert_true(os.path.exists(gen.dir_prefix)) finally: if os.path.exists(gen_dir): shutil.rmtree(gen_dir) if gen: # Avoid catastrophy assert_true(gen.dir_prefix.startswith(gen_dir)) shutil.rmtree(gen.dir_prefix)
def test_render_flair_function_true(): """ Make sure flair rendering function behaves properly when it IS given flair to render """ cert = CertificateGen(settings.CERT_DATA.keys()[0]) overlay_pdf_buffer = StringIO.StringIO() page = canvas.Canvas(overlay_pdf_buffer, pagesize=landscape(A4)) flair = [{ 'image': { 'x': 575, 'y': 325, 'width': 125, 'height': 125, 'file': 'images/test_flair.png' } }] assert_true(draw_flair(cert, flair, 'top', page, context=None))
def main(): """ Generates some pfds using each template for different names for review in a pdf viewer. Will copy out the pdfs into the certs/ dir """ pdf_dir = "/var/tmp/gen_certs" copy_dir = "/var/tmp/certs" if args.output_file: # ensure we can open the output file output_f = open(args.output_file, 'aw') # Remove files if they exist for d in [pdf_dir, copy_dir]: if os.path.exists(d): shutil.rmtree(d) if not os.path.exists(copy_dir): os.makedirs(copy_dir) certificate_data = [] if args.course_id: course_list = [args.course_id] else: course_list = settings.CERT_DATA.keys() for course in course_list: if args.name: name_list = [args.name] elif args.input_file: with open(args.input_file) as f: name_list = [line.rstrip() for line in f.readlines()] else: name_list = NAMES for name in name_list: cert = CertificateGen( course, args.template_file, aws_id=settings.CERT_AWS_ID, aws_key=settings.CERT_AWS_KEY, dir_prefix=pdf_dir, long_org=args.long_org, long_course=args.long_course, issued_date=args.issued_date, ) (download_uuid, verify_uuid, download_url) = cert.create_and_upload( name, upload=True, cleanup=False) certificate_data.append([name, download_url]) gen_dir = os.path.join( cert.dir_prefix, S3_CERT_PATH, download_uuid) copy_dest = '{copy_dir}/{course}-{name}.pdf'.format( copy_dir=copy_dir, name=name.replace(" ", "-").replace("/", "-"), course=course.replace("/", "-")) shutil.copyfile('{0}/Certificate.pdf'.format(gen_dir), unicode(copy_dest.decode('utf-8'))) print "Created {0}".format(copy_dest) for row in certificate_data: print '\t'.join(row) if args.output_file: certificate_writer = csv.writer(output_f, quoting=csv.QUOTE_MINIMAL) for row in certificate_data: certificate_writer.writerow(row) output_f.close()
def main(): manager = XQueuePullManager(settings.QUEUE_URL, settings.QUEUE_NAME, settings.QUEUE_AUTH_USER, settings.QUEUE_AUTH_PASS, settings.QUEUE_USER, settings.QUEUE_PASS) while True: if manager.get_length() == 0: log.debug("{0} has no jobs".format(str(manager))) time.sleep(SLEEP_TIME) continue else: log.debug('queue length: {0}'.format(manager.get_length())) xqueue_body = {} xqueue_header = '' action = '' username = '' course_id = '' template_pdf = None name = '' certdata = manager.get_submission() log.debug('xqueue response: {0}'.format(certdata)) try: xqueue_body = json.loads(certdata['xqueue_body']) xqueue_header = json.loads(certdata['xqueue_header']) action = xqueue_body['action'] username = xqueue_body['username'] course_id = xqueue_body['course_id'] template_pdf = xqueue_body.get('template_pdf', None) name = xqueue_body['name'] cert = CertificateGen(course_id, template_pdf, aws_id=args.aws_id, aws_key=args.aws_key) if action in ['remove', 'regen']: cert.delete_certificate(xqueue_body['delete_download_uuid'], xqueue_body['delete_verify_uuid']) if action in ['remove']: continue except (TypeError, ValueError, KeyError) as e: log.critical('Unable to parse queue response submission ({0}) : {1}'.format(e, certdata)) if settings.DEBUG: raise else: continue try: log.info('Generating certificate for {0} ({1}), in {2}'.format( username.encode('utf-8'), name.encode('utf-8'), course_id.encode('utf-8'))) (download_uuid, verify_uuid, download_url) = cert.create_and_upload(name.encode('utf-8')) except Exception as e: # global exception handler, if anything goes wrong # during the generation of the pdf we will let the LMS # know so it can be re-submitted, the LMS will update # the state to error # get as much info as possible about the exception # for the post back to the LMS exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] error_reason = '({0} {1}) {2}: {3} : {4}:{5}'.format( username, course_id, exc_type, e, fname, exc_tb.tb_lineno) log.critical('An error occurred during certificate generation ' '{0}'.format(error_reason)) xqueue_reply = {'xqueue_header': json.dumps(xqueue_header), 'xqueue_body': json.dumps({ 'error': 'There was an error processing' 'the certificate request : {0}'.format(e), 'username': username, 'course_id': course_id, 'error_reason': error_reason, }) } manager.respond(xqueue_reply) if settings.DEBUG: raise else: continue # post result back to the LMS xqueue_reply = {'xqueue_header': json.dumps(xqueue_header), 'xqueue_body': json.dumps({ 'action': action, 'download_uuid': download_uuid, 'verify_uuid': verify_uuid, 'username': username, 'course_id': course_id, 'url': download_url, })} log.info("Posting result to the LMS: {0}".format(xqueue_reply)) manager.respond(xqueue_reply)
def main(): """ Generates some pfds using each template for different names for review in a pdf viewer. Will copy out the pdfs into the certs/ dir """ pdf_dir = TMP_GEN_DIR copy_dir = TMP_GEN_DIR + "+copy" # Remove files if they exist for d in [pdf_dir, copy_dir]: if os.path.exists(d): shutil.rmtree(d) if not os.path.exists(copy_dir): os.makedirs(copy_dir) certificate_data = [] if args.course_id: course_list = [args.course_id] else: course_list = settings.CERT_DATA.keys() upload_files = not args.no_upload for course in course_list: if args.name: name_list = [args.name] elif args.input_file: with open(args.input_file) as f: name_list = [line.rstrip() for line in f.readlines()] else: name_list = NAMES for name in name_list: cert = CertificateGen( course, args.template_file, aws_id=settings.CERT_AWS_ID, aws_key=settings.CERT_AWS_KEY, dir_prefix=pdf_dir, long_org=args.long_org, long_course=args.long_course, issued_date=args.issued_date, ) title = None if args.assign_title: title = random.choice(stanford_cme_titles)[0] print "assigning random title", name, title grade = None if args.grade_text: grade = args.grade_text (download_uuid, verify_uuid, download_url) = cert.create_and_upload(name, upload=upload_files, copy_to_webroot=False, cleanup=False, designation=title, grade=grade) certificate_data.append((name, course, args.long_org, args.long_course, download_url)) gen_dir = os.path.join(cert.dir_prefix, S3_CERT_PATH, download_uuid) copy_dest = '{copy_dir}/{course}-{name}.pdf'.format( copy_dir=copy_dir, name=name.replace(" ", "-").replace("/", "-"), course=course.replace("/", "-")) try: shutil.copyfile('{0}/{1}'.format(gen_dir, TARGET_FILENAME), unicode(copy_dest.decode('utf-8'))) except Exception, msg: # Sometimes we have problems finding or creating the files to be copied; # the following lines help us debug this case print msg print "%s\n%s\n%s\n%s\n%s\n%s" % (name, download_uuid, verify_uuid, download_uuid, gen_dir, copy_dest) raise print "Created {0}".format(copy_dest)
def main(): """ Generates some pfds using each template for different names for review in a pdf viewer. Will copy out the pdfs into the certs/ dir """ pdf_dir = TMP_GEN_DIR copy_dir = TMP_GEN_DIR + "+copy" # Remove files if they exist for d in [pdf_dir, copy_dir]: if os.path.exists(d): shutil.rmtree(d) if not os.path.exists(copy_dir): os.makedirs(copy_dir) certificate_data = [] if args.course_id: course_id = args.course_id long_course = args.long_course or course_id course_list = [(course_id, args.designation, long_course)] else: # Compile course list based on openedx-certificates/cert-data.yml # gen_cert.py should and will fail if certificates-templates/cert-data.yml is used course_list = [] for course_id, course_template in settings.CERT_DATA.iteritems(): course_list.append((course_id, None, course_id)) designations = course_template.get('designations', {}) for key, value in designations.iteritems(): for title in value['titles']: course_list.append((course_id, title, course_id)) subtemplates = course_template.get('subtemplates', {}) for title in subtemplates.keys(): course_list.append((course_id, title, course_id)) upload_files = not args.no_upload for course, designation, long_course in course_list: if args.name: name_list = [args.name] elif args.input_file: with open(args.input_file) as f: name_list = [line.rstrip() for line in f.readlines()] else: name_list = NAMES for name in name_list: cert = CertificateGen( course, args.template_file, aws_id=settings.CERT_AWS_ID, aws_key=settings.CERT_AWS_KEY, dir_prefix=pdf_dir, long_org=args.long_org, long_course=args.long_course, issued_date=args.issued_date, designation=designation, ) grade = None if args.grade_text: grade = args.grade_text (download_uuid, verify_uuid, download_url) = cert.create_and_upload(name, upload=upload_files, copy_to_webroot=False, cleanup=False, grade=grade) certificate_data.append((name, course, args.long_org, args.long_course, download_url)) gen_dir = os.path.join(cert.dir_prefix, S3_CERT_PATH, download_uuid) # Remove non-ascii chars from filename before saving locally (This is not the production filename) name = ''.join([i if ord(i) < 128 else ' ' for i in name]) suffix = '' if designation: suffix = "_{designation}".format( designation=designation, ) copy_dest = '{copy_dir}/{course}-{name}{suffix}.pdf'.format( copy_dir=copy_dir, name=name.replace(" ", "-").replace("/", "-"), suffix=suffix, course=course.replace("/", "-")) try: shutil.copyfile('{0}/{1}'.format(gen_dir, TARGET_FILENAME), unicode(copy_dest.decode('utf-8'))) except Exception, msg: # Sometimes we have problems finding or creating the files to be copied; # the following lines help us debug this case print msg print "%s\n%s\n%s\n%s\n%s\n%s" % (name, download_uuid, verify_uuid, download_uuid, gen_dir, copy_dest) raise print "Created {0}".format(copy_dest)
def main(): manager = XQueuePullManager(settings.QUEUE_URL, settings.QUEUE_NAME, settings.QUEUE_AUTH_USER, settings.QUEUE_AUTH_PASS, settings.QUEUE_USER, settings.QUEUE_PASS) last_course = None # The last course_id we generated for cert = None # A CertificateGen instance for a particular course while True: if manager.get_length() == 0: log.debug("{0} has no jobs".format(str(manager))) time.sleep(SLEEP_TIME) continue else: log.debug('queue length: {0}'.format(manager.get_length())) xqueue_body = {} xqueue_header = '' action = '' username = '' grade = None course_id = '' course_name = '' template_pdf = None name = '' certdata = manager.get_submission() log.debug('xqueue response: {0}'.format(certdata)) try: xqueue_body = json.loads(certdata['xqueue_body']) xqueue_header = json.loads(certdata['xqueue_header']) action = xqueue_body['action'] username = xqueue_body['username'] course_id = xqueue_body['course_id'] course_name = xqueue_body['course_name'] name = xqueue_body['name'] template_pdf = xqueue_body.get('template_pdf', None) grade = xqueue_body.get('grade', None) issued_date = xqueue_body.get('issued_date', None) designation = xqueue_body.get('designation', None) if last_course != course_id: cert = CertificateGen( course_id, template_pdf, aws_id=args.aws_id, aws_key=args.aws_key, #long_course=course_name.encode('utf-8'), issued_date=issued_date, ) last_course = course_id if action in ['remove', 'regen']: cert.delete_certificate(xqueue_body['delete_download_uuid'], xqueue_body['delete_verify_uuid']) if action in ['remove']: continue except (TypeError, ValueError, KeyError, IOError) as e: log.critical('Unable to parse queue submission ({0}) : {1}'.format(e, certdata)) if settings.DEBUG: raise else: continue try: log.info( "Generating certificate for {username} ({name}), " "in {course_id}, with grade {grade}".format( username=username.encode('utf-8'), name=name.encode('utf-8'), course_id=course_id.encode('utf-8'), grade=grade, ) ) (download_uuid, verify_uuid, download_url) = cert.create_and_upload(name.encode('utf-8') , grade=grade, designation=designation) # cert.create_and_upload(name.encode('utf-8') , grade=grade, designation=designation) #try: # pass except Exception as e: # global exception handler, if anything goes wrong # during the generation of the pdf we will let the LMS # know so it can be re-submitted, the LMS will update # the state to error # get as much info as possible about the exception # for the post back to the LMS exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] error_reason = ( "({username} {course_id}) " "{exception_type}: {exception}: " "{file_name}:{line_number}".format( username=username, course_id=course_id, exception_type=exc_type, exception=e, file_name=fname, line_number=exc_tb.tb_lineno ) ) log.critical(traceback.format_exc()) log.critical( 'An error occurred during certificate generation {reason}'.format( reason=error_reason, ) ) xqueue_reply = { 'xqueue_header': json.dumps(xqueue_header), 'xqueue_body': json.dumps({ 'error': 'There was an error processing the certificate request: {error}'.format( error=e, ), 'username': username, 'course_id': course_id, 'error_reason': error_reason, }), } manager.respond(xqueue_reply) if settings.DEBUG: raise else: continue # post result back to the LMS xqueue_reply = { 'xqueue_header': json.dumps(xqueue_header), 'xqueue_body': json.dumps({ 'action': action, 'download_uuid': download_uuid, 'verify_uuid': verify_uuid, 'username': username, 'course_id': course_id, 'url': download_url, }), } log.info("Posting result to the LMS: {0}".format(xqueue_reply)) manager.respond(xqueue_reply)
def main(): """ Generates some pfds using each template for different names for review in a pdf viewer. Will copy out the pdfs into the certs/ dir """ pdf_dir = "/var/tmp/gen_certs" copy_dir = "/var/tmp/certs" if args.output_file: # ensure we can open the output file output_f = open(args.output_file, 'aw') # Remove files if they exist for d in [pdf_dir, copy_dir]: if os.path.exists(d): shutil.rmtree(d) if not os.path.exists(copy_dir): os.makedirs(copy_dir) certificate_data = [] if args.course_id: course_list = [args.course_id] else: course_list = settings.CERT_DATA.keys() for course in course_list: if args.name: name_list = [args.name] elif args.input_file: with open(args.input_file) as f: name_list = [line.rstrip() for line in f.readlines()] else: name_list = NAMES for name in name_list: cert = CertificateGen( course, args.template_file, aws_id=settings.CERT_AWS_ID, aws_key=settings.CERT_AWS_KEY, dir_prefix=pdf_dir, long_org=args.long_org, long_course=args.long_course, issued_date=args.issued_date, ) (download_uuid, verify_uuid, download_url) = cert.create_and_upload( name, upload=True, copy_to_webroot=False, cleanup=False) certificate_data.append([name, download_url]) gen_dir = os.path.join( cert.dir_prefix, S3_CERT_PATH, download_uuid) copy_dest = '{copy_dir}/{course}-{name}.pdf'.format( copy_dir=copy_dir, name=name.replace(" ", "-").replace("/", "-"), course=course.replace("/", "-")) shutil.copyfile('{0}/Certificate.pdf'.format(gen_dir), unicode(copy_dest.decode('utf-8'))) print "Created {0}".format(copy_dest) for row in certificate_data: print '\t'.join(row) if args.output_file: certificate_writer = csv.writer(output_f, quoting=csv.QUOTE_MINIMAL) for row in certificate_data: certificate_writer.writerow(row) output_f.close()
def main(): """ Generates some pfds using each template for different names for review in a pdf viewer. Will copy out the pdfs into the certs/ dir """ pdf_dir = TMP_GEN_DIR copy_dir = TMP_GEN_DIR + "+copy" # Remove files if they exist for d in [pdf_dir, copy_dir]: if os.path.exists(d): shutil.rmtree(d) if not os.path.exists(copy_dir): os.makedirs(copy_dir) certificate_data = [] if args.course_id: course_id = args.course_id long_course = args.long_course or course_id course_list = [(course_id, args.designation, long_course)] else: # Compile course list based on openedx-certificates/cert-data.yml # gen_cert.py should and will fail if certificates-templates/cert-data.yml is used course_list = [] for course_id, course_template in settings.CERT_DATA.iteritems(): course_list.append((course_id, None, course_id)) designations = course_template.get('designations', {}) for key, value in designations.iteritems(): for title in value['titles']: course_list.append((course_id, title, course_id)) subtemplates = course_template.get('subtemplates', {}) for title in subtemplates.keys(): course_list.append((course_id, title, course_id)) upload_files = not args.no_upload for course, designation, long_course in course_list: if args.name: name_list = [args.name] elif args.input_file: with open(args.input_file) as f: name_list = [line.rstrip() for line in f.readlines()] else: name_list = NAMES for name in name_list: cert = CertificateGen( course, args.template_file, aws_id=settings.CERT_AWS_ID, aws_key=settings.CERT_AWS_KEY, dir_prefix=pdf_dir, long_org=args.long_org, long_course=args.long_course, issued_date=args.issued_date, designation=designation, ) grade = None if args.grade_text: grade = args.grade_text (download_uuid, verify_uuid, download_url) = cert.create_and_upload(name, upload=upload_files, copy_to_webroot=False, cleanup=False, grade=grade) certificate_data.append( (name, course, args.long_org, args.long_course, download_url)) gen_dir = os.path.join(cert.dir_prefix, S3_CERT_PATH, download_uuid) # Remove non-ascii chars from filename before saving locally (This is not the production filename) name = ''.join([i if ord(i) < 128 else ' ' for i in name]) suffix = '' if designation: suffix = "_{designation}".format(designation=designation, ) copy_dest = '{copy_dir}/{course}-{name}{suffix}.pdf'.format( copy_dir=copy_dir, name=name.replace(" ", "-").replace("/", "-"), suffix=suffix, course=course.replace("/", "-")) try: shutil.copyfile('{0}/{1}'.format(gen_dir, TARGET_FILENAME), unicode(copy_dest.decode('utf-8'))) except Exception, msg: # Sometimes we have problems finding or creating the files to be copied; # the following lines help us debug this case print msg print "%s\n%s\n%s\n%s\n%s\n%s" % (name, download_uuid, verify_uuid, download_uuid, gen_dir, copy_dest) raise print "Created {0}".format(copy_dest)
def main(): manager = XQueuePullManager(settings.QUEUE_URL, settings.QUEUE_NAME, settings.QUEUE_AUTH_USER, settings.QUEUE_AUTH_PASS, settings.QUEUE_USER, settings.QUEUE_PASS) while True: if manager.get_length() == 0: log.debug("{0} has no jobs".format(str(manager))) time.sleep(settings.QUEUE_POLL_FREQUENCY) continue else: log.debug('queue length: {0}'.format(manager.get_length())) certdata = manager.get_submission() log.debug('xqueue response: {0}'.format(certdata)) try: xqueue_body = json.loads(certdata['xqueue_body']) xqueue_header = json.loads(certdata['xqueue_header']) action = xqueue_body['action'] username = xqueue_body['username'].encode('utf-8') course_id = xqueue_body['course_id'].encode('utf-8') course_name = xqueue_body['course_name'].encode('utf-8') name = xqueue_body['name'].encode('utf-8') template_pdf = xqueue_body.get('template_pdf', None) grade = xqueue_body.get('grade', None) if grade: grade = grade.encode('utf-8') issued_date = xqueue_body.get('issued_date', None) designation = xqueue_body.get('designation', None) if designation: designation = designation.encode('utf-8') cert = CertificateGen( course_id, template_pdf, aws_id=args.aws_id, aws_key=args.aws_key, long_course=course_name, issued_date=issued_date, designation=designation, ) except (TypeError, ValueError, KeyError, IOError) as e: log.critical('Unable to parse queue submission ({0}) : {1}'.format( e, certdata)) if settings.DEBUG: raise else: continue try: log.info( "Generating certificate for {username} ({name}), " "in {course_id}, with grade {grade} and designation {designation}" .format( username=username, name=name, course_id=course_id, grade=grade, designation=designation, )) (download_uuid, verify_uuid, download_url) = cert.create_and_upload(name, grade=grade) except Exception as e: # global exception handler, if anything goes wrong # during the generation of the pdf we will let the LMS # know so it can be re-submitted, the LMS will update # the state to error # get as much info as possible about the exception # for the post back to the LMS exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] error_reason = ("({username} {course_id}) " "{exception_type}: {exception}: " "{file_name}:{line_number}".format( username=username, course_id=course_id, exception_type=exc_type, exception=e, file_name=fname, line_number=exc_tb.tb_lineno, )) log.critical( 'An error occurred during certificate generation {reason}'. format(reason=error_reason, )) xqueue_reply = { 'xqueue_header': json.dumps(xqueue_header), 'xqueue_body': json.dumps({ 'error': 'There was an error processing the certificate request: {error}' .format(error=e, ), 'username': username, 'course_id': course_id, 'error_reason': error_reason, }), } manager.respond(xqueue_reply) if settings.DEBUG: raise else: continue # post result back to the LMS xqueue_reply = { 'xqueue_header': json.dumps(xqueue_header), 'xqueue_body': json.dumps({ 'action': action, 'download_uuid': download_uuid, 'verify_uuid': verify_uuid, 'username': username, 'course_id': course_id, 'url': download_url, }), } log.info("Posting result to the LMS: {0}".format(xqueue_reply)) manager.respond(xqueue_reply)
def main(): """ Generates samples of the Berkeley 188x letterhead Will copy out the pdfs into the letterheads/ dir """ pdf_dir = "/var/tmp/gen_letterheads" copy_dir = "/var/tmp/letterheads" # Remove files if they exist for d in [pdf_dir, copy_dir]: if os.path.exists(d): shutil.rmtree(d) if not os.path.exists(copy_dir): os.makedirs(copy_dir) download_urls = [] # only CS188.1x has the letterhead course = 'BerkeleyX/CS188.1x/2012_Fall' if args.file: with open(args.file, 'rb') as f: dist = json.loads(f.read()) new_dist = [] letterhead = CertificateGen( course, settings.CERT_AWS_ID, settings.CERT_AWS_KEY, dir_prefix=pdf_dir) for entry in dist: print entry['name'] (download_uuid, verify_uuid, download_url) = letterhead.create_and_upload( entry['name'].encode('utf-8'), upload=True, cleanup=True, letterhead=True) entry.update({'url': download_url}) new_dist.append(entry) with open('/tmp/distinguished_data', 'wb') as f: f.write(json.dumps(new_dist)) else: for name in NAMES: letterhead = CertificateGen( course, settings.CERT_AWS_ID, settings.CERT_AWS_KEY, dir_prefix=pdf_dir) (download_uuid, verify_uuid, download_url) = letterhead.create_and_upload( name, upload=True, cleanup=False, letterhead=True) download_urls.append(download_url) gen_dir = os.path.join( letterhead.dir_prefix, S3_CERT_PATH, download_uuid) copy_dest = '{copy_dir}/{course}-{name}.pdf'.format( copy_dir=copy_dir, name=name.replace(" ", "-").replace("/", "-"), course=course.replace("/", "-")) shutil.copyfile('{0}/distinction-letter.pdf'.format(gen_dir), unicode(copy_dest.decode('utf-8'))) print "Created {0}".format(copy_dest) print "\n".join(download_urls)
def main(): manager = XQueuePullManager(settings.QUEUE_URL, settings.QUEUE_NAME, settings.QUEUE_AUTH_USER, settings.QUEUE_AUTH_PASS, settings.QUEUE_USER, settings.QUEUE_PASS) last_course = None # The last course_id we generated for cert = None # A CertificateGen instance for a particular course while True: if manager.get_length() == 0: log.debug("{0} has no jobs".format(str(manager))) time.sleep(SLEEP_TIME) continue else: log.debug('queue length: {0}'.format(manager.get_length())) xqueue_body = {} xqueue_header = '' action = '' username = '' grade = None course_id = '' course_name = '' template_pdf = None name = '' certdata = manager.get_submission() log.debug('xqueue response: {0}'.format(certdata)) try: xqueue_body = json.loads(certdata['xqueue_body']) xqueue_header = json.loads(certdata['xqueue_header']) action = xqueue_body['action'] username = xqueue_body['username'] course_id = xqueue_body['course_id'] course_name = xqueue_body['course_name'] name = xqueue_body['name'] template_pdf = xqueue_body.get('template_pdf', None) grade = xqueue_body.get('grade', None) issued_date = xqueue_body.get('issued_date', None) designation = xqueue_body.get('designation', None) if last_course != course_id: cert = CertificateGen( course_id, template_pdf, aws_id=args.aws_id, aws_key=args.aws_key, long_course=course_name.encode('utf-8'), issued_date=issued_date, ) last_course = course_id if action in ['remove', 'regen']: cert.delete_certificate(xqueue_body['delete_download_uuid'], xqueue_body['delete_verify_uuid']) if action in ['remove']: continue except (TypeError, ValueError, KeyError, IOError) as e: log.critical('Unable to parse queue submission ({0}) : {1}'.format( e, certdata)) if settings.DEBUG: raise else: continue try: log.info("Generating certificate for {username} ({name}), " "in {course_id}, with grade {grade}".format( username=username.encode('utf-8'), name=name.encode('utf-8'), course_id=course_id.encode('utf-8'), grade=grade, )) (download_uuid, verify_uuid, download_url) = cert.create_and_upload(name.encode('utf-8'), grade=grade, designation=designation) except Exception as e: # global exception handler, if anything goes wrong # during the generation of the pdf we will let the LMS # know so it can be re-submitted, the LMS will update # the state to error # get as much info as possible about the exception # for the post back to the LMS exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] error_reason = ("({username} {course_id}) " "{exception_type}: {exception}: " "{file_name}:{line_number}".format( username=username, course_id=course_id, exception_type=exc_type, exception=e, file_name=fname, line_number=exc_tb.tb_lineno, )) log.critical( 'An error occurred during certificate generation {reason}'. format(reason=error_reason, )) xqueue_reply = { 'xqueue_header': json.dumps(xqueue_header), 'xqueue_body': json.dumps({ 'error': 'There was an error processing the certificate request: {error}' .format(error=e, ), 'username': username, 'course_id': course_id, 'error_reason': error_reason, }), } manager.respond(xqueue_reply) if settings.DEBUG: raise else: continue # post result back to the LMS xqueue_reply = { 'xqueue_header': json.dumps(xqueue_header), 'xqueue_body': json.dumps({ 'action': action, 'download_uuid': download_uuid, 'verify_uuid': verify_uuid, 'username': username, 'course_id': course_id, 'url': download_url, }), } log.info("Posting result to the LMS: {0}".format(xqueue_reply)) try: with open( '/edx/var/certs/constancias_generadas/constancias_curso_' + course_id.replace('/', '_'), 'a+') as file: file.write( '%s,%s,%s,%s \n' % (username, download_uuid, verify_uuid, download_url)) file.close() log.info("Save data into backup file: {status}".format( status='/edx/var/certs/constancias_generadas/constancias_curso_' + course_id.replace('/', '_'))) except Exception as e: log.critical( 'Backup certificate data: An error occurred during certificate generation {reason}' .format(reason=e, )) manager.respond(xqueue_reply)
def main(): """ Generates some pfds using each template for different names for review in a pdf viewer. Will copy out the pdfs into the certs/ dir """ pdf_dir = TMP_GEN_DIR copy_dir = TMP_GEN_DIR + "+copy" # Remove files if they exist for d in [pdf_dir, copy_dir]: if os.path.exists(d): shutil.rmtree(d) if not os.path.exists(copy_dir): os.makedirs(copy_dir) certificate_data = [] if args.course_id: course_list = [args.course_id] else: course_list = list(settings.CERT_DATA.keys()) upload_files = not args.no_upload for course in course_list: if args.name: name_list = [args.name] elif args.input_file: with open(args.input_file) as f: name_list = [line.rstrip() for line in f.readlines()] else: name_list = NAMES for name in name_list: cert = CertificateGen( course, args.template_file, aws_id=settings.CERT_AWS_ID, aws_key=settings.CERT_AWS_KEY, dir_prefix=pdf_dir, long_org=args.long_org, long_course=args.long_course, issued_date=args.issued_date, ) title = None if args.assign_title: title = random.choice(stanford_cme_titles)[0] print("assigning random title", name, title) grade = None if args.grade_text: grade = args.grade_text (download_uuid, verify_uuid, download_url) = cert.create_and_upload(name, upload=upload_files, copy_to_webroot=False, cleanup=False, designation=title, grade=grade) certificate_data.append((name, course, args.long_org, args.long_course, download_url)) gen_dir = os.path.join(cert.dir_prefix, S3_CERT_PATH, download_uuid) copy_dest = '{copy_dir}/{course}-{name}.pdf'.format( copy_dir=copy_dir, name=name.replace(" ", "-").replace("/", "-"), course=course.replace("/", "-")) try: shutil.copyfile('{0}/{1}'.format(gen_dir, TARGET_FILENAME), six.text_type(copy_dest)) except Exception as msg: # Sometimes we have problems finding or creating the files to be copied; # the following lines help us debug this case print(msg) print("%s\n%s\n%s\n%s\n%s\n%s" % (name, download_uuid, verify_uuid, download_uuid, gen_dir, copy_dest)) raise print("Created {0}".format(copy_dest)) should_write_report_to_stdout = not args.no_report if args.report_file: try: with open(args.report_file, 'wb') as file_report: csv_writer = csv.writer(file_report, quoting=csv.QUOTE_ALL) csv_writer.writerows(certificate_data) should_write_report_to_stdout = False except IOError as error: LOG.error("Unable to open report file: %s", error) if should_write_report_to_stdout: for row in certificate_data: print('\t'.join(row))
def main(): """ Generates some pfds using each template for different names for review in a pdf viewer. Will copy out the pdfs into the certs/ dir """ pdf_dir = TMP_GEN_DIR copy_dir = TMP_GEN_DIR + "+copy" if args.output_file: # ensure we can open the output file output_f = open(args.output_file, 'bw') # Remove files if they exist for d in [pdf_dir, copy_dir]: if os.path.exists(d): shutil.rmtree(d) if not os.path.exists(copy_dir): os.makedirs(copy_dir) certificate_data = [] if args.course_id: course_list = [args.course_id] else: course_list = settings.CERT_DATA.keys() upload_files = True if args.no_upload: upload_files = False for course in course_list: if args.name: name_list = [args.name] elif args.input_file: with open(args.input_file) as f: name_list = [line.rstrip() for line in f.readlines()] else: name_list = NAMES for name in name_list: cert = CertificateGen( course, args.template_file, aws_id=settings.CERT_AWS_ID, aws_key=settings.CERT_AWS_KEY, dir_prefix=pdf_dir, long_org=args.long_org, long_course=args.long_course, issued_date=args.issued_date, ) title = None if args.random_title: title = random.choice(stanford_cme_titles)[0] print "generating random title", name, title grade = None if args.grade_text: grade = args.grade_text (download_uuid, verify_uuid, download_url) = cert.create_and_upload(name, upload=upload_files, copy_to_webroot=False, cleanup=False, designation=title, grade=grade) certificate_data.append((name, download_url)) gen_dir = os.path.join(cert.dir_prefix, S3_CERT_PATH, download_uuid) copy_dest = '{copy_dir}/{course}-{name}.pdf'.format( copy_dir=copy_dir, name=name.replace(" ", "-").replace("/", "-"), course=course.replace("/", "-")) try: shutil.copyfile('{0}/{1}'.format(gen_dir, TARGET_FILENAME), unicode(copy_dest.decode('utf-8'))) except Exception, msg: # Sometimes we have problems finding or creating the files to be copied; # the following lines help us debug this case print msg print "%s\n%s\n%s\n%s\n%s\n%s" % (name, download_uuid, verify_uuid, download_uuid, gen_dir, copy_dest) raise print "Created {0}".format(copy_dest)
def main(): """ Generates samples of the Berkeley 188x letterhead Will copy out the pdfs into the letterheads/ dir """ pdf_dir = "/var/tmp/gen_letterheads" copy_dir = "/var/tmp/letterheads" # Remove files if they exist for d in [pdf_dir, copy_dir]: if os.path.exists(d): shutil.rmtree(d) if not os.path.exists(copy_dir): os.makedirs(copy_dir) download_urls = [] # only CS188.1x has the letterhead course = 'BerkeleyX/CS188.1x/2012_Fall' if args.file: with open(args.file, 'rb') as f: dist = json.loads(f.read()) new_dist = [] letterhead = CertificateGen(course, settings.CERT_AWS_ID, settings.CERT_AWS_KEY, dir_prefix=pdf_dir) for entry in dist: print entry['name'] (download_uuid, verify_uuid, download_url) = letterhead.create_and_upload( entry['name'].encode('utf-8'), upload=True, cleanup=True, letterhead=True) entry.update({'url': download_url}) new_dist.append(entry) with open('/tmp/distinguished_data', 'wb') as f: f.write(json.dumps(new_dist)) else: for name in NAMES: letterhead = CertificateGen(course, settings.CERT_AWS_ID, settings.CERT_AWS_KEY, dir_prefix=pdf_dir) (download_uuid, verify_uuid, download_url) = letterhead.create_and_upload(name, upload=True, cleanup=False, letterhead=True) download_urls.append(download_url) gen_dir = os.path.join(letterhead.dir_prefix, S3_CERT_PATH, download_uuid) copy_dest = '{copy_dir}/{course}-{name}.pdf'.format( copy_dir=copy_dir, name=name.replace(" ", "-").replace("/", "-"), course=course.replace("/", "-")) shutil.copyfile('{0}/distinction-letter.pdf'.format(gen_dir), unicode(copy_dest.decode('utf-8'))) print "Created {0}".format(copy_dest) print "\n".join(download_urls)