def _update_template_styles(pod_template, style_template_filename): """ Update template pod_template by templateStyle. """ # we check if the pod_template has been modified except by style only style_changes_only = \ pod_template.style_modification_md5 and pod_template.current_md5 == pod_template.style_modification_md5 # save in temporary file, the template temp_file = create_temporary_file(pod_template.odt_file, 'pod_template.odt') new_template = open(temp_file.name, 'w') new_template.write(pod_template.odt_file.data) new_template.close() # merge style from templateStyle in template cmd = '{path} {script} {tmp_file} {extension} -e ' \ '{libreoffice_host} -p {port} ' \ '-t {style_template} -v -a {stream}'.format(path=config.get_uno_path(), script=CONVSCRIPT, tmp_file=temp_file.name, extension='odt', libreoffice_host=config.get_oo_server(), port=config.get_oo_port(), style_template=style_template_filename, stream=config.get_use_stream()) (stdout, stderr) = executeCommand(cmd.split()) if stderr: logger.error("Error during command '%s'" % cmd) logger.error("Error is '%s'" % stderr) portal = api.portal.get() request = portal.REQUEST api.portal.show_message(message=_( u"Problem during styles update on template '${tmpl}': ${err}", mapping={ 'tmpl': safe_unicode(pod_template.absolute_url_path()), 'err': safe_unicode(stderr) }), request=request, type='error') raise Redirect( request.get('ACTUAL_URL'), translate( _(u"Problem during styles update on template '${tmpl}': ${err}", mapping={ 'tmpl': safe_unicode(pod_template.absolute_url_path()), 'err': safe_unicode(stderr) }))) # read the merged file resTempFileName = '.res.'.join(temp_file.name.rsplit('.', 1)) if os.path.isfile(resTempFileName): resTemplate = open(resTempFileName, 'rb') # update template result = NamedBlobFile( data=resTemplate.read(), contentType='application/vnd.oasis.opendocument.text', filename=pod_template.odt_file.filename) pod_template.odt_file = result remove_tmp_file(resTempFileName) # if only styles were modified: update the style_modification_md5 attribute if style_changes_only: pod_template.style_modification_md5 = pod_template.current_md5 remove_tmp_file(temp_file.name)
def _render_document(self, pod_template, output_format, sub_documents, raiseOnError=False, **kwargs): """ Render a single document of type 'output_format' using the odt file 'document_template' as the generation template. Subdocuments is a dictionnary of previously generated subtemplate that will be merged into the current generated document. """ document_template = pod_template.get_file() tmp_dir = os.getenv('CUSTOM_TMP', None) temp_filename = tempfile.mktemp( suffix='.{extension}'.format(extension=output_format), dir=tmp_dir) # Prepare rendering context helper_view = self.get_generation_context_helper() generation_context = self._get_generation_context( helper_view, pod_template=pod_template) # enrich the generation context with previously generated documents utils.update_dict_with_validation( generation_context, sub_documents, _("Error when merging merge_templates in generation context")) # enable optimalColumnWidths if enabled in the config and/or on ConfigurablePodTemplate stylesMapping = {} optimalColumnWidths = "OCW_.*" distributeColumns = "DC_.*" column_modifier = pod_template.get_column_modifier() if column_modifier == -1: column_modifier = config.get_column_modifier() if column_modifier == 'disabled': optimalColumnWidths = False distributeColumns = False else: stylesMapping = { 'table': TableProperties(columnModifier=column_modifier != 'nothing' and column_modifier or None) } # if raiseOnError is not enabled, enabled it in the config excepted if user is a Manager # and currently generated document use odt format if not raiseOnError: if config.get_raiseOnError_for_non_managers(): raiseOnError = True if 'Manager' in api.user.get_roles( ) and output_format == 'odt': raiseOnError = False # stylesMapping.update({'para[class=None, parent != cell]': 'texte_delibe', # 'para[class=xSmallText, parent != cell]': 'bodyXSmall', # 'para[class=smallText, parent != cell]': 'bodySmall', # 'para[class=largeText, parent != cell]': 'bodyLarge', # 'para[class=xLargeText, parent != cell]': 'bodyXLarge', # 'para[class=indentation, parent != cell]': 'bodyIndentation', # 'para[class=None, parent = cell]': 'cell_delibe', # 'table': TableProperties(cellContentStyle='cell_delibe'), # 'para[class=xSmallText, parent = cell]': 'cellXSmall', # 'para[class=smallText, parent = cell]': 'cellSmall', # 'para[class=largeText, parent = cell]': 'cellLarge', # 'para[class=xLargeText, parent = cell]': 'cellXLarge', # 'para[class=indentation, parent = cell]': 'cellIndentation', # }) # stylesMapping.update({'para[class=None,parent!=cell]': 'texte_delibe', # 'para[class=xSmallText,parent!=cell]': 'bodyXSmall', # 'para[class=smallText,parent!=cell]': 'bodySmall', # 'para[class=largeText,parent!=cell]': 'bodyLarge', # 'para[class=xLargeText,parent!=cell]': 'bodyXLarge', # 'para[class=indentation,parent!=cell]': 'bodyIndentation', # 'para[class=xSmallText,parent=cell]': 'cellXSmall', # 'para[class=smallText,parent=cell]': 'cellSmall', # 'para[class=largeText,parent=cell]': 'cellLarge', # 'para[class=xLargeText,parent=cell]': 'cellXLarge', # 'para[class=indentation,parent=cell]': 'cellIndentation', # }) renderer = Renderer(StringIO(document_template.data), generation_context, temp_filename, pythonWithUnoPath=config.get_uno_path(), ooServer=config.get_oo_server(), ooPort=config.get_oo_port(), raiseOnError=raiseOnError, imageResolver=api.portal.get(), forceOoCall=True, optimalColumnWidths=optimalColumnWidths, distributeColumns=distributeColumns, stylesMapping=stylesMapping, stream=config.get_use_stream(), **kwargs) # it is only now that we can initialize helper view's appy pod renderer all_helper_views = self.get_views_for_appy_renderer( generation_context, helper_view) for view in all_helper_views: view._set_appy_renderer(renderer) renderer.run() # return also generation_context to test ist content in tests return temp_filename, generation_context