diff --git a/src/ansys/dynamicreporting/core/adr_report.py b/src/ansys/dynamicreporting/core/adr_report.py index 15d88cad..318cc3d5 100644 --- a/src/ansys/dynamicreporting/core/adr_report.py +++ b/src/ansys/dynamicreporting/core/adr_report.py @@ -535,7 +535,8 @@ def get_iframe(self, width: int = 1000, height: int = 800, filter: str = ""): def export_pdf( self, file_name: str = "", - query: Optional[dict] = None, + query_params: Optional[dict] = None, + item_filter: Optional[str] = None, page: Optional[list] = None, delay: Optional[int] = None, ) -> bool: @@ -547,8 +548,11 @@ def export_pdf( ---------- file_name : str Path and filename for the PDF file to export. - query : dict, optional - Dictionary for query parameters to apply to report template before export. Default: None + query_params : dict, optional + Dictionary for parameters to apply to report template. Default: None + item_filter: str, optional + String corresponding to query to run on the database items before rendering the report. + Default: None page : list, optional List of integers that represents the size of the exported pdf. Default: None, which corresponds to A4 size @@ -569,7 +573,8 @@ def export_pdf( adr_service = adr.Service(ansys_installation = r'C:\\Program Files\\ANSYS Inc\\v232') ret = adr_service.connect() my_report = adr_service.get_report(report_name = "My Top Report") - succ = my_report.export_pdf(file_name=r'D:\\tmp\\myreport.pdf') + succ = my_report.export_pdf(file_name=r'D:\\tmp\\myreport.pdf', query = {"colormode": "dark"}) + succ2 = my_report.export_pdf(filename=r'D:\\tmp\\onlyimages.pdf', item_filter = 'A|i_type|cont|image;') """ success = False # pragma: no cover if self.service is None: # pragma: no cover @@ -579,12 +584,13 @@ def export_pdf( self.service.logger.error("No connection to any server") return "" try: # pragma: no cover - if query is None: - query = {} + if query_params is None: + query_params = {} self.service.serverobj.export_report_as_pdf( report_guid=self.report.guid, file_name=file_name, - query=query, + query=query_params, + item_filter=item_filter, page=page, parent=None, delay=delay, @@ -599,7 +605,8 @@ def export_pdf( def export_html( self, directory_name: str = "", - query: Optional[dict] = None, + query_params: Optional[dict] = None, + item_filter: Optional[str] = None, filename: Optional[str] = "index.html", no_inline_files: Optional[bool] = False, ) -> bool: @@ -610,8 +617,11 @@ def export_html( ---------- directory_name : str .... - query : dict, optional - Dictionary for query parameters to apply to report template before export. Default: None + query_params : dict, optional + Dictionary for parameters to apply to report template. Default: None + item_filter: str, optional + String corresponding to query to run on the database items before rendering the report. + Default: None filename : str, optional Filename for the exported static HTML file. Default: index.html no_inline_files : bool, optional @@ -631,7 +641,8 @@ def export_html( adr_service = adr.Service(ansys_installation = r'C:\\Program Files\\ANSYS Inc\\v232') ret = adr_service.connect() my_report = adr_service.get_report(report_name = "My Top Report") - succ = my_report.export_html(directory_name = r'D:\\tmp') + succ = my_report.export_html(directory_name = r'D:\\tmp', query={"colormode": "dark"}) + succ2 = my_report.export_html(filename=r'D:\\tmp\\onlyimages.pdf', item_filter = 'A|i_type|cont|image;') """ success = False if self.service is None: # pragma: no cover @@ -641,12 +652,13 @@ def export_html( self.service.logger.error("No connection to any server") return "" try: - if query is None: - query = {} + if query_params is None: + query_params = {} self.service.serverobj.export_report_as_html( report_guid=self.report.guid, directory_name=directory_name, - query=query, + query=query_params, + item_filter=item_filter, filename=filename, no_inline_files=no_inline_files, ansys_version=self.service._ansys_version, diff --git a/src/ansys/dynamicreporting/core/utils/report_remote_server.py b/src/ansys/dynamicreporting/core/utils/report_remote_server.py index d1c97145..5e347828 100755 --- a/src/ansys/dynamicreporting/core/utils/report_remote_server.py +++ b/src/ansys/dynamicreporting/core/utils/report_remote_server.py @@ -34,6 +34,7 @@ from urllib3.util.retry import Retry from . import exceptions, filelock, report_objects, report_utils +from ..adr_utils import build_query_url from .encoders import BaseEncoder @@ -852,7 +853,7 @@ def _download_report(self, url, file_name, directory_name=None): with open(file_path, "wb") as report: report.write(resp.content) - def build_url_with_query(self, report_guid, query, rest_api=False): + def build_url_with_query(self, report_guid, query, item_filter=None, rest_api=False): url = self.get_URL() if rest_api: url += f"/api/generate-report/?view={str(report_guid)}" @@ -864,6 +865,9 @@ def build_url_with_query(self, report_guid, query, rest_api=False): url += f"&{key}" else: url += f"&{key}={value}" + if item_filter: + query_str = build_query_url(filter=item_filter) + url += query_str return url def export_report_as_html( @@ -871,6 +875,7 @@ def export_report_as_html( report_guid, directory_name, query=None, + item_filter=None, filename="index.html", no_inline_files=False, ansys_version=None, @@ -881,7 +886,7 @@ def export_report_as_html( directory_path = os.path.abspath(directory_name) from ansys.dynamicreporting.core.utils.report_download_html import ReportDownloadHTML - url = self.build_url_with_query(report_guid, query) + url = self.build_url_with_query(report_guid, query, item_filter) # ask the server for the Ansys version number. It will generally know it. _ansys_version = self.get_api_version().get("ansys_version", self._ansys_version) if ansys_version: @@ -901,6 +906,7 @@ def export_report_as_pdf( report_guid, file_name, query=None, + item_filter=None, page=None, parent=None, delay=None, @@ -910,7 +916,7 @@ def export_report_as_pdf( if query is None: query = {} query["print"] = "pdf" - url = self.build_url_with_query(report_guid, query) + url = self.build_url_with_query(report_guid, query, item_filter) file_path = os.path.abspath(file_name) if has_qt and (parent is not None): from .report_download_pdf import NexusPDFSave diff --git a/test_cleanup.py b/test_cleanup.py index 3a28bb41..284c2d41 100644 --- a/test_cleanup.py +++ b/test_cleanup.py @@ -41,6 +41,7 @@ file_list.append("mypresentation") file_list.append("mytest.pdf") file_list.append("again_mytest") +file_list.append("again_mytest_filter") for i_file in file_list: try: os.remove(i_file) diff --git a/tests/test_data/query_db/db.sqlite3 b/tests/test_data/query_db/db.sqlite3 index 561fa51b..d616a7ae 100755 Binary files a/tests/test_data/query_db/db.sqlite3 and b/tests/test_data/query_db/db.sqlite3 differ diff --git a/tests/test_data/query_db/media/csf_conversion_version b/tests/test_data/query_db/media/csf_conversion_version index 7edf3119..3bc2e4d6 100755 --- a/tests/test_data/query_db/media/csf_conversion_version +++ b/tests/test_data/query_db/media/csf_conversion_version @@ -1 +1 @@ -23.20R1 +25.20R1 diff --git a/tests/test_report.py b/tests/test_report.py index d4796d45..8cbf2573 100755 --- a/tests/test_report.py +++ b/tests/test_report.py @@ -118,6 +118,22 @@ def test_save_as_pdf(adr_service_query, request, get_exec) -> None: assert success is True +@pytest.mark.ado_test +def test_save_as_pdf_with_filter(adr_service_query, request, get_exec) -> None: + exec_basis = get_exec + if exec_basis: + success = False + try: + my_report = adr_service_query.get_report(report_name="My Top Report") + pdf_file = os.path.join(request.fspath.dirname, "again_mytest_filter") + success = my_report.export_pdf(file_name=pdf_file, item_filter="A|i_type|cont|image;") + except Exception: + success = False + else: # If no local installation, then skip this test + success = True + assert success is True + + @pytest.mark.ado_test def test_save_as_html(adr_service_query) -> None: success = False diff --git a/tests/test_report_remote_server.py b/tests/test_report_remote_server.py index c9d4d974..2f2a760b 100755 --- a/tests/test_report_remote_server.py +++ b/tests/test_report_remote_server.py @@ -291,6 +291,43 @@ def test_export_pdf(adr_service_query, get_exec) -> None: assert success is True +@pytest.mark.ado_test +def test_export_pdf_with_filter(adr_service_query, get_exec) -> None: + exec_basis = get_exec + item_filter = "A|i_type|cont|image;" + success = False + try: + my_report = adr_service_query.get_report(report_name="My Top Report") + success = True + except SyntaxError: + success = False + s = adr_service_query.serverobj + if exec_basis: + if environ.get("ANSYS_REL_INT_I"): + ansys_version = int(environ.get("ANSYS_REL_INT_I")) + else: + import re + + matches = re.search(r".*v([0-9]{3}).*", exec_basis) + ansys_version = int(matches.group(1)) + s.export_report_as_pdf( + report_guid=my_report.report.guid, + file_name="mytest", + exec_basis=exec_basis, + ansys_version=ansys_version, + item_filter=item_filter, + ) + else: + # If no local installation, then you can not run the routine for pdf conversion. OSError expected. + try: + s.export_report_as_pdf( + report_guid=my_report.report.guid, file_name="mytest", item_filter=item_filter + ) + except OSError: + success = True + assert success is True + + @pytest.mark.ado_test def test_export_pptx_error(adr_service_query) -> None: my_report = adr_service_query.get_report(report_name="My Top Report")