From 5331712756c0fe9b524fd40efd2bf20ae5be1281 Mon Sep 17 00:00:00 2001 From: Anne Maier Date: Thu, 4 May 2023 15:32:32 +0200 Subject: [PATCH 01/17] set logo ref hook method --- dev/config/pyramid_oereb.yml.mako | 2 + pyramid_oereb/core/config.py | 19 ++++++++ pyramid_oereb/core/hook_methods.py | 26 +++++++++++ pyramid_oereb/core/readers/extract.py | 4 +- pyramid_oereb/core/records/extract.py | 7 ++- pyramid_oereb/core/renderer/extract/json_.py | 49 ++++---------------- 6 files changed, 66 insertions(+), 41 deletions(-) diff --git a/dev/config/pyramid_oereb.yml.mako b/dev/config/pyramid_oereb.yml.mako index aace2f21ec..923315c581 100644 --- a/dev/config/pyramid_oereb.yml.mako +++ b/dev/config/pyramid_oereb.yml.mako @@ -358,6 +358,8 @@ pyramid_oereb: db_connection: *main_db_connection # The model which maps the logo images database table. model: pyramid_oereb.contrib.data_sources.standard.models.main.Logo + hooks: + get_logo_ref: pyramid_oereb.core.hook_methods.get_logo_ref # The processor of the oereb project joins the document type labels. In the standard configuration this # is assumed to be read from a database. Hint: If you want to read the values out of an existing database diff --git a/pyramid_oereb/core/config.py b/pyramid_oereb/core/config.py index 862e5f8120..ca59c181f9 100644 --- a/pyramid_oereb/core/config.py +++ b/pyramid_oereb/core/config.py @@ -729,6 +729,25 @@ def get_logo_lookup_confederation(): return Config.get_logo_lookup('confederation') + @staticmethod + def get_logo_hooks(): + """ + Returns the hook methods specified in config file. + + Returns: + list: list of hook methods provided in config in dotted names. + + Raises: + ConfigurationError + """ + + logo_config = Config.get_logo_config() + if logo_config is None: + raise ConfigurationError("Missing configuration for logos") + if logo_config.get('hooks') is None: + raise ConfigurationError("Missing configuration for logos hook methods") + return logo_config.get('hooks') + @staticmethod def init_document_types(): """ diff --git a/pyramid_oereb/core/hook_methods.py b/pyramid_oereb/core/hook_methods.py index d11de52279..d63b2ddebe 100644 --- a/pyramid_oereb/core/hook_methods.py +++ b/pyramid_oereb/core/hook_methods.py @@ -3,10 +3,14 @@ from mako.template import Template from pyramid.path import AssetResolver from functools import cmp_to_key +import logging from pyramid_oereb import route_prefix +from pyramid_oereb.core import get_multilingual_element from pyramid_oereb.core.records.office import OfficeRecord +log = logging.getLogger(__name__) + def get_symbol(params, theme_config): """ @@ -47,6 +51,28 @@ def get_symbol_ref(request, record): _query=query ) +def get_logo_refs(request, logo_code, language, image_dict): + """ + Returns the link to the logos. + + Args: + request (pyramid.request.Request): The current request instance. + logo_code (str): Code of logo, eg. bs or ch. + language (str): language of extract. + image_dict (dict): dict of image + + Returns: + uri: the link to the logos. + """ + return request.route_url( + '{0}/image/logo'.format(route_prefix), + logo=logo_code, + language=language, + extension=get_multilingual_element( + image_dict, + language + ).extension + ) def get_surveying_data_provider(real_estate): """ diff --git a/pyramid_oereb/core/readers/extract.py b/pyramid_oereb/core/readers/extract.py index df7b8c2f38..cc6e548ae1 100644 --- a/pyramid_oereb/core/readers/extract.py +++ b/pyramid_oereb/core/readers/extract.py @@ -129,6 +129,7 @@ def read(self, params, real_estate, municipality): canton_logo = Config.get_canton_logo() municipality_logo = Config.get_municipality_logo(municipality.fosnr) qr_code_image = ImageRecord(params.qr_code) + hooks = Config.get_logo_hooks() self.extract = ExtractRecord( real_estate, @@ -143,7 +144,8 @@ def read(self, params, real_estate, municipality): theme_without_data=themes_without_data, general_information=general_information, qr_code=qr_code_image, - qr_code_ref=params.qr_code_ref + qr_code_ref=params.qr_code_ref, + hooks=hooks ) log.debug("read() done") diff --git a/pyramid_oereb/core/records/extract.py b/pyramid_oereb/core/records/extract.py index 0fec9cd247..dbef340031 100644 --- a/pyramid_oereb/core/records/extract.py +++ b/pyramid_oereb/core/records/extract.py @@ -32,6 +32,8 @@ class ExtractRecord(object): pyramid_oereb.core.records.disclaimer.DisclaimerRecord or None): Disclaimers for the extract. glossaries (list of pyramid_oereb.lib.records.glossary.GlossaryRecord): Glossaries for the extract. + hooks (dict of str): The hook methods: get_logo_ref. They have to be provided as + dotted string for further use with dotted name resolver of pyramid package. """ creation_date = None @@ -44,7 +46,7 @@ class ExtractRecord(object): def __init__(self, real_estate, logo_plr_cadastre, federal_logo, cantonal_logo, municipality_logo, plr_cadastre_authority, update_date_os, disclaimers=None, glossaries=None, concerned_theme=None, not_concerned_theme=None, theme_without_data=None, - general_information=None, qr_code=None, qr_code_ref=None): + general_information=None, qr_code=None, qr_code_ref=None, hooks=None): """ Args: real_estate (pyramid_oereb.lib.records.real_estate.RealEstateRecord): The real estate in its @@ -71,6 +73,8 @@ def __init__(self, real_estate, logo_plr_cadastre, federal_logo, cantonal_logo, qr_code (pyramid_oereb.lib.records.image.ImageRecord or None): QR code for the extract as ImageRecord. qr_code_ref (str or None): The URL to retrieve the QR code. + hooks (dict of str): The hook methods: get_logo_ref. They have to be provided as + dotted string for further use with dotted name resolver of pyramid package. """ if not isinstance(update_date_os, datetime): warnings.warn('Type of "update_date_os" should be "datetime.datetime"') @@ -109,3 +113,4 @@ def __init__(self, real_estate, logo_plr_cadastre, federal_logo, cantonal_logo, self.glossaries = [] self.qr_code = qr_code self.qr_code_ref = qr_code_ref + self.hooks = hooks diff --git a/pyramid_oereb/core/renderer/extract/json_.py b/pyramid_oereb/core/renderer/extract/json_.py index 518a6e57fe..2d919c96e6 100644 --- a/pyramid_oereb/core/renderer/extract/json_.py +++ b/pyramid_oereb/core/renderer/extract/json_.py @@ -6,6 +6,7 @@ from pyramid.request import Request from pyramid.response import Response from pyramid.testing import DummyRequest +from pyramid.path import DottedNameResolver from pyramid_oereb import Config, route_prefix from pyramid_oereb.core import get_multilingual_element @@ -118,45 +119,15 @@ def _render(self, extract, param): 'QRCode': extract.qr_code.encode() }) else: - extract_dict.update({ - 'LogoPLRCadastreRef': self._request.route_url( - '{0}/image/logo'.format(route_prefix), - logo='oereb', - language=self._language, - extension=get_multilingual_element( - extract.logo_plr_cadastre.image_dict, - self._language - ).extension - ), - 'FederalLogoRef': self._request.route_url( - '{0}/image/logo'.format(route_prefix), - logo='confederation', - language=self._language, - extension=get_multilingual_element( - extract.federal_logo.image_dict, - self._language - ).extension - ), - 'CantonalLogoRef': self._request.route_url( - '{0}/image/logo'.format(route_prefix), - logo='canton', - language=self._language, - extension=get_multilingual_element( - extract.cantonal_logo.image_dict, - self._language - ).extension - ), - 'MunicipalityLogoRef': self._request.route_url( - '{0}/image/logo'.format(route_prefix), - logo='municipality', - language=self._language, - extension=get_multilingual_element( - extract.municipality_logo.image_dict, - self._language - ).extension - ) + '?fosnr={}'.format(extract.real_estate.fosnr), - 'QRCodeRef': extract.qr_code_ref - }) + method = DottedNameResolver().resolve(str(extract.hooks.get('get_logo_ref'))) + if callable(method): + extract_dict.update({ + 'LogoPLRCadastreRef': method(self._request, 'oereb', self._language, extract.logo_plr_cadastre.image_dict), + 'FederalLogoRef': method(self._request, 'confederation', self._language, extract.federal_logo.image_dict), + 'CantonalLogoRef': method(self._request, 'canton', self._language, extract.cantonal_logo.image_dict), + 'MunicipalityLogoRef': method(self._request, 'municipality', self._language, extract.municipality_logo.image_dict) + '?fosnr={}'.format(extract.real_estate.fosnr), + 'QRCodeRef': extract.qr_code_ref + }) if extract.electronic_signature is not None: extract_dict['ElectronicSignature'] = extract.electronic_signature From 9126197b11110100a8c1590a97a647d43a9a4fd8 Mon Sep 17 00:00:00 2001 From: Anne Maier Date: Thu, 4 May 2023 16:33:18 +0200 Subject: [PATCH 02/17] adjust json extract with new hook methods --- pyramid_oereb/core/hook_methods.py | 14 ++++++++++++ pyramid_oereb/core/renderer/extract/json_.py | 24 ++++++++++++++------ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/pyramid_oereb/core/hook_methods.py b/pyramid_oereb/core/hook_methods.py index d63b2ddebe..f2875ef627 100644 --- a/pyramid_oereb/core/hook_methods.py +++ b/pyramid_oereb/core/hook_methods.py @@ -74,6 +74,20 @@ def get_logo_refs(request, logo_code, language, image_dict): ).extension ) +def get_qr_code_ref(request, qr_code_ref): + """ + Returns the link for the qr_code. + + Args: + request (pyramid.request.Request): The current request instance. + qr_code_ref (str): The string of qr-code url. + + Returns: + uri: the link to the qr_code. + """ + + return qr_code_ref + def get_surveying_data_provider(real_estate): """ diff --git a/pyramid_oereb/core/renderer/extract/json_.py b/pyramid_oereb/core/renderer/extract/json_.py index 2d919c96e6..5ebf747b64 100644 --- a/pyramid_oereb/core/renderer/extract/json_.py +++ b/pyramid_oereb/core/renderer/extract/json_.py @@ -6,6 +6,7 @@ from pyramid.request import Request from pyramid.response import Response from pyramid.testing import DummyRequest +from pyramid.httpexceptions import HTTPServerError from pyramid.path import DottedNameResolver from pyramid_oereb import Config, route_prefix @@ -119,15 +120,24 @@ def _render(self, extract, param): 'QRCode': extract.qr_code.encode() }) else: - method = DottedNameResolver().resolve(str(extract.hooks.get('get_logo_ref'))) - if callable(method): + try: + method_logo = DottedNameResolver().resolve(str(extract.hooks.get('get_logo_ref'))) + method_qr_code = DottedNameResolver().resolve(str(extract.hooks.get('get_qr_code_ref'))) + except ModuleNotFoundError: + log.error('No "get_logo_ref" or "get_qr_code_ref" method found in logo config') + raise HTTPServerError() + + if callable(method_logo) and callable(method_qr_code): extract_dict.update({ - 'LogoPLRCadastreRef': method(self._request, 'oereb', self._language, extract.logo_plr_cadastre.image_dict), - 'FederalLogoRef': method(self._request, 'confederation', self._language, extract.federal_logo.image_dict), - 'CantonalLogoRef': method(self._request, 'canton', self._language, extract.cantonal_logo.image_dict), - 'MunicipalityLogoRef': method(self._request, 'municipality', self._language, extract.municipality_logo.image_dict) + '?fosnr={}'.format(extract.real_estate.fosnr), - 'QRCodeRef': extract.qr_code_ref + 'LogoPLRCadastreRef': method_logo(self._request, 'oereb', self._language, extract.logo_plr_cadastre.image_dict), + 'FederalLogoRef': method_logo(self._request, 'confederation', self._language, extract.federal_logo.image_dict), + 'CantonalLogoRef': method_logo(self._request, 'canton', self._language, extract.cantonal_logo.image_dict), + 'MunicipalityLogoRef': method_logo(self._request, 'municipality', self._language, extract.municipality_logo.image_dict) + '?fosnr={}'.format(extract.real_estate.fosnr), + 'QRCodeRef': method_qr_code(self._request, extract.qr_code_ref) }) + else: + log.error('No "get_logo_ref" or "get_qr_code_ref" method found in logo config') + raise HTTPServerError() if extract.electronic_signature is not None: extract_dict['ElectronicSignature'] = extract.electronic_signature From 61fa8be208dc37699843cbcf3386236534bce7c4 Mon Sep 17 00:00:00 2001 From: Anne Maier Date: Thu, 4 May 2023 16:40:39 +0200 Subject: [PATCH 03/17] linting --- pyramid_oereb/core/config.py | 2 +- pyramid_oereb/core/hook_methods.py | 4 +- pyramid_oereb/core/renderer/extract/json_.py | 43 ++++++++++++++++---- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/pyramid_oereb/core/config.py b/pyramid_oereb/core/config.py index ca59c181f9..e564e1ffed 100644 --- a/pyramid_oereb/core/config.py +++ b/pyramid_oereb/core/config.py @@ -736,7 +736,7 @@ def get_logo_hooks(): Returns: list: list of hook methods provided in config in dotted names. - + Raises: ConfigurationError """ diff --git a/pyramid_oereb/core/hook_methods.py b/pyramid_oereb/core/hook_methods.py index f2875ef627..7625f894e3 100644 --- a/pyramid_oereb/core/hook_methods.py +++ b/pyramid_oereb/core/hook_methods.py @@ -51,6 +51,7 @@ def get_symbol_ref(request, record): _query=query ) + def get_logo_refs(request, logo_code, language, image_dict): """ Returns the link to the logos. @@ -60,7 +61,7 @@ def get_logo_refs(request, logo_code, language, image_dict): logo_code (str): Code of logo, eg. bs or ch. language (str): language of extract. image_dict (dict): dict of image - + Returns: uri: the link to the logos. """ @@ -74,6 +75,7 @@ def get_logo_refs(request, logo_code, language, image_dict): ).extension ) + def get_qr_code_ref(request, qr_code_ref): """ Returns the link for the qr_code. diff --git a/pyramid_oereb/core/renderer/extract/json_.py b/pyramid_oereb/core/renderer/extract/json_.py index 5ebf747b64..4c2e3cbced 100644 --- a/pyramid_oereb/core/renderer/extract/json_.py +++ b/pyramid_oereb/core/renderer/extract/json_.py @@ -9,7 +9,7 @@ from pyramid.httpexceptions import HTTPServerError from pyramid.path import DottedNameResolver -from pyramid_oereb import Config, route_prefix +from pyramid_oereb import Config from pyramid_oereb.core import get_multilingual_element from pyramid_oereb.core.records.documents import DocumentRecord from pyramid_oereb.core.records.theme import ThemeRecord @@ -121,19 +121,46 @@ def _render(self, extract, param): }) else: try: - method_logo = DottedNameResolver().resolve(str(extract.hooks.get('get_logo_ref'))) - method_qr_code = DottedNameResolver().resolve(str(extract.hooks.get('get_qr_code_ref'))) + method_logo = DottedNameResolver().resolve( + str(extract.hooks.get('get_logo_ref')) + ) + method_qr_code = DottedNameResolver().resolve( + str(extract.hooks.get('get_qr_code_ref')) + ) except ModuleNotFoundError: log.error('No "get_logo_ref" or "get_qr_code_ref" method found in logo config') raise HTTPServerError() if callable(method_logo) and callable(method_qr_code): extract_dict.update({ - 'LogoPLRCadastreRef': method_logo(self._request, 'oereb', self._language, extract.logo_plr_cadastre.image_dict), - 'FederalLogoRef': method_logo(self._request, 'confederation', self._language, extract.federal_logo.image_dict), - 'CantonalLogoRef': method_logo(self._request, 'canton', self._language, extract.cantonal_logo.image_dict), - 'MunicipalityLogoRef': method_logo(self._request, 'municipality', self._language, extract.municipality_logo.image_dict) + '?fosnr={}'.format(extract.real_estate.fosnr), - 'QRCodeRef': method_qr_code(self._request, extract.qr_code_ref) + 'LogoPLRCadastreRef': method_logo( + self._request, + 'oereb', + self._language, + extract.logo_plr_cadastre.image_dict + ), + 'FederalLogoRef': method_logo( + self._request, + 'confederation', + self._language, + extract.federal_logo.image_dict + ), + 'CantonalLogoRef': method_logo( + self._request, + 'canton', + self._language, + extract.cantonal_logo.image_dict + ), + 'MunicipalityLogoRef': method_logo( + self._request, + 'municipality', + self._language, + extract.municipality_logo.image_dict + ) + '?fosnr={}'.format(extract.real_estate.fosnr), + 'QRCodeRef': method_qr_code( + self._request, + extract.qr_code_ref + ) }) else: log.error('No "get_logo_ref" or "get_qr_code_ref" method found in logo config') From 1f0962a5b6043f5792af92dae2bc4b7df9d7753a Mon Sep 17 00:00:00 2001 From: Anne Maier Date: Thu, 4 May 2023 16:44:03 +0200 Subject: [PATCH 04/17] add hooks to tests --- dev/config/pyramid_oereb.yml.mako | 1 + .../resources/test_config.yml | 3 +++ .../resources/test_custom_config.yml | 3 +++ tests/resources/test_config.yml | 3 +++ 4 files changed, 10 insertions(+) diff --git a/dev/config/pyramid_oereb.yml.mako b/dev/config/pyramid_oereb.yml.mako index 923315c581..8dd0a58270 100644 --- a/dev/config/pyramid_oereb.yml.mako +++ b/dev/config/pyramid_oereb.yml.mako @@ -360,6 +360,7 @@ pyramid_oereb: model: pyramid_oereb.contrib.data_sources.standard.models.main.Logo hooks: get_logo_ref: pyramid_oereb.core.hook_methods.get_logo_ref + get_qr_code_ref: pyramid_oereb.core.hook_methods.get_qr_code_ref # The processor of the oereb project joins the document type labels. In the standard configuration this # is assumed to be read from a database. Hint: If you want to read the values out of an existing database diff --git a/tests/contrib.print_proxy.mapfish_print/resources/test_config.yml b/tests/contrib.print_proxy.mapfish_print/resources/test_config.yml index b9b2134422..3b1dd144ee 100644 --- a/tests/contrib.print_proxy.mapfish_print/resources/test_config.yml +++ b/tests/contrib.print_proxy.mapfish_print/resources/test_config.yml @@ -22,6 +22,9 @@ pyramid_oereb: params: db_connection: postgresql://postgres:postgres@oereb-db:5432/pyramid_oereb_test model: pyramid_oereb.contrib.data_sources.standard.models.main.Logo + hooks: + get_logo_ref: pyramid_oereb.core.hook_methods.get_logo_ref + get_qr_code_ref: pyramid_oereb.core.hook_methods.get_qr_code_ref document_types: source: diff --git a/tests/contrib.print_proxy.mapfish_print/resources/test_custom_config.yml b/tests/contrib.print_proxy.mapfish_print/resources/test_custom_config.yml index a967257dd6..c253e36031 100644 --- a/tests/contrib.print_proxy.mapfish_print/resources/test_custom_config.yml +++ b/tests/contrib.print_proxy.mapfish_print/resources/test_custom_config.yml @@ -22,6 +22,9 @@ pyramid_oereb: params: db_connection: postgresql://postgres:postgres@oereb-db:5432/pyramid_oereb_test model: pyramid_oereb.standard.models.main.Logo + hooks: + get_logo_ref: pyramid_oereb.core.hook_methods.get_logo_ref + get_qr_code_ref: pyramid_oereb.core.hook_methods.get_qr_code_ref document_types: source: diff --git a/tests/resources/test_config.yml b/tests/resources/test_config.yml index af25f1937a..ad896d48a6 100644 --- a/tests/resources/test_config.yml +++ b/tests/resources/test_config.yml @@ -178,6 +178,9 @@ pyramid_oereb: params: db_connection: *main_db_connection model: pyramid_oereb.contrib.data_sources.standard.models.main.Logo + hooks: + get_logo_ref: pyramid_oereb.core.hook_methods.get_logo_ref + get_qr_code_ref: pyramid_oereb.core.hook_methods.get_qr_code_ref document_types: source: From 30b579110dd74899d0a9f6bb5d5ce1eb55371ffe Mon Sep 17 00:00:00 2001 From: Anne Maier Date: Thu, 4 May 2023 17:47:42 +0200 Subject: [PATCH 05/17] fix setup --- pyramid_oereb/core/hook_methods.py | 2 +- pyramid_oereb/core/readers/extract.py | 4 +- pyramid_oereb/core/records/extract.py | 7 +- pyramid_oereb/core/renderer/__init__.py | 40 +++++++++++ pyramid_oereb/core/renderer/extract/json_.py | 75 ++++++++------------ 5 files changed, 73 insertions(+), 55 deletions(-) diff --git a/pyramid_oereb/core/hook_methods.py b/pyramid_oereb/core/hook_methods.py index 7625f894e3..80b6ed3d29 100644 --- a/pyramid_oereb/core/hook_methods.py +++ b/pyramid_oereb/core/hook_methods.py @@ -52,7 +52,7 @@ def get_symbol_ref(request, record): ) -def get_logo_refs(request, logo_code, language, image_dict): +def get_logo_ref(request, logo_code, language, image_dict): """ Returns the link to the logos. diff --git a/pyramid_oereb/core/readers/extract.py b/pyramid_oereb/core/readers/extract.py index cc6e548ae1..df7b8c2f38 100644 --- a/pyramid_oereb/core/readers/extract.py +++ b/pyramid_oereb/core/readers/extract.py @@ -129,7 +129,6 @@ def read(self, params, real_estate, municipality): canton_logo = Config.get_canton_logo() municipality_logo = Config.get_municipality_logo(municipality.fosnr) qr_code_image = ImageRecord(params.qr_code) - hooks = Config.get_logo_hooks() self.extract = ExtractRecord( real_estate, @@ -144,8 +143,7 @@ def read(self, params, real_estate, municipality): theme_without_data=themes_without_data, general_information=general_information, qr_code=qr_code_image, - qr_code_ref=params.qr_code_ref, - hooks=hooks + qr_code_ref=params.qr_code_ref ) log.debug("read() done") diff --git a/pyramid_oereb/core/records/extract.py b/pyramid_oereb/core/records/extract.py index dbef340031..0fec9cd247 100644 --- a/pyramid_oereb/core/records/extract.py +++ b/pyramid_oereb/core/records/extract.py @@ -32,8 +32,6 @@ class ExtractRecord(object): pyramid_oereb.core.records.disclaimer.DisclaimerRecord or None): Disclaimers for the extract. glossaries (list of pyramid_oereb.lib.records.glossary.GlossaryRecord): Glossaries for the extract. - hooks (dict of str): The hook methods: get_logo_ref. They have to be provided as - dotted string for further use with dotted name resolver of pyramid package. """ creation_date = None @@ -46,7 +44,7 @@ class ExtractRecord(object): def __init__(self, real_estate, logo_plr_cadastre, federal_logo, cantonal_logo, municipality_logo, plr_cadastre_authority, update_date_os, disclaimers=None, glossaries=None, concerned_theme=None, not_concerned_theme=None, theme_without_data=None, - general_information=None, qr_code=None, qr_code_ref=None, hooks=None): + general_information=None, qr_code=None, qr_code_ref=None): """ Args: real_estate (pyramid_oereb.lib.records.real_estate.RealEstateRecord): The real estate in its @@ -73,8 +71,6 @@ def __init__(self, real_estate, logo_plr_cadastre, federal_logo, cantonal_logo, qr_code (pyramid_oereb.lib.records.image.ImageRecord or None): QR code for the extract as ImageRecord. qr_code_ref (str or None): The URL to retrieve the QR code. - hooks (dict of str): The hook methods: get_logo_ref. They have to be provided as - dotted string for further use with dotted name resolver of pyramid package. """ if not isinstance(update_date_os, datetime): warnings.warn('Type of "update_date_os" should be "datetime.datetime"') @@ -113,4 +109,3 @@ def __init__(self, real_estate, logo_plr_cadastre, federal_logo, cantonal_logo, self.glossaries = [] self.qr_code = qr_code self.qr_code_ref = qr_code_ref - self.hooks = hooks diff --git a/pyramid_oereb/core/renderer/__init__.py b/pyramid_oereb/core/renderer/__init__.py index 4b5d73e700..f999cb5708 100644 --- a/pyramid_oereb/core/renderer/__init__.py +++ b/pyramid_oereb/core/renderer/__init__.py @@ -52,6 +52,46 @@ def get_symbol_ref(cls, request, record): log.error('No "get_symbol_ref" method found for theme {}'.format(record.theme.code)) raise HTTPServerError() + @classmethod + def get_logo_ref(cls, request, logo_code, language, image_dict): + """ + Returns the link to the symbol of the specified logo. + + Args: + request (pyramid.request.Request): The current request instance. + logo_code (str): Code of logo, eg. bs or ch. + language (str): language of extract. + image_dict (dict): dict of image + + Returns: + uri: The link to the symbol for the specified logo. + """ + method = None + method = DottedNameResolver().resolve(Config.get_logo_hooks().get('get_logo_ref')) + if callable(method): + return method(request, logo_code, language, image_dict) + log.error('No "get_logo_ref" method found for logos') + raise HTTPServerError() + + @classmethod + def get_qr_code_ref(cls, request, qr_code_ref): + """ + Returns the link for the qr_code. + + Args: + request (pyramid.request.Request): The current request instance. + qr_code_ref (str): The string of qr-code url. + + Returns: + uri: the link to the qr_code. + """ + method = None + method = DottedNameResolver().resolve(Config.get_logo_hooks().get('get_qr_code_ref')) + if callable(method): + return method(request, qr_code_ref) + log.error('No "get_qr_code_ref" method found for logos') + raise HTTPServerError() + @classmethod def get_response(cls, system): """ diff --git a/pyramid_oereb/core/renderer/extract/json_.py b/pyramid_oereb/core/renderer/extract/json_.py index 4c2e3cbced..0e800ad086 100644 --- a/pyramid_oereb/core/renderer/extract/json_.py +++ b/pyramid_oereb/core/renderer/extract/json_.py @@ -9,7 +9,7 @@ from pyramid.httpexceptions import HTTPServerError from pyramid.path import DottedNameResolver -from pyramid_oereb import Config +from pyramid_oereb import Config, route_prefix from pyramid_oereb.core import get_multilingual_element from pyramid_oereb.core.records.documents import DocumentRecord from pyramid_oereb.core.records.theme import ThemeRecord @@ -120,51 +120,36 @@ def _render(self, extract, param): 'QRCode': extract.qr_code.encode() }) else: - try: - method_logo = DottedNameResolver().resolve( - str(extract.hooks.get('get_logo_ref')) - ) - method_qr_code = DottedNameResolver().resolve( - str(extract.hooks.get('get_qr_code_ref')) - ) - except ModuleNotFoundError: - log.error('No "get_logo_ref" or "get_qr_code_ref" method found in logo config') - raise HTTPServerError() - - if callable(method_logo) and callable(method_qr_code): - extract_dict.update({ - 'LogoPLRCadastreRef': method_logo( - self._request, - 'oereb', - self._language, - extract.logo_plr_cadastre.image_dict - ), - 'FederalLogoRef': method_logo( - self._request, - 'confederation', - self._language, - extract.federal_logo.image_dict - ), - 'CantonalLogoRef': method_logo( - self._request, - 'canton', - self._language, - extract.cantonal_logo.image_dict - ), - 'MunicipalityLogoRef': method_logo( - self._request, - 'municipality', - self._language, - extract.municipality_logo.image_dict - ) + '?fosnr={}'.format(extract.real_estate.fosnr), - 'QRCodeRef': method_qr_code( - self._request, - extract.qr_code_ref - ) + extract_dict.update({ + 'LogoPLRCadastreRef': self.get_logo_ref( + self._request, + 'oereb', + self._language, + extract.logo_plr_cadastre.image_dict + ), + 'FederalLogoRef': self.get_logo_ref( + self._request, + 'confederation', + self._language, + extract.federal_logo.image_dict + ), + 'CantonalLogoRef': self.get_logo_ref( + self._request, + 'canton', + self._language, + extract.cantonal_logo.image_dict + ), + 'MunicipalityLogoRef': self.get_logo_ref( + self._request, + 'municipality', + self._language, + extract.municipality_logo.image_dict + ) + '?fosnr={}'.format(extract.real_estate.fosnr), + 'QRCodeRef': self.get_qr_code_ref( + self._request, + extract.qr_code_ref + ) }) - else: - log.error('No "get_logo_ref" or "get_qr_code_ref" method found in logo config') - raise HTTPServerError() if extract.electronic_signature is not None: extract_dict['ElectronicSignature'] = extract.electronic_signature From cb500185761c5fa6f465a98ce77bd217785dcc71 Mon Sep 17 00:00:00 2001 From: Anne Maier Date: Mon, 8 May 2023 14:08:53 +0200 Subject: [PATCH 06/17] fix linting --- pyramid_oereb/core/hook_methods.py | 3 ++- pyramid_oereb/core/renderer/__init__.py | 2 +- pyramid_oereb/core/renderer/extract/json_.py | 6 ++---- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/pyramid_oereb/core/hook_methods.py b/pyramid_oereb/core/hook_methods.py index 80b6ed3d29..e6cf500177 100644 --- a/pyramid_oereb/core/hook_methods.py +++ b/pyramid_oereb/core/hook_methods.py @@ -83,13 +83,14 @@ def get_qr_code_ref(request, qr_code_ref): Args: request (pyramid.request.Request): The current request instance. qr_code_ref (str): The string of qr-code url. - + Returns: uri: the link to the qr_code. """ return qr_code_ref + def get_surveying_data_provider(real_estate): """ diff --git a/pyramid_oereb/core/renderer/__init__.py b/pyramid_oereb/core/renderer/__init__.py index f999cb5708..8ffb433d13 100644 --- a/pyramid_oereb/core/renderer/__init__.py +++ b/pyramid_oereb/core/renderer/__init__.py @@ -81,7 +81,7 @@ def get_qr_code_ref(cls, request, qr_code_ref): Args: request (pyramid.request.Request): The current request instance. qr_code_ref (str): The string of qr-code url. - + Returns: uri: the link to the qr_code. """ diff --git a/pyramid_oereb/core/renderer/extract/json_.py b/pyramid_oereb/core/renderer/extract/json_.py index 0e800ad086..82e4331b18 100644 --- a/pyramid_oereb/core/renderer/extract/json_.py +++ b/pyramid_oereb/core/renderer/extract/json_.py @@ -6,10 +6,8 @@ from pyramid.request import Request from pyramid.response import Response from pyramid.testing import DummyRequest -from pyramid.httpexceptions import HTTPServerError -from pyramid.path import DottedNameResolver -from pyramid_oereb import Config, route_prefix +from pyramid_oereb import Config from pyramid_oereb.core import get_multilingual_element from pyramid_oereb.core.records.documents import DocumentRecord from pyramid_oereb.core.records.theme import ThemeRecord @@ -122,7 +120,7 @@ def _render(self, extract, param): else: extract_dict.update({ 'LogoPLRCadastreRef': self.get_logo_ref( - self._request, + self._request, 'oereb', self._language, extract.logo_plr_cadastre.image_dict From 8c7889b9a98cdde61f6d6ee9da276885bb6bd847 Mon Sep 17 00:00:00 2001 From: Anne Maier Date: Mon, 8 May 2023 15:59:35 +0200 Subject: [PATCH 07/17] ignore linting error import --- pyramid_oereb/core/renderer/extract/json_.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyramid_oereb/core/renderer/extract/json_.py b/pyramid_oereb/core/renderer/extract/json_.py index 82e4331b18..e82cbfc582 100644 --- a/pyramid_oereb/core/renderer/extract/json_.py +++ b/pyramid_oereb/core/renderer/extract/json_.py @@ -7,7 +7,7 @@ from pyramid.response import Response from pyramid.testing import DummyRequest -from pyramid_oereb import Config +from pyramid_oereb import Config, route_prefix # noqa: F401 from pyramid_oereb.core import get_multilingual_element from pyramid_oereb.core.records.documents import DocumentRecord from pyramid_oereb.core.records.theme import ThemeRecord From 7e2149a9d4b53d109d693ab328e73dcc1d0ce6aa Mon Sep 17 00:00:00 2001 From: Anne Maier Date: Tue, 9 May 2023 10:35:09 +0200 Subject: [PATCH 08/17] add json test for hook method get_logo_ref --- pyramid_oereb/core/hook_methods.py | 1 + tests/core/test_hook_methods.py | 36 +++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/pyramid_oereb/core/hook_methods.py b/pyramid_oereb/core/hook_methods.py index e6cf500177..51387d1cea 100644 --- a/pyramid_oereb/core/hook_methods.py +++ b/pyramid_oereb/core/hook_methods.py @@ -65,6 +65,7 @@ def get_logo_ref(request, logo_code, language, image_dict): Returns: uri: the link to the logos. """ + return request.route_url( '{0}/image/logo'.format(route_prefix), logo=logo_code, diff --git a/tests/core/test_hook_methods.py b/tests/core/test_hook_methods.py index bdb75786a0..2b69362609 100644 --- a/tests/core/test_hook_methods.py +++ b/tests/core/test_hook_methods.py @@ -1,3 +1,4 @@ +import io from pyramid_oereb.core.records.extract import ExtractRecord from pyramid_oereb.core.records.law_status import LawStatusRecord import pytest @@ -5,6 +6,7 @@ from shapely.wkt import loads from unittest.mock import patch +from PIL import Image from pyramid.testing import DummyRequest @@ -15,7 +17,7 @@ from pyramid_oereb.core.records.view_service import LegendEntryRecord from pyramid_oereb.core.records.real_estate import RealEstateRecord from pyramid_oereb.core.hook_methods import compare, get_symbol, get_symbol_ref, \ - get_surveying_data_update_date, plr_sort_within_themes + get_logo_ref, get_surveying_data_update_date, plr_sort_within_themes from pyramid_oereb.contrib.data_sources.standard.sources.plr import StandardThemeConfigParser import pyramid_oereb.contrib.data_sources.standard.hook_methods from tests.core.records.test_extract import create_dummy_extract @@ -61,6 +63,18 @@ def legend_entry_data(pyramid_oereb_test_config, dbsession, transact, file_adapt yield legend_entries +@pytest.fixture +def png_image(): + yield Image.new("RGB", (72, 36), (128, 128, 128)) + + +@pytest.fixture +def png_binary(png_image): + output = io.BytesIO() + png_image.save(output, format='PNG') + yield output.getvalue() + + def test_get_symbol(): with pytest.raises(NotImplementedError): binary_image, content_type = get_symbol({'identifier': "1"}, {}) @@ -82,6 +96,26 @@ def test_get_symbol_ref(pyramid_test_config): assert url.path == '/image/symbol/ch.BelasteteStandorte/legend_entry.png' +@pytest.mark.parametrize('test_value, expected_results', [ + ({ + 'logo_code': 'ch', + 'language': 'de', + }, '/image/logo/ch/de.png'), + ({ + 'logo_code': 'bs', + 'language': 'fr', + }, '/image/logo/bs/fr.png') + ]) +def test_get_logo_ref(test_value, expected_results, png_binary): + request = DummyRequest() + url = urlparse(get_logo_ref(request, + test_value.get('logo_code'), + test_value.get('language'), + {test_value.get('language'): ImageRecord(png_binary)} + )) + assert url.path == expected_results + + def test_get_surveying_data_date(): real_estate = RealEstateRecord('test_type', 'BL', 'Nusshof', 1, 100, loads('POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))')) From 976ec1753060aef6ab431136a430629564060215 Mon Sep 17 00:00:00 2001 From: Anne Maier Date: Tue, 9 May 2023 11:13:01 +0200 Subject: [PATCH 09/17] fix tests --- pyramid_oereb/core/renderer/extract/json_.py | 2 +- .../test_mapfish_print.py | 1 - tests/core/renderer/test_json.py | 7 ------- tests/core/webservice/test_getextractbyid.py | 1 - 4 files changed, 1 insertion(+), 10 deletions(-) diff --git a/pyramid_oereb/core/renderer/extract/json_.py b/pyramid_oereb/core/renderer/extract/json_.py index e82cbfc582..82e4331b18 100644 --- a/pyramid_oereb/core/renderer/extract/json_.py +++ b/pyramid_oereb/core/renderer/extract/json_.py @@ -7,7 +7,7 @@ from pyramid.response import Response from pyramid.testing import DummyRequest -from pyramid_oereb import Config, route_prefix # noqa: F401 +from pyramid_oereb import Config from pyramid_oereb.core import get_multilingual_element from pyramid_oereb.core.records.documents import DocumentRecord from pyramid_oereb.core.records.theme import ThemeRecord 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 c6c3c56c55..732f3cfafc 100644 --- a/tests/contrib.print_proxy.mapfish_print/test_mapfish_print.py +++ b/tests/contrib.print_proxy.mapfish_print/test_mapfish_print.py @@ -847,7 +847,6 @@ def dummy_pdf(): @patch.object(pyramid_oereb.core.views.webservice, 'route_prefix', 'oereb') -@patch.object(pyramid_oereb.core.renderer.extract.json_, 'route_prefix', 'oereb') @patch.object(pyramid_oereb.core.config.Config, 'municipalities', [MunicipalityRecord(1234, 'test', True)]) def test_mfp_service(mock_responses, pyramid_test_config, real_estate_data, diff --git a/tests/core/renderer/test_json.py b/tests/core/renderer/test_json.py index 181d62f30f..790706f34c 100644 --- a/tests/core/renderer/test_json.py +++ b/tests/core/renderer/test_json.py @@ -3,7 +3,6 @@ import datetime import pytest -from unittest.mock import patch from shapely.geometry import MultiPolygon, Polygon, Point, LineString from pyramid.path import DottedNameResolver @@ -30,9 +29,6 @@ from tests.mockrequest import MockRequest from pyramid_oereb.core.views.webservice import Parameter -import pyramid_oereb.core.renderer.extract.json_ -import pyramid_oereb.core.hook_methods - def law_status(): return LawStatusRecord(u'inKraft', {u'de': u'Rechtskräftig'}) @@ -58,7 +54,6 @@ def glossary_expected(): }] -@patch.object(pyramid_oereb.core.renderer.extract.json_, 'route_prefix', 'oereb') @pytest.mark.parametrize('parameter, glossaries_input, glossaries_expected', [ (default_param(), glossary_input(), glossary_expected()), (default_param(), [], []), @@ -230,7 +225,6 @@ def test_format_real_estate(DummyRenderInfo, real_estate_test_data): } -@patch.object(pyramid_oereb.core.hook_methods, 'route_prefix', 'oereb') @pytest.mark.parametrize('parameter', [ default_param(), Parameter('json', False, True, False, 'BL0200002829', '1000', 'CH775979211712', 'de'), @@ -504,7 +498,6 @@ def test_format_theme(DummyRenderInfo, params): } -@patch.object(pyramid_oereb.core.hook_methods, 'route_prefix', 'oereb') @pytest.mark.parametrize('parameter', [ default_param(), Parameter('json', 'reduced', False, True, 'BL0200002829', '1000', 'CH775979211712', 'de') diff --git a/tests/core/webservice/test_getextractbyid.py b/tests/core/webservice/test_getextractbyid.py index 54a5340a49..26f0199cb1 100644 --- a/tests/core/webservice/test_getextractbyid.py +++ b/tests/core/webservice/test_getextractbyid.py @@ -148,7 +148,6 @@ def test_return_no_content(): @patch.object(pyramid_oereb.core.hook_methods, 'route_prefix', 'oereb') -@patch.object(pyramid_oereb.core.renderer.extract.json_, 'route_prefix', 'oereb') @patch.object(pyramid_oereb.core.views.webservice, 'route_prefix', 'oereb') @patch.object(MockRequest, 'route_url', lambda *args, **kwargs: '') @pytest.mark.parametrize('egrid,topics', [ From 64cca38ec77c8316eab013743bfd53ee82d68f2c Mon Sep 17 00:00:00 2001 From: Anne Maier Date: Tue, 9 May 2023 11:52:14 +0200 Subject: [PATCH 10/17] test_config.py get_logo_ref --- tests/core/test_config.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/core/test_config.py b/tests/core/test_config.py index 85768ec311..45b0097a7f 100644 --- a/tests/core/test_config.py +++ b/tests/core/test_config.py @@ -96,6 +96,26 @@ def mock_read_logos(): assert Config.logos is None +@pytest.mark.run(order=-1) +def test_get_logo_hooks(): + with patch.object(Config, 'get_logo_config', return_value={"hooks": []}): + Config._config = None + assert Config.get_logo_hooks() == [] + + +@pytest.mark.parametrize('test_value', [ + ({"logos": [{"hooks": []}]}), + ({"logos": [{"not_expecting_key": []}]}), + (None) +]) +@pytest.mark.run(order=-1) +def test_get_logo_hooks_none(test_value): + with patch.object(Config, 'get_logo_config', return_value=test_value): + Config._config = None + with pytest.raises(ConfigurationError): + Config.get_logo_hooks() + + @pytest.mark.run(order=-1) def test_get_all_federal(config_path): Config._config = None From 70ba8857827776c0f6737b4f1f7a7b836ba369d2 Mon Sep 17 00:00:00 2001 From: Anne Maier Date: Tue, 9 May 2023 14:42:26 +0200 Subject: [PATCH 11/17] add test for get_qr_code_ref --- tests/core/test_hook_methods.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/core/test_hook_methods.py b/tests/core/test_hook_methods.py index 2b69362609..80a5d85c6b 100644 --- a/tests/core/test_hook_methods.py +++ b/tests/core/test_hook_methods.py @@ -17,7 +17,8 @@ from pyramid_oereb.core.records.view_service import LegendEntryRecord from pyramid_oereb.core.records.real_estate import RealEstateRecord from pyramid_oereb.core.hook_methods import compare, get_symbol, get_symbol_ref, \ - get_logo_ref, get_surveying_data_update_date, plr_sort_within_themes + get_logo_ref, get_qr_code_ref, get_surveying_data_update_date,\ + plr_sort_within_themes from pyramid_oereb.contrib.data_sources.standard.sources.plr import StandardThemeConfigParser import pyramid_oereb.contrib.data_sources.standard.hook_methods from tests.core.records.test_extract import create_dummy_extract @@ -116,6 +117,15 @@ def test_get_logo_ref(test_value, expected_results, png_binary): assert url.path == expected_results +@pytest.mark.parametrize('test_value, expected_results', [ + ('', ''), + (None, None) + ]) +def test_get_qr_code_ref(test_value, expected_results): + request = DummyRequest() + assert get_qr_code_ref(request, test_value) == expected_results + + def test_get_surveying_data_date(): real_estate = RealEstateRecord('test_type', 'BL', 'Nusshof', 1, 100, loads('POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))')) From f19889dd919a2319577aca56af19e3ee2e7ecdf2 Mon Sep 17 00:00:00 2001 From: Anne Maier Date: Tue, 9 May 2023 15:48:23 +0200 Subject: [PATCH 12/17] add tests for renderer.init --- tests/core/renderer/test_base.py | 47 ++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/tests/core/renderer/test_base.py b/tests/core/renderer/test_base.py index 9b5322eee5..643bf1c5b8 100644 --- a/tests/core/renderer/test_base.py +++ b/tests/core/renderer/test_base.py @@ -1,8 +1,11 @@ # -*- coding: utf-8 -*- +import io +from urllib.parse import urlparse import pytest from unittest.mock import patch import datetime +from PIL import Image from pyramid.httpexceptions import HTTPServerError, HTTPInternalServerError from pyramid.response import Response @@ -19,6 +22,18 @@ from tests.mockrequest import MockRequest +@pytest.fixture +def png_image(): + yield Image.new("RGB", (72, 36), (128, 128, 128)) + + +@pytest.fixture +def png_binary(png_image): + output = io.BytesIO() + png_image.save(output, format='PNG') + yield output.getvalue() + + def test_call(DummyRenderInfo, pyramid_oereb_test_config): renderer = Base(DummyRenderInfo()) assert isinstance(renderer.info, DummyRenderInfo) @@ -206,3 +221,35 @@ def test_get_symbol_ref(theme_code, pyramid_test_config, pyramid_oereb_test_conf assert ref == 'http://example.com/image/symbol/{}/legend_entry.svg?identifier=1'.format( theme_code ) + + +@pytest.mark.parametrize('test_value, expected_results', [ + ({ + 'logo_code': 'ch', + 'language': 'de', + }, '/image/logo/ch/de.png'), + ({ + 'logo_code': 'bs', + 'language': 'fr', + }, '/image/logo/bs/fr.png') + ]) +def test_get_logo_ref(test_value, expected_results, png_binary): + with patch.object(Config, 'get_logo_hooks', + return_value={"get_logo_ref": "pyramid_oereb.core.hook_methods.get_logo_ref"}): + request = DummyRequest() + url = urlparse(Base.get_logo_ref(request, + test_value.get('logo_code'), + test_value.get('language'), + {test_value.get('language'): ImageRecord(png_binary)})) + assert url.path == expected_results + + +@pytest.mark.parametrize('test_value, expected_results', [ + ('', ''), + (None, None) + ]) +def test_get_qr_code_ref(test_value, expected_results): + with patch.object(Config, 'get_logo_hooks', + return_value={"get_logo_ref": "pyramid_oereb.core.hook_methods.get_qr_code_ref"}): + request = DummyRequest() + assert Base.get_qr_code_ref(request, test_value) == expected_results From efdb210058e6092034e19a546a0412ac97076ea5 Mon Sep 17 00:00:00 2001 From: Anne Maier Date: Tue, 9 May 2023 16:00:45 +0200 Subject: [PATCH 13/17] fix test --- tests/core/renderer/test_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/core/renderer/test_base.py b/tests/core/renderer/test_base.py index 643bf1c5b8..1a739b66af 100644 --- a/tests/core/renderer/test_base.py +++ b/tests/core/renderer/test_base.py @@ -250,6 +250,6 @@ def test_get_logo_ref(test_value, expected_results, png_binary): ]) def test_get_qr_code_ref(test_value, expected_results): with patch.object(Config, 'get_logo_hooks', - return_value={"get_logo_ref": "pyramid_oereb.core.hook_methods.get_qr_code_ref"}): + return_value={"get_qr_code_ref": "pyramid_oereb.core.hook_methods.get_qr_code_ref"}): request = DummyRequest() assert Base.get_qr_code_ref(request, test_value) == expected_results From 2dde75bdab65f4b71668f362a6ef93a5345553c2 Mon Sep 17 00:00:00 2001 From: Anne Maier Date: Wed, 10 May 2023 08:20:27 +0200 Subject: [PATCH 14/17] add tests for method not callable --- tests/core/renderer/test_base.py | 34 ++++++++++++++++++++++++++++++++ tests/core/test_hook_methods.py | 1 + 2 files changed, 35 insertions(+) diff --git a/tests/core/renderer/test_base.py b/tests/core/renderer/test_base.py index 1a739b66af..2366b32588 100644 --- a/tests/core/renderer/test_base.py +++ b/tests/core/renderer/test_base.py @@ -244,6 +244,27 @@ def test_get_logo_ref(test_value, expected_results, png_binary): assert url.path == expected_results +@pytest.mark.parametrize('test_value, expected_results', [ + ({ + 'logo_code': 'ch', + 'language': 'de', + }, '/image/logo/ch/de.png'), + ({ + 'logo_code': 'bs', + 'language': 'fr', + }, '/image/logo/bs/fr.png') + ]) +def test_get_logo_ref_no_method(test_value, expected_results, png_binary): + with patch.object(Config, 'get_logo_hooks', + return_value={"get_logo_ref": "pyramid_oereb.core.hook_methods.get_logo_ref"}): + with patch.object(pyramid_oereb.core.hook_methods, 'get_logo_ref', {}): + with pytest.raises(HTTPServerError): + Base.get_logo_ref(DummyRequest(), + test_value.get('logo_code'), + test_value.get('language'), + {test_value.get('language'): ImageRecord(png_binary)}) + + @pytest.mark.parametrize('test_value, expected_results', [ ('', ''), (None, None) @@ -253,3 +274,16 @@ def test_get_qr_code_ref(test_value, expected_results): return_value={"get_qr_code_ref": "pyramid_oereb.core.hook_methods.get_qr_code_ref"}): request = DummyRequest() assert Base.get_qr_code_ref(request, test_value) == expected_results + + +@pytest.mark.parametrize('test_value, expected_results', [ + ('', '') + ]) +def test_get_qr_code_ref_no_method(test_value, expected_results): + with patch.object(Config, 'get_logo_hooks', + return_value={ + "get_qr_code_ref": "pyramid_oereb.core.hook_methods.get_qr_code_ref" + }): + with patch.object(pyramid_oereb.core.hook_methods, 'get_qr_code_ref', {}): + with pytest.raises(HTTPServerError): + Base.get_qr_code_ref(DummyRequest(), test_value) diff --git a/tests/core/test_hook_methods.py b/tests/core/test_hook_methods.py index 80a5d85c6b..5dc317c733 100644 --- a/tests/core/test_hook_methods.py +++ b/tests/core/test_hook_methods.py @@ -119,6 +119,7 @@ def test_get_logo_ref(test_value, expected_results, png_binary): @pytest.mark.parametrize('test_value, expected_results', [ ('', ''), + ({}, {}), (None, None) ]) def test_get_qr_code_ref(test_value, expected_results): From c03c86b35935ec87d6e4443af5b9d79de8238d28 Mon Sep 17 00:00:00 2001 From: Anne Maier Date: Wed, 10 May 2023 08:27:46 +0200 Subject: [PATCH 15/17] fix linting --- tests/core/renderer/test_base.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/core/renderer/test_base.py b/tests/core/renderer/test_base.py index 2366b32588..e1f4bb9a7f 100644 --- a/tests/core/renderer/test_base.py +++ b/tests/core/renderer/test_base.py @@ -260,9 +260,9 @@ def test_get_logo_ref_no_method(test_value, expected_results, png_binary): with patch.object(pyramid_oereb.core.hook_methods, 'get_logo_ref', {}): with pytest.raises(HTTPServerError): Base.get_logo_ref(DummyRequest(), - test_value.get('logo_code'), - test_value.get('language'), - {test_value.get('language'): ImageRecord(png_binary)}) + test_value.get('logo_code'), + test_value.get('language'), + {test_value.get('language'): ImageRecord(png_binary)}) @pytest.mark.parametrize('test_value, expected_results', [ From f23fa6600e3126b852e6c9363b6fbc39764a00cd Mon Sep 17 00:00:00 2001 From: Anne Maier Date: Wed, 10 May 2023 12:33:34 +0200 Subject: [PATCH 16/17] make xml extract use hook methods --- .../core/renderer/extract/templates/xml/extract.xml | 10 +++++----- pyramid_oereb/core/renderer/extract/xml_.py | 2 ++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/pyramid_oereb/core/renderer/extract/templates/xml/extract.xml b/pyramid_oereb/core/renderer/extract/templates/xml/extract.xml index 7941212dcf..2b76efcb47 100644 --- a/pyramid_oereb/core/renderer/extract/templates/xml/extract.xml +++ b/pyramid_oereb/core/renderer/extract/templates/xml/extract.xml @@ -65,12 +65,12 @@ ${extract.extract_identifier} ${extract.qr_code.encode()} %else: - ${request.route_url('{0}/image/logo'.format(route_prefix), logo='oereb', language=language, extension=get_multilingual_element(extract.logo_plr_cadastre.image_dict, language).extension) | x} - ${request.route_url('{0}/image/logo'.format(route_prefix), logo='confederation', language=language, extension=get_multilingual_element(extract.federal_logo.image_dict, language).extension) | x} - ${request.route_url('{0}/image/logo'.format(route_prefix), logo='canton', language=language, extension=get_multilingual_element(extract.cantonal_logo.image_dict, language).extension) | x} - ${request.route_url('{0}/image/logo'.format(route_prefix), logo='municipality', language=language, extension=get_multilingual_element(extract.municipality_logo.image_dict, language).extension) + '?fosnr={}'.format(extract.real_estate.fosnr) | x} + ${get_logo_ref(request=request, logo_code='oereb', language=language, image_dict=extract.logo_plr_cadastre.image_dict) | x} + ${get_logo_ref(request=request, logo_code='confederation', language=language, image_dict=extract.logo_plr_cadastre.image_dict) | x} + ${get_logo_ref(request=request, logo_code='canton', language=language, image_dict=extract.logo_plr_cadastre.image_dict) | x} + ${get_logo_ref(request=request, logo_code='municipality', language=language, image_dict=extract.logo_plr_cadastre.image_dict) + '?fosnr={}'.format(extract.real_estate.fosnr) | x} ${extract.extract_identifier} - ${extract.qr_code_ref} + ${get_qr_code_ref(request=request, qr_code_ref=extract.qr_code_ref) | x} %endif %for general_information in extract.general_information: <%include file="general_information.xml" args="general_information=general_information"/> diff --git a/pyramid_oereb/core/renderer/extract/xml_.py b/pyramid_oereb/core/renderer/extract/xml_.py index afa75bee93..83851c0b2a 100644 --- a/pyramid_oereb/core/renderer/extract/xml_.py +++ b/pyramid_oereb/core/renderer/extract/xml_.py @@ -83,6 +83,8 @@ def _render(self, extract, params): 'get_localized_image': self.get_localized_image, 'request': self._request, 'get_symbol_ref': self.get_symbol_ref, + 'get_logo_ref': self.get_logo_ref, + 'get_qr_code_ref': self.get_qr_code_ref, 'date_format': '%Y-%m-%dT%H:%M:%S' }) return content From cf5d6fbcc75838e3e57ceeded40f151c263cad2c Mon Sep 17 00:00:00 2001 From: Anne Maier Date: Wed, 10 May 2023 14:21:12 +0200 Subject: [PATCH 17/17] remove logging from hook method --- pyramid_oereb/core/hook_methods.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/pyramid_oereb/core/hook_methods.py b/pyramid_oereb/core/hook_methods.py index 51387d1cea..a2993e4d43 100644 --- a/pyramid_oereb/core/hook_methods.py +++ b/pyramid_oereb/core/hook_methods.py @@ -3,14 +3,11 @@ from mako.template import Template from pyramid.path import AssetResolver from functools import cmp_to_key -import logging from pyramid_oereb import route_prefix from pyramid_oereb.core import get_multilingual_element from pyramid_oereb.core.records.office import OfficeRecord -log = logging.getLogger(__name__) - def get_symbol(params, theme_config): """