def generate_zipfile(self): # we might be called when the zip file has already been generated. # abort in such cases. if self.zip_job.is_finished(): return pdfs = {} for document_id in self.zip_job.list_document_ids(): doc_status = self.zip_job.get_doc_status(document_id) if doc_status['status'] == 'finished': pdfs[document_id] = doc_status.pop('blob') doc_status['status'] = 'zipped' with ZipGenerator() as generator, elevated_privileges(): zipper = MeetingPDFDocumentZipper(self.meeting, pdfs, generator) zip_blob_file = NamedBlobFile(data=zipper.get_zip_file(), contentType='application/zip') self.zip_job.set_zip_file(zip_blob_file)
def test_zip_is_not_empty(self): file = create( Builder("file").titled(u"File").attach_file_containing( "Testdata file in subfolder", u"subtest.txt")) ziprepresentation = getMultiAdapter((file, self.request), interface=IZipRepresentation) with ZipGenerator() as zipgenerator: self.assertTrue(zipgenerator.is_empty) for file_path, file_pointer in ziprepresentation.get_files(): zipgenerator.add_file(file_path, file_pointer) self.assertFalse(zipgenerator.is_empty) generated_zip_pointer = zipgenerator.generate() if (not hasattr(generated_zip_pointer, "name") or (os.stat(generated_zip_pointer.name).st_size == 0)): raise AssertionError()
def debug_excerpt_docxcompose(self): if not self.is_manager(): raise Forbidden if self.agenda_item.is_paragraph: raise NotFound excerpt_protocol_data = ExcerptProtocolData(self.meeting, [self.agenda_item]) header_template = self.agenda_item.get_excerpt_header_template() suffix_template = self.agenda_item.get_excerpt_suffix_template() with ZipGenerator() as generator: if header_template: sablon = Sablon(header_template).process( excerpt_protocol_data.as_json()) generator.add_file(u'000_excerpt_header_template.docx', StringIO(sablon.file_data)) document = self.agenda_item.resolve_document() filename = u'001_agenda_item_{}.docx'.format( safe_unicode(document.Title())) generator.add_file(filename, document.file.open()) if suffix_template: sablon = Sablon(suffix_template).process( excerpt_protocol_data.as_json()) generator.add_file(u'002_excerpt_suffix_template.docx', StringIO(sablon.file_data)) # Return zip response = self.request.response zip_file = generator.generate() filename = '{}.zip'.format(normalize_path(self.meeting.title)) response.setHeader( "Content-Disposition", 'inline; filename="{0}"'.format( safe_unicode(filename).encode('utf-8'))) response.setHeader("Content-type", "application/zip") response.setHeader("Content-Length", os.stat(zip_file.name).st_size) return filestream_iterator(zip_file.name, 'rb')
def zip_selected(self, objects): response = self.request.response # check if zipexport is allowed on this context enabled_view = getMultiAdapter((self.context, self.request), name=u'zipexport-enabled') if not enabled_view.zipexport_enabled(): raise NotFound() with ZipGenerator() as generator: for obj in objects: repre = getMultiAdapter((obj, self.request), interface=IZipRepresentation) for path, pointer in repre.get_files(): try: generator.add_file(path, pointer) except LargeZipFile: messages = IStatusMessage(self.request) messages.add(_("statmsg_zip_file_too_big", default=u"Content is too big " "to export"), type=u"error") return self.request.response.redirect( self.context.absolute_url()) # check if zip has files if generator.is_empty: raise NoExportableContent() zip_file = generator.generate() filename = '%s.zip' % self.context.title response.setHeader( "Content-Disposition", 'inline; filename="%s"' % safe_unicode(filename).encode('utf-8')) response.setHeader("Content-type", "application/zip") response.setHeader("Content-Length", os.stat(zip_file.name).st_size) return filestream_iterator(zip_file.name, 'rb')
def test_generator_creates_unique_file_names(self): folder = create(Builder('folder').titled(u"folder")) create( Builder("file").within(folder).attach_file_containing( "File1", u"file.txt")) create( Builder("file").within(folder).attach_file_containing( "File2", u"file.txt")) create( Builder("file").within(folder).attach_file_containing( "File3", u"file.txt")) ziprepresentation = getMultiAdapter((folder, self.request), interface=IZipRepresentation) with ZipGenerator() as zipgenerator: for path, pointer in ziprepresentation.get_files(): zipgenerator.add_file(path, pointer) in_zip_file_list = zipgenerator.zip_file.namelist() self.assertEquals([u'file.txt', u'file (2).txt', u'file (3).txt'], in_zip_file_list)
def __call__(self): if not is_word_meeting_implementation_enabled(): raise Forbidden with ZipGenerator() as generator: self.add_header_sablon(generator) for index, agenda_item in enumerate(self.meeting.agenda_items, 1): self.add_agenda_item(index, agenda_item, generator) self.add_suffix_sablon(index, generator) # Return zip response = self.request.response zip_file = generator.generate() filename = '{}.zip'.format(normalize_path(self.meeting.title)) response.setHeader( "Content-Disposition", 'inline; filename="{0}"'.format( safe_unicode(filename).encode('utf-8'))) response.setHeader("Content-type", "application/zip") response.setHeader( "Content-Length", os.stat(zip_file.name).st_size) return filestream_iterator(zip_file.name, 'rb')
def generate_zip(self): response = self.request.response with ZipGenerator() as generator: # Protocol generator.add_file(*self.get_protocol()) # Agenda items self.add_agenda_items_attachments(generator) # Agenda items list self.add_agenda_item_list(generator) # Return zip zip_file = generator.generate() filename = '{}.zip'.format(normalize_path(self.model.title)) response.setHeader( "Content-Disposition", 'inline; filename="{0}"'.format( safe_unicode(filename).encode('utf-8'))) response.setHeader("Content-type", "application/zip") response.setHeader("Content-Length", os.stat(zip_file.name).st_size) return filestream_iterator(zip_file.name, 'rb')
def test_filenames_are_normalized(self): with ZipGenerator() as zipgenerator: zipgenerator.add_file('F\xc3\xbc\xc3\xb6 B\xc3\xa4r.tar.gz', StringIO()) self.assertItemsEqual([u'Fuo Bar.tar.gz'], zipgenerator.zip_file.namelist())
def test_generator_raises_exception_when_not_used_as_generator(self): zipgenerator = ZipGenerator() self.assertRaises(StandardError, zipgenerator.generate)
def test_add_folder_will_create_a_new_folder(self): with ZipGenerator() as zipgenerator: zipgenerator.add_folder('Documents') self.assertEqual(['Documents/'], zipgenerator.zip_file.namelist())