From 2ebf392cd5e8bb965850d3e6e9a0af41dc7445a7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Peter=20Sch=C3=A4r?= <peter.schaer@be.ch>
Date: Fri, 30 Aug 2024 14:32:32 +0200
Subject: [PATCH] toc length calculation includes law_status (#2038)

---
 .../mapfish_print/mapfish_print.py            | 12 ++++----
 .../print_proxy/mapfish_print/toc_pages.py    | 30 +++++++++++--------
 .../test_mapfish_print.py                     |  2 +-
 3 files changed, 23 insertions(+), 21 deletions(-)

diff --git a/pyramid_oereb/contrib/print_proxy/mapfish_print/mapfish_print.py b/pyramid_oereb/contrib/print_proxy/mapfish_print/mapfish_print.py
index b5b783179d..62d92210a8 100644
--- a/pyramid_oereb/contrib/print_proxy/mapfish_print/mapfish_print.py
+++ b/pyramid_oereb/contrib/print_proxy/mapfish_print/mapfish_print.py
@@ -73,13 +73,6 @@ def __call__(self, value, system):
         extract_as_dict = self._render(extract_record, value[1])
         feature_geometry = mapping(extract_record.real_estate.limit)
 
-        display_qrcode = Config.get('print', {}).get('display_qrcode', False)
-
-        if Config.get('print', {}).get('compute_toc_pages', False):
-            extract_as_dict['nbTocPages'] = TocPages(extract_as_dict, display_qrcode).getNbPages()
-        else:
-            extract_as_dict['nbTocPages'] = 1
-
         # set the global_datetime variable so that it can be used later for the archive
         self.set_global_datetime(extract_as_dict['CreationDate'])
         self.convert_to_printable_extract(extract_as_dict, feature_geometry)
@@ -98,6 +91,11 @@ def __call__(self, value, system):
             'display_qrcode', False
         )
 
+        if print_config.get('compute_toc_pages', False):
+            extract_as_dict['nbTocPages'] = TocPages(extract_as_dict).getNbPages()
+        else:
+            extract_as_dict['nbTocPages'] = 1
+
         spec = {
             'layout': Config.get('print', {})['template_name'],
             'outputFormat': 'pdf',
diff --git a/pyramid_oereb/contrib/print_proxy/mapfish_print/toc_pages.py b/pyramid_oereb/contrib/print_proxy/mapfish_print/toc_pages.py
index 4171440570..7e6c580127 100644
--- a/pyramid_oereb/contrib/print_proxy/mapfish_print/toc_pages.py
+++ b/pyramid_oereb/contrib/print_proxy/mapfish_print/toc_pages.py
@@ -7,7 +7,7 @@
 
 class TocPages():
 
-    def __init__(self, extract, display_qrcode):
+    def __init__(self, extract):
         self.disposable_height = 842 - 70  # A4 size - (footer + header); toc.jrxml
         self.d1_height = 77  # toc.jrxml
         self.d2_height = 29  # toc.jrxml
@@ -18,7 +18,7 @@ def __init__(self, extract, display_qrcode):
         self.d6_left_height = 38  # toc.jrxml
         self.d6_right_height = 20  # toc.jrxml
         self.extract = extract
-        self.display_qrcode = display_qrcode
+        self.display_qrcode = self.extract['Display_QRCode']
         self.total_length = self.compute_total_lenght()
 
     def compute_d1(self):
@@ -34,7 +34,11 @@ def compute_d2(self):
         page_label_height = 10  # toc.jrxml
         total_size += blank_space_above + page_label_height
         toc_item_height = 17  # toc.jrxml (20 in tocConcernedTheme.jrxml)
-        total_size += len(self.extract['ConcernedTheme']) * toc_item_height
+        unique_concerned_themes = []
+        for concerned_theme in self.extract['ConcernedTheme']:
+            if concerned_theme not in unique_concerned_themes:
+                unique_concerned_themes.append(concerned_theme)
+        total_size += len(unique_concerned_themes) * toc_item_height
         log.debug(f"d2 total_size: {total_size}")
         if total_size > self.d2_height:
             return total_size
@@ -80,19 +84,19 @@ def compute_d6_left(self):
         general_information_item_line_heigth = 8  # general_info_and_disclaimer.jrxml
         total_size += general_information_title_height
         for i in self.extract.get('GeneralInformation', []):
-            total_size += self.compute_length_of_wrapped_text(i[0]['Text'],
+            total_size += self.compute_length_of_wrapped_text(i['Info'],
                                                               78,
                                                               general_information_item_line_heigth)
         # LandRegister-Disclaimer (1 title, 1 item)
         land_register_disclaimer_title_line_height = 8  # general_info_and_disclaimer.jrxml
         land_register_disclaimer_item_line_height = 8  # general_info_and_disclaimer.jrxml
-        for i in self.extract.get('DisclaimerLandRegister', []):
-            total_size += self.compute_length_of_wrapped_text(i['Title'][0]['Text'],
-                                                              65,
-                                                              land_register_disclaimer_title_line_height)
-            total_size += self.compute_length_of_wrapped_text(i['Content'][0]['Text'],
-                                                              78,
-                                                              land_register_disclaimer_item_line_height)
+
+        total_size += self.compute_length_of_wrapped_text(self.extract.get('DisclaimerLandRegister_Title', ''),
+                                                            65,
+                                                            land_register_disclaimer_title_line_height)
+        total_size += self.compute_length_of_wrapped_text(self.extract.get('DisclaimerLandRegister_Content', ''),
+                                                            78,
+                                                            land_register_disclaimer_item_line_height)
         log.debug('d6 left total_size : {}'.format(total_size))
         if total_size > self.d6_left_height:
             return total_size
@@ -115,10 +119,10 @@ def compute_d6_right(self):
 
         # Disclaimers (multiple items)
         for i in self.extract.get('Disclaimer', []):
-            total_size += self.compute_length_of_wrapped_text(i['Title'][0]['Text'],
+            total_size += self.compute_length_of_wrapped_text(i['Title'],
                                                               65,
                                                               disclaimer_title_line_height)
-            total_size += self.compute_length_of_wrapped_text(i['Content'][0]['Text'],
+            total_size += self.compute_length_of_wrapped_text(i['Content'],
                                                               78,
                                                               disclaimer_item_line_height)
         total_size += blank_space_below_disclaimers
diff --git a/tests/contrib.print_proxy.mapfish_print/test_mapfish_print.py b/tests/contrib.print_proxy.mapfish_print/test_mapfish_print.py
index a7c678a711..92ec3ac9e9 100644
--- a/tests/contrib.print_proxy.mapfish_print/test_mapfish_print.py
+++ b/tests/contrib.print_proxy.mapfish_print/test_mapfish_print.py
@@ -100,7 +100,7 @@ def geometry(coordinates):
 
 
 def test_toc_pages(extract):
-    assert TocPages(extract, False).getNbPages() == 1
+    assert TocPages(extract).getNbPages() == 1
 
 
 def getSameEntryInList(reference, objects):