def test_sablon_availability(self): json_data = json.dumps({'foo': 42}) sablon = Sablon(MockTemplate()) sablon.process(json_data) self.assertEqual(0, sablon.returncode, msg=sablon.stderr)
def __call__(self): template = self.model.get_toc_template() if not template: api.portal.show_message( _('msg_no_toc_template', default=u'There is no toc template configured, toc could ' 'not be generated.'), request=self.request, type='error') return self.request.RESPONSE.redirect( "{}/#periods".format(self.context.parent.absolute_url())) sablon = Sablon(template) sablon.process(self.get_json_data()) assert sablon.is_processed_successfully(), sablon.stderr filename = self.get_filename().encode('utf-8') response = self.request.response response.setHeader('X-Theme-Disabled', 'True') response.setHeader('Content-Type', MIME_DOCX) response.setHeader("Content-Disposition", 'attachment; filename="{}"'.format(filename)) return sablon.file_data
def generate_file_data(self): template = self.document_operations.get_sablon_template(self.meeting) sablon = Sablon(template) sablon.process( self.document_operations.get_meeting_data(self.meeting).as_json()) assert sablon.is_processed_successfully(), sablon.stderr return sablon.file_data
def __call__(self): response = self.request.response template = self.model.get_toc_template() if not template: api.portal.show_message(_( 'msg_no_toc_template', default=u'There is no toc template configured, toc could ' 'not be generated.'), request=self.request, type='error') response.setHeader('X-ogg-reload-page', "True") return sablon = Sablon(template) try: sablon.process(self.get_json_data()) except SablonProcessingFailed: message = _(u'Error while processing Sablon template') api.portal.show_message(message, request=self.request, type='error') response.setHeader('X-ogg-reload-page', "True") return filename = self.get_filename().encode('utf-8') response.setHeader('X-Theme-Disabled', 'True') response.setHeader('Content-Type', MIME_DOCX) response.setHeader("Content-Disposition", 'attachment; filename="{}"'.format(filename)) return sablon.file_data
def __call__(self): response = self.request.response template = self.model.get_toc_template() if not template: api.portal.show_message( _('msg_no_toc_template', default=u'There is no toc template configured, toc could ' 'not be generated.'), request=self.request, type='error') response.setHeader('X-ogg-reload-page', "True") return sablon = Sablon(template) try: sablon.process(self.get_json_data()) except SablonProcessingFailed: message = _(u'Error while processing Sablon template') api.portal.show_message(message, request=self.request, type='error') response.setHeader('X-ogg-reload-page', "True") return filename = self.get_filename().encode('utf-8') response.setHeader('X-Theme-Disabled', 'True') response.setHeader('Content-Type', MIME_DOCX) response.setHeader("Content-Disposition", 'attachment; filename="{}"'.format(filename)) return sablon.file_data
def sablon_template_is_valid(value): # create the sablon template using the blob file sablon = Sablon(None) for template_type, data in VALIDATION_DATA.items(): sablon.process(json.dumps(data), namedblobfile=value) if sablon.is_processed_successfully(): return True return False
def render(self): sablon = Sablon(self.context) sablon.process(json.dumps(SAMPLE_MEETING_DATA)) assert sablon.is_processed_successfully(), sablon.stderr response = self.request.response response.setHeader('X-Theme-Disabled', 'True') response.setHeader('Content-Type', MIME_DOCX) response.setHeader("Content-Disposition", 'attachment; filename="{}"'.format('template.docx')) return sablon.file_data
def sablon_template_is_valid(value): # create the sablon template using the blob file sablon = Sablon(None) for template_type, data in VALIDATION_DATA.items(): try: sablon.process(json.dumps(data), namedblobfile=value) return True except SablonProcessingFailed: continue return False
def __call__(self): sablon = Sablon(self.context) sablon.process(json.dumps(SAMPLE_MEETING_DATA)) assert sablon.is_processed_successfully(), sablon.stderr response = self.request.response response.setHeader('X-Theme-Disabled', 'True') response.setHeader('Content-Type', MIME_DOCX) response.setHeader("Content-Disposition", 'attachment; filename="{}"'.format('template.docx')) return sablon.file_data
def __call__(self): sablon = Sablon(self.operations.get_sablon_template(self.model)) sablon.process(self.get_protocol_json()) assert sablon.is_processed_successfully(), sablon.stderr filename = self.operations.get_filename(self.model).encode('utf-8') response = self.request.response response.setHeader('X-Theme-Disabled', 'True') response.setHeader('Content-Type', MIME_DOCX) response.setHeader("Content-Disposition", 'attachment; filename="{}"'.format(filename)) return sablon.file_data
def __call__(self): sablon = Sablon(self.context) try: sablon.process(json.dumps(SAMPLE_MEETING_DATA)) except SablonProcessingFailed as err: return safe_unicode(err.message) response = self.request.response response.setHeader('X-Theme-Disabled', 'True') response.setHeader('Content-Type', MIME_DOCX) response.setHeader("Content-Disposition", 'attachment; filename="{}"'.format('template.docx')) return sablon.file_data
def get_sablon_for_paragraph(self, agenda_item): template = self._get_paragraph_template() if template is None: return return Sablon(self._get_paragraph_template()).process( ProtocolData(self.meeting, [agenda_item]).as_json())
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')
def add_sablon_for_paragraph(self, index, agenda_item, generator): committee = self.meeting.committee.resolve_committee() template = committee.get_paragraph_template() sablon = Sablon(template).process( ProtocolData(self.meeting, [agenda_item]).as_json()) filename = u'{:03d}_paragraph_{}.docx'.format( index, safe_unicode(agenda_item.title)) generator.add_file(filename, StringIO(sablon.file_data))
def add_suffix_sablon(self, index, generator): template = self.meeting.get_protocol_suffix_template() if template is None: return sablon = Sablon(template).process( self.operations.get_meeting_data(self.meeting).as_json()) generator.add_file( u'{:03d}_protocol_suffix_template.docx'.format(index + 1), StringIO(sablon.file_data))
def generate_file_data(self): template = self.document_operations.get_sablon_template(self.meeting) sablon = Sablon(template) sablon.process( self.document_operations.get_meeting_data(self.meeting).as_json()) tmpdir_path = tempfile.mkdtemp(prefix='opengever.core.doxcmerge_') master_path = join(tmpdir_path, 'master.docx') output_path = join(tmpdir_path, 'protocol.docx') try: # XXX: this is a bit dumb since sablon would already have generated # a temporary file with open(master_path, 'wb') as master_file: master_file.write(sablon.file_data) composer = Composer(Document(master_path)) for index, agenda_item in enumerate(self.meeting.agenda_items): target_path = join(tmpdir_path, 'agenda_item_{}.docx'.format(index)) if agenda_item.is_paragraph: self._export_paragraph_as_document(agenda_item, target_path) composer.append(Document(target_path)) elif agenda_item.has_document: self._export_regular_agenda_item_document( agenda_item, target_path) composer.append(Document(target_path)) composer.save(output_path) with open(output_path, 'rb') as merged_file: data = merged_file.read() finally: shutil.rmtree(tmpdir_path) return data
def get_header_sablon(self): template = self.meeting.get_protocol_header_template() if template is None: raise MissingProtocolHeaderTemplate() return Sablon(template).process( self.document_operations.get_meeting_data(self.meeting).as_json())
def add_header_sablon(self, generator): template = self.meeting.get_protocol_header_template() sablon = Sablon(template).process( self.operations.get_meeting_data(self.meeting).as_json()) generator.add_file(u'000_protocol_header_template.docx', StringIO(sablon.file_data))
def generate_file_data(self): template = self.document_operations.get_sablon_template(self.meeting) data = self.document_operations.get_meeting_data( self.meeting).as_json() sablon = Sablon(template).process(data) return sablon.file_data
def get_sablon(self, template): return Sablon(template).process(self.excerpt_protocol_data.as_json())
def _export_paragraph_as_document(self, agenda_item, target_path): sablon = Sablon(self._get_paragraph_template()) sablon.process(ProtocolData(self.meeting, [agenda_item]).as_json()) with open(target_path, 'wb') as fio: fio.write(sablon.file_data)
def get_agenda_item_suffix_sablon(self, agenda_item): template = self.meeting.get_agenda_item_suffix_template() if template is None: return return Sablon(template).process( ProtocolData(self.meeting, [agenda_item]).as_json())
def create_agenda_item_list(self): template = self.get_sablon_template() sablon = Sablon(template) sablon.process(self.get_json_data()) assert sablon.is_processed_successfully(), sablon.stderr return sablon.file_data
def get_suffix_sablon(self): return Sablon(self.meeting.get_protocol_suffix_template()).process( self.document_operations.get_meeting_data(self.meeting).as_json())
def generate_file_data(self): template = self.document_operations.get_sablon_template(self.meeting) sablon = Sablon(template) sablon.process(self.document_operations.get_meeting_data(self.meeting).as_json()) return sablon.file_data