def handle(self, *args, **options): # maybe this should be replaced with a CLI options: do_upload = os.environ.get('AGREEMENTS_S3_UPLOAD_ENABLED', False) Agreement.objects.all().delete() Issuer.objects.all().delete() agreements_zip = ZipFile(options['path']) all_pdfs = [ name for name in agreements_zip.namelist() if name.upper().endswith('.PDF') ] blanks = empty_folder_test(agreements_zip, all_pdfs) if blanks: error_msg = ("Processing error: Blank folders were found " "in the source zip file:\n{}".format(", ".join( [folder for folder in blanks]))) raise CommandError(error_msg) if options['verbosity'] >= 1: output_file = self.stdout else: output_file = open(os.devnull, 'a') for pdf_path in all_pdfs: _util.save_agreement(agreements_zip, pdf_path, output_file, windows=options['windows'], upload=do_upload)
def test_save_agreement(self): agreements_zip = zipfile.ZipFile(sample_zip) # windows-1252 encoded: raw_path = 'Bankers\x92 Bank of Kansas/1.pdf' buf = StringIO() agreement = _util.save_agreement(agreements_zip, raw_path, buf, windows=True, upload=False) self.assertEqual(agreement.file_name, '1.pdf')
def test_save_agreement(self): agreements_zip = zipfile.ZipFile(utf8_zip) expectedName = 'Visa Cardholder Agreement and Disclosures.pdf' raw_path = 'UTF_agreements/' + expectedName buf = six.StringIO() agreement = _util.save_agreement(agreements_zip, raw_path, buf, upload=False) self.assertEqual(agreement.file_name, expectedName)
def test_save_agreement(self): agreements_zip = zipfile.ZipFile(sample_zip) # windows-1252 encoded: raw_path = 'Bankers\x92 Bank of Kansas/1.pdf' buf = StringIO() agreement = _util.save_agreement( agreements_zip, raw_path, buf, windows=True, upload=False) self.assertEqual(agreement.file_name, '1.pdf')
def handle(self, *args, **options): # maybe this should be replaced with a CLI options: do_upload = os.environ.get('AGREEMENTS_S3_UPLOAD_ENABLED', False) Agreement.objects.all().delete() Issuer.objects.all().delete() agreements_zip = ZipFile(options['path']) # Zip files default to IBM Code Page 437 encoding unless a specific bit # is set. See Appendix D in the zip file spec: # https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT all_pdfs = [ info.filename if (info.flag_bits & 0x800) == 0 else force_text(info.filename, 'cp437') for info in agreements_zip.infolist() if info.filename.upper().endswith('.PDF') ] blanks = empty_folder_test(agreements_zip, all_pdfs) if blanks: error_msg = ( "Processing error: Blank folders were found " "in the source zip file:\n{}".format( ", ".join([folder for folder in blanks])) ) raise CommandError(error_msg) if options['verbosity'] >= 1: output_file = self.stdout else: output_file = open(os.devnull, 'a') for pdf_path in all_pdfs: _util.save_agreement( agreements_zip, pdf_path, output_file, upload=do_upload)
def test_save_agreement(self): agreements_zip = zipfile.ZipFile(sample_zip) if six.PY3: # pragma: no cover raw_path = b'Bankers\x92 Bank of Kansas/1.pdf'.decode('cp437') else: # pragma: no cover raw_path = 'Bankers\x92 Bank of Kansas/1.pdf' buf = six.StringIO() agreement = _util.save_agreement(agreements_zip, raw_path, buf, upload=False) self.assertEqual(agreement.file_name, '1.pdf')
def test_save_agreement(self): agreements_zip = zipfile.ZipFile(sample_zip) if six.PY3: # pragma: no cover raw_path = b'Bankers\x92 Bank of Kansas/1.pdf'.decode('cp437') else: # pragma: no cover raw_path = 'Bankers\x92 Bank of Kansas/1.pdf' buf = six.StringIO() agreement = _util.save_agreement( agreements_zip, raw_path, buf, upload=False) self.assertEqual(agreement.file_name, '1.pdf')