Skip to content

Commit

Permalink
Merge pull request #1744 from openoereb/logo_ref_hook_method
Browse files Browse the repository at this point in the history
logo ref hook method
  • Loading branch information
svamaa authored Jun 2, 2023
2 parents af72f6e + 4f6f9f0 commit 95a5ce4
Show file tree
Hide file tree
Showing 16 changed files with 296 additions and 54 deletions.
3 changes: 3 additions & 0 deletions dev/config/pyramid_oereb.yml.mako
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,9 @@ 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
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
Expand Down
19 changes: 19 additions & 0 deletions pyramid_oereb/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
"""
Expand Down
41 changes: 41 additions & 0 deletions pyramid_oereb/core/hook_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from functools import cmp_to_key

from pyramid_oereb import route_prefix
from pyramid_oereb.core import get_multilingual_element
from pyramid_oereb.core.records.office import OfficeRecord


Expand Down Expand Up @@ -48,6 +49,46 @@ def get_symbol_ref(request, record):
)


def get_logo_ref(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_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):
"""
Expand Down
40 changes: 40 additions & 0 deletions pyramid_oereb/core/renderer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
69 changes: 30 additions & 39 deletions pyramid_oereb/core/renderer/extract/json_.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pyramid.response import Response
from pyramid.testing import DummyRequest

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
Expand Down Expand Up @@ -119,44 +119,35 @@ def _render(self, extract, param):
})
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
})
'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
)
})

if extract.electronic_signature is not None:
extract_dict['ElectronicSignature'] = extract.electronic_signature
Expand Down
10 changes: 5 additions & 5 deletions pyramid_oereb/core/renderer/extract/templates/xml/extract.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@
<data:ExtractIdentifier>${extract.extract_identifier}</data:ExtractIdentifier>
<data:QRCode>${extract.qr_code.encode()}</data:QRCode>
%else:
<data:LogoPLRCadastreRef>${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}</data:LogoPLRCadastreRef>
<data:FederalLogoRef>${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}</data:FederalLogoRef>
<data:CantonalLogoRef>${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}</data:CantonalLogoRef>
<data:MunicipalityLogoRef>${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}</data:MunicipalityLogoRef>
<data:LogoPLRCadastreRef>${get_logo_ref(request=request, logo_code='oereb', language=language, image_dict=extract.logo_plr_cadastre.image_dict) | x}</data:LogoPLRCadastreRef>
<data:FederalLogoRef>${get_logo_ref(request=request, logo_code='confederation', language=language, image_dict=extract.logo_plr_cadastre.image_dict) | x}</data:FederalLogoRef>
<data:CantonalLogoRef>${get_logo_ref(request=request, logo_code='canton', language=language, image_dict=extract.logo_plr_cadastre.image_dict) | x}</data:CantonalLogoRef>
<data:MunicipalityLogoRef>${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}</data:MunicipalityLogoRef>
<data:ExtractIdentifier>${extract.extract_identifier}</data:ExtractIdentifier>
<data:QRCodeRef>${extract.qr_code_ref}</data:QRCodeRef>
<data:QRCodeRef>${get_qr_code_ref(request=request, qr_code_ref=extract.qr_code_ref) | x}</data:QRCodeRef>
%endif
%for general_information in extract.general_information:
<%include file="general_information.xml" args="general_information=general_information"/>
Expand Down
2 changes: 2 additions & 0 deletions pyramid_oereb/core/renderer/extract/xml_.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
81 changes: 81 additions & 0 deletions tests/core/renderer/test_base.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -206,3 +221,69 @@ 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', [
({
'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)
])
def test_get_qr_code_ref(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"}):
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)
Loading

0 comments on commit 95a5ce4

Please sign in to comment.