From 47e476b2c58bf960e7be747256513d37e209265a Mon Sep 17 00:00:00 2001 From: Michael Harbarth Date: Thu, 5 Sep 2024 18:37:32 +0200 Subject: [PATCH] fix: don't delete any rendering layouts and keep the order of existing rendering layouts even when overwrite-layouts is enabled --- .../converters/document_renderer.py | 17 +++++++++++++++-- tests/test_documents.py | 17 ++++++++++++++--- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/capella2polarion/converters/document_renderer.py b/capella2polarion/converters/document_renderer.py index 15a9dc6..6a5a095 100644 --- a/capella2polarion/converters/document_renderer.py +++ b/capella2polarion/converters/document_renderer.py @@ -347,6 +347,19 @@ def update_mixed_authority_document( document, session.headings, text_work_item_provider ) + def _update_rendering_layouts( + self, + document: polarion_api.Document, + rendering_layouts: list[polarion_api.RenderingLayout], + ): + """Keep existing work item layouts in their original order.""" + document.rendering_layouts = document.rendering_layouts or [] + for rendering_layout in rendering_layouts: + index = polarion_html_helper.get_layout_index( + "section", document.rendering_layouts, rendering_layout.type + ) + document.rendering_layouts[index] = rendering_layout + def _get_and_customize_doc( self, project_id: str | None, @@ -359,11 +372,11 @@ def _get_and_customize_doc( old_doc, text_work_items = self.existing_documents.get( (project_id, space, name), (None, []) ) - if old_doc: + if old_doc is not None: if title: old_doc.title = title if self.overwrite_layouts: - old_doc.rendering_layouts = rendering_layouts + self._update_rendering_layouts(old_doc, rendering_layouts) if self.overwrite_heading_numbering: old_doc.outline_numbering = heading_numbering diff --git a/tests/test_documents.py b/tests/test_documents.py index 93c4b1c..7e88b2f 100644 --- a/tests/test_documents.py +++ b/tests/test_documents.py @@ -46,9 +46,12 @@ def existing_documents() -> polarion_repo.DocumentRepository: value=MIXED_AUTHORITY_DOCUMENT.read_text("utf-8"), ), rendering_layouts=[ + polarion_api.RenderingLayout( + "text", "paragraph", type="text" + ), polarion_api.RenderingLayout( "Class", "paragraph", type="class" - ) + ), ], ), [], @@ -461,7 +464,7 @@ def test_render_all_documents_partially_successfully( ) assert ( len(projects_data[None].updated_docs[1].document.rendering_layouts) - == 1 + == 2 ) assert ( projects_data[None].updated_docs[0].document.outline_numbering is None @@ -540,7 +543,15 @@ def test_render_all_documents_overwrite_headings_layouts( updated_docs = projects_data[None].updated_docs assert len(updated_docs[0].document.rendering_layouts) == 2 - assert len(updated_docs[1].document.rendering_layouts) == 2 + assert len(updated_docs[1].document.rendering_layouts) == 3 + assert updated_docs[1].document.rendering_layouts[0].type == "text" + assert updated_docs[1].document.rendering_layouts[1].type == "class" + assert ( + "tree_view_diagram" + in updated_docs[1] + .document.rendering_layouts[1] + .properties.fields_at_end + ) assert updated_docs[0].document.outline_numbering is False assert updated_docs[1].document.outline_numbering is False