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 debug_excerpt_docxcompose(self): if not api.user.has_permission('cmf.ManagePortal'): 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')
class MergeDocxExcerptCommand(CreateDocumentCommand): """Create or update a merged excerpt word file. If an excerpt header is available it is considered the master document. If not, the agenda item`s file is considered the master document. """ def __init__(self, context, agenda_item, filename, title): self.agenda_item = agenda_item self.excerpt_protocol_data = ExcerptProtocolData( self.agenda_item.meeting, [self.agenda_item]) self.filename = filename super(MergeDocxExcerptCommand, self).__init__( context=context, filename=filename, data=None, title=title) def generate_file_data(self): header_template = self.agenda_item.get_excerpt_header_template() suffix_template = self.agenda_item.get_excerpt_suffix_template() agenda_item_document = self.agenda_item.resolve_document() has_header_template = header_template is not None if has_header_template and suffix_template is None: return agenda_item_document.file.data if has_header_template: sablon = self.get_sablon(template=header_template) master_data = sablon.file_data else: master_data = agenda_item_document.file.data with DocxMergeTool(master_data, remove_property_fields=False) as merge_tool: if has_header_template: merge_tool.add(agenda_item_document.file.data) if suffix_template is not None: sablon = self.get_sablon(template=suffix_template) merge_tool.add(sablon.file_data) return merge_tool() def get_sablon(self, template): return Sablon(template).process(self.excerpt_protocol_data.as_json()) def execute(self): self.set_file( self.filename, self.generate_file_data(), MIME_DOCX, ) return super(MergeDocxExcerptCommand, self).execute()