diff --git a/.travis.yml b/.travis.yml index ce6b33e..9061cab 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,11 +1,9 @@ sudo: false language: python python: - - 3.6 - - 3.7 - 3.8 install: - - pip install -r requirements.txt + - pip install -r requirements-dev.txt - python setup.py develop - pip install nose coverage nose-testconfig coveralls webtest script: diff --git a/CHANGES.rst b/CHANGES.rst index 8967f8e..032ae7f 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,10 +1,15 @@ +1.2.0 (13-04-2023) +------------------ + +- Drop python < 3.8 support + 1.1.0 (30-03-2023) -------------------- +------------------ - Overschakelen naar nieuwe AGIV services (#164) 1.0.0 (13-04-2022) -------------------- +------------------ - Upgrade naar Pyramid 2.0 (#144) diff --git a/crabpy_pyramid/__init__.py b/crabpy_pyramid/__init__.py index f9b9eda..6c2e05e 100644 --- a/crabpy_pyramid/__init__.py +++ b/crabpy_pyramid/__init__.py @@ -4,21 +4,25 @@ import os from collections import Sequence +from crabpy.client import AdressenRegisterClient from crabpy.client import crab_factory +from crabpy.gateway.adressenregister import Gateway from crabpy.gateway.capakey import CapakeyRestGateway from crabpy.gateway.crab import CrabGateway from pyramid.config import Configurator from pyramid.settings import asbool from zope.interface import Interface -from crabpy_pyramid.renderers.capakey import ( - json_list_renderer as capakey_json_list_renderer, +from crabpy_pyramid.renderers.adressenregister import \ + json_item_renderer as adresreg_json_item_renderer +from crabpy_pyramid.renderers.adressenregister import \ + json_list_renderer as adresreg_json_list_renderer +from crabpy_pyramid.renderers.capakey import \ json_item_renderer as capakey_json_item_renderer -) -from crabpy_pyramid.renderers.crab import ( - json_list_renderer as crab_json_list_renderer, - json_item_renderer as crab_json_item_renderer -) +from crabpy_pyramid.renderers.capakey import \ + json_list_renderer as capakey_json_list_renderer +from crabpy_pyramid.renderers.crab import json_item_renderer as crab_json_item_renderer +from crabpy_pyramid.renderers.crab import json_list_renderer as crab_json_list_renderer log = logging.getLogger(__name__) GENERATE_ETAG_ROUTE_NAMES = set() @@ -32,16 +36,27 @@ class ICrab(Interface): pass +class IAdressenregister(Interface): + pass + def _parse_settings(settings): defaults = { 'capakey.include': False, 'crab.include': True, - 'cache.file.root': '/tmp/dogpile_data' + 'adressenregister.include': True, + 'adressenregister.base_url': 'https://api.basisregisters.vlaanderen.be', + 'adressenregister.api_key': None, + 'cache.file.root': '/tmp/dogpile_data', } args = defaults.copy() + if 'adressenregister.api_key' not in settings: + log.warning( + "No adressenregister.api_key set in settings. " + "The api might stop working after reaching the limit of x requests per day." + ) # booelean settings - for short_key_name in ('capakey.include', 'crab.include'): + for short_key_name in ('capakey.include', 'crab.include', 'adressenregister.include'): key_name = "crabpy.%s" % short_key_name if key_name in settings: args[short_key_name] = asbool(settings.get( @@ -49,13 +64,23 @@ def _parse_settings(settings): )) # string setting - for short_key_name in ('proxy.http', 'proxy.https', 'cache.file.root'): + for short_key_name in ( + 'proxy.http', + 'proxy.https', + 'cache.file.root', + 'adressenregister.base_url', + 'adressenregister.api_key' + ): key_name = "crabpy.%s" % short_key_name if key_name in settings: args[short_key_name] = settings.get(key_name) # cache configuration - for short_key_name in ('crab.cache_config', 'capakey.cache_config'): + for short_key_name in ( + 'crab.cache_config', + 'capakey.cache_config', + 'adressenregister.cache_config' + ): key_name = "crabpy.%s." % short_key_name cache_config = {} for skey in settings.keys(): @@ -65,7 +90,7 @@ def _parse_settings(settings): args[short_key_name] = cache_config # crab wsdl settings - for short_key_name in ('crab.wsdl', ): + for short_key_name in ('crab.wsdl',): key_name = "crabpy.%s" % short_key_name if key_name in settings: args[short_key_name] = settings.get(key_name) @@ -122,6 +147,24 @@ def _build_crab(registry, settings): return registry.queryUtility(ICrab) +def _build_adressenregister(registry, settings): + adressenregister = registry.queryUtility(IAdressenregister) + if adressenregister is not None: + return adressenregister + if 'cache_config' in settings: + cache_config = settings['cache_config'] + del settings['cache_config'] + else: + cache_config = None + gateway = Gateway( + client=AdressenRegisterClient(settings["base_url"], settings["api_key"]), + cache_settings=cache_config + ) + + registry.registerUtility(gateway, IAdressenregister) + return registry.queryUtility(IAdressenregister) + + def get_capakey(registry): """ Get the Capakey Gateway @@ -151,6 +194,21 @@ def get_crab(registry): return regis.queryUtility(ICrab) +def get_adressenregister(registry): + """ + Get the Adresssenregister Gateway + + :rtype: :class:`crabpy.gateway.adressenregister.Gateway` + # argument might be a config or a request + """ + # argument might be a config or a request + regis = getattr(registry, 'registry', None) + if regis is None: + regis = registry + + return regis.queryUtility(IAdressenregister) + + def _get_proxy_settings(settings): base_settings = {} http = settings.get('proxy.http', None) @@ -260,6 +318,23 @@ def includeme(config): config.include('crabpy_pyramid.routes.crab') config.scan('crabpy_pyramid.views.crab') + # adressenregister wordt afgekort tot adresreg + adresreg_settings = dict( + _filter_settings(settings, 'adressenregister.'), + **base_settings + ) + + if adresreg_settings['include']: + log.info('Adding adressen register Gateway.') + del adresreg_settings['include'] + config.add_renderer('adresreg_listjson', adresreg_json_list_renderer) + config.add_renderer('adresreg_itemjson', adresreg_json_item_renderer) + _build_adressenregister(config.registry, adresreg_settings) + config.add_directive('get_adressenregister', get_adressenregister) + config.add_request_method(get_adressenregister, 'adressenregister_gateway') + config.include('crabpy_pyramid.routes.adressenregister') + config.scan('crabpy_pyramid.views.adressenregister') + def main(global_config, **settings): """ diff --git a/crabpy_pyramid/renderers/adressenregister.py b/crabpy_pyramid/renderers/adressenregister.py new file mode 100644 index 0000000..3bc778c --- /dev/null +++ b/crabpy_pyramid/renderers/adressenregister.py @@ -0,0 +1,237 @@ +import crabpy +import pycountry +from crabpy.gateway import adressenregister +from pyramid.renderers import JSON + +json_list_renderer = JSON() +json_item_renderer = JSON() + + +def list_gewesten_adapter(obj, request): + """ + Adapter for rendering a list of + :class:`crabpy.gateway.adressenregister.Gewest` to json. + """ + return {"id": obj.id, "naam": obj.naam} + + +def list_provincie_adapter(obj, request): + """ + Adapter for rendering a list of + :class:`crabpy.gateway.adressenregister.Provincie` to json. + """ + return { + "niscode": obj.niscode, + "naam": obj.naam, + "gewest": { + "id": obj.gewest, + }, + } + + +def list_deelgemeente_adapter(obj, request): + """ + Adapter for rendering a list of + :class:`crabpy.gateway.adressenregister.Deelgemeente` to json. + """ + return { + "id": obj.id, + "naam": obj.naam, + "gemeente_niscode": obj.gemeente_niscode, + } + + +def list_gemeente_adapter(obj, request): + """ + Adapter for rendering a list of + :class:`crabpy.gateway.adressenregister.Gemeenten` to json. + """ + return {"niscode": obj.niscode, "naam": obj.naam, "uri": obj.uri} + + +def list_straten_adapter(obj, request): + """ + Adapter for rendering a list of + :class:`crabpy.gateway.adresregister.Straat` to json. + """ + return {"id": obj.id, "naam": obj.naam, "status": obj.status, "uri": obj.uri} + + +def list_adressen_adapter(obj, request): + """ + Adapter for rendering a list of + :class:`crabpy.gateway.adresregister.Adres` to json. + """ + return { + "id": obj.id, + "uri": obj.uri, + "label": obj.label, + "huisnummer": obj.huisnummer, + "busnummer": obj.busnummer, + "status": obj.status, + } + + +def list_percelen_adapter(obj, request): + """ + Adapter for rendering a list of + :class:`crabpy.gateway.adressenregister.Perceel` to json. + """ + return {"id": obj.id, "uri": obj.uri, "status": obj.status} + + +def list_postinfo_adapter(obj, request): + """ + Adapter for rendering a list of + :class:`crabpy.gateway.adressenregister.Postinfo` to json. + """ + return { + "postcode": obj.id, + "uri": obj.uri, + "status": obj.status, + "namen": obj.namen(), + } + + +def list_landen_adapter(obj, request): + """ + Adapter for rendering a list of landen to json. + """ + return {"id": obj.alpha_2, "naam": _(obj.name)} + + +json_list_renderer.add_adapter(adressenregister.Gewest, list_gewesten_adapter) +json_list_renderer.add_adapter(adressenregister.Provincie, list_provincie_adapter) +json_list_renderer.add_adapter(adressenregister.Deelgemeente, list_deelgemeente_adapter) +json_list_renderer.add_adapter(adressenregister.Gemeente, list_gemeente_adapter) +json_list_renderer.add_adapter(adressenregister.Straat, list_straten_adapter) +json_list_renderer.add_adapter(adressenregister.Adres, list_adressen_adapter) +json_list_renderer.add_adapter(adressenregister.Perceel, list_percelen_adapter) +json_list_renderer.add_adapter(adressenregister.Postinfo, list_postinfo_adapter) +json_list_renderer.add_adapter(pycountry.db.Data, list_landen_adapter) + + +def item_gewest_adapter(obj, request): + """ + Adapter for rendering an object of + :class:`crabpy.gateway.adressenregister.Gewest` to json. + """ + return { + "id": obj.id, + "naam": obj.naam, + "centroid": obj.centroid, + "bounding_box": obj.bounding_box, + } + + +def item_provincie_adapter(obj, request): + """ + Adapter for rendering a object of + :class:`crabpy.gateway.adressenregister.Provincie` to json. + """ + return { + "niscode": obj.niscode, + "naam": obj.naam, + "gewest": { + "id": obj.gewest, + }, + } + + +def item_gemeente_adapter(obj, request): + """ + Adapter for rendering an object of + :class:`crabpy.gateway.adressenregister.Gemeente` to json. + """ + return { + "niscode": obj.niscode, + "uri": obj.uri, + "naam": obj.naam(), + "taal": obj.taal, + "status": obj.status, + } + + +def item_deelgemeente_adapter(obj, request): + """ + Adapter for rendering a object of + :class:`crabpy.gateway.adressenregister.Deelgemeente` to json. + """ + return { + "id": obj.id, + "naam": obj.naam, + "gemeente": {"id": obj.gemeente.id, "naam": obj.gemeente.naam}, + } + + +def item_straat_adapter(obj, request): + """ + Adapter for rendering an object of + :class:`crabpy.gateway.adressenregister.Straat` to json. + """ + return {"id": obj.id, "naam": obj.naam(), "status": obj.status, "uri": obj.uri} + + +def item_adres_adapter(obj, request): + """ + Adapter for rendering a list of + :class:`crabpy.gateway.adresregister.Adres` to json. + """ + return { + "id": obj.id, + "uri": obj.uri, + "label": obj.label, + "huisnummer": obj.huisnummer, + "busnummer": obj.busnummer, + "status": obj.status, + } + + +def item_perceel_adapter(obj, request): + """ + Adapter for rendering an object of + :class:`crabpy.gateway.adressenregister.Perceel` to json. + """ + return { + "id": obj.id, + "uri": obj.uri, + "status": obj.status, + "adressen": [{"id": adres.id} for adres in obj.adressen], + } + + +def item_land_adapter(obj, request): + """ + Adapter for rendering an item of + :class: `pycountry.db.Data` to json. + """ + return { + "id": obj.alpha_2, + "alpha2": obj.alpha_2, + "alpha3": obj.alpha_3, + "naam": obj.name, + } + + +def list_postinfo_adapter(obj, request): + """ + Adapter for rendering a list of + :class:`crabpy.gateway.adressenregister.Postinfo` to json. + """ + return { + "postcode": obj.id, + "uri": obj.uri, + "status": obj.status, + "namen": obj.namen(), + } + + +json_item_renderer.add_adapter(adressenregister.Gewest, item_gewest_adapter) +json_item_renderer.add_adapter(adressenregister.Provincie, item_provincie_adapter) +json_item_renderer.add_adapter(adressenregister.Deelgemeente, item_deelgemeente_adapter) +json_item_renderer.add_adapter(adressenregister.Gemeente, item_gemeente_adapter) +json_item_renderer.add_adapter(adressenregister.Straat, item_straat_adapter) +json_item_renderer.add_adapter(adressenregister.Perceel, item_perceel_adapter) +json_item_renderer.add_adapter(adressenregister.Adres, item_adres_adapter) +json_item_renderer.add_adapter(adressenregister.Postinfo, list_postinfo_adapter) +json_item_renderer.add_adapter(pycountry.db.Data, item_land_adapter) diff --git a/crabpy_pyramid/routes/adressenregister.py b/crabpy_pyramid/routes/adressenregister.py new file mode 100644 index 0000000..d6c341b --- /dev/null +++ b/crabpy_pyramid/routes/adressenregister.py @@ -0,0 +1,118 @@ +import crabpy_pyramid + + +def includeme(config): + crabpy_pyramid.add_route( + config, "adressenregister_list_gewesten", "/adressenregister/gewesten" + ) + crabpy_pyramid.add_route( + config, + "adressenregister_get_gewest_by_id", + "/adressenregister/gewesten/{gewest_id}", + ) + + crabpy_pyramid.add_route( + config, + "adressenregister_list_provincies", + "/adressenregister/gewesten/{gewest_id}/provincies", + ) + crabpy_pyramid.add_route( + config, + "adressenregister_get_provincie", + "/adressenregister/provincies/{provincie_id}", + ) + crabpy_pyramid.add_route( + config, + "adressenregister_list_deelgemeenten", + "/adressenregister/gewesten/{gewest_id}/deelgemeenten", + ) + crabpy_pyramid.add_route( + config, + "adressenregister_list_deelgemeenten_by_gemeente", + "/adressenregister/gemeenten/{niscode}/deelgemeenten", + ) + crabpy_pyramid.add_route( + config, + "adressenregister_get_deelgemeente_by_id", + "/adressenregister/deelgemeenten/{deelgemeente_id}", + ) + + crabpy_pyramid.add_route( + config, + "adressenregister_list_gemeenten_by_provincie", + "/adressenregister/provincies/{provincie_id}/gemeenten", + ) + crabpy_pyramid.add_route( + config, + "list_gemeenten_adressenregister", + "/adressenregister/gewesten/{gewest_id}/gemeenten", + ) + + crabpy_pyramid.add_route( + config, "get_gemeente_adressenregister", "/adressenregister/gemeenten/{niscode}" + ) + + crabpy_pyramid.add_route( + config, + "adressenregister_list_straten", + "/adressenregister/gemeenten/{niscode}/straten", + ) + + crabpy_pyramid.add_route( + config, + "adressenregister_get_straat_by_id", + "/adressenregister/straten/{straat_id}", + ) + + crabpy_pyramid.add_route( + config, + "adressenregister_list_adressen", + "/adressenregister/straten/{straat_id}/adressen", + ) + + crabpy_pyramid.add_route( + config, + "adressenregister_get_adres_by_straat_and_huisnummer", + "/adressenregister/straten/{straat_id}/huisnummers/{huisnummer}", + ) + + crabpy_pyramid.add_route( + config, + "adressenregister_get_adres_by_straat_and_huisnummer_and_busnummer", + "/adressenregister/straten/{straat_id}/huisnummers/{huisnummer}/busnummers/{busnummer}", + ) + + crabpy_pyramid.add_route( + config, + "adressenregister_get_adres_by_id", + "/adressenregister/adressen/{adres_id}", + ) + + crabpy_pyramid.add_route( + config, + "adressenregister_list_percelen_by_adres", + "/adressenregister/adressen/{adres_id}/percelen", + ) + crabpy_pyramid.add_route( + config, + "adressenregister_get_perceel_by_id", + "/adressenregister/percelen/{perceel_id}", + ) + + crabpy_pyramid.add_route( + config, + "adressenregister_list_postinfo_by_gemeente", + "/adressenregister/gemeenten/{gemeente_naam}/postinfo", + ) + crabpy_pyramid.add_route( + config, + "adressenregister_get_postinfo_by_postcode", + "/adressenregister/postinfo/{postcode}", + ) + + crabpy_pyramid.add_route( + config, "adressenregister_list_landen", "/adressenregister/landen" + ) + crabpy_pyramid.add_route( + config, "adressenregister_get_land_by_id", "/adressenregister/landen/{land_id}" + ) diff --git a/crabpy_pyramid/tests/fixtures/adressenregister.py b/crabpy_pyramid/tests/fixtures/adressenregister.py new file mode 100644 index 0000000..295a1e0 --- /dev/null +++ b/crabpy_pyramid/tests/fixtures/adressenregister.py @@ -0,0 +1,332 @@ +gemeenten = { + "@context": "https://docs.basisregisters.vlaanderen.be/context" + "/gemeente/2023-02-28/gemeente_lijst.jsonld", + "gemeenten": [ + { + "@type": "Gemeente", + "identificator": { + "id": "https://data.vlaanderen.be/id/gemeente/11001", + "naamruimte": "https://data.vlaanderen.be/id/gemeente", + "objectId": "11001", + "versieId": "2002-08-13T16:33:18+02:00", + }, + "detail": "https://api.basisregisters.vlaanderen.be/v2/gemeenten/11001", + "gemeentenaam": { + "geografischeNaam": {"spelling": "Aartselaar", "taal": "nl"} + }, + "gemeenteStatus": "inGebruik", + } + ], +} + +gemeente = { + "@context": "https://docs.basisregisters.vlaanderen.be/context/gemeente" + "/2023-02-28/gemeente_detail.jsonld", + "@type": "Gemeente", + "identificator": { + "id": "https://data.vlaanderen.be/id/gemeente/11001", + "naamruimte": "https://data.vlaanderen.be/id/gemeente", + "objectId": "11001", + "versieId": "2002-08-13T16:33:18+02:00", + }, + "officieleTalen": ["nl"], + "faciliteitenTalen": [], + "gemeentenamen": [{"spelling": "Aartselaar", "taal": "nl"}], + "gemeenteStatus": "inGebruik", +} + +straten = { + "@context": "https://docs.basisregisters.vlaanderen.be/context/straatnaam" + "/2023-02-28/straatnaam_lijst.jsonld", + "straatnamen": [ + { + "@type": "Straatnaam", + "identificator": { + "id": "https://data.vlaanderen.be/id/straatnaam/1", + "naamruimte": "https://data.vlaanderen.be/id/straatnaam", + "objectId": "1", + "versieId": "2011-04-29T13:34:14+02:00", + }, + "detail": "https://api.basisregisters.vlaanderen.be/v2/straatnamen/1", + "straatnaam": { + "geografischeNaam": {"spelling": "Acacialaan", "taal": "nl"} + }, + "straatnaamStatus": "inGebruik", + }, + { + "@type": "Straatnaam", + "identificator": { + "id": "https://data.vlaanderen.be/id/straatnaam/2", + "naamruimte": "https://data.vlaanderen.be/id/straatnaam", + "objectId": "2", + "versieId": "2011-04-29T13:34:14+02:00", + }, + "detail": "https://api.basisregisters.vlaanderen.be/v2/straatnamen/2", + "straatnaam": { + "geografischeNaam": {"spelling": "Adriaan Sanderslei", "taal": "nl"} + }, + "straatnaamStatus": "inGebruik", + }, + ], +} + +straat = { + "@context": "https://docs.basisregisters.vlaanderen.be/context/straatnaam/2023-02-28" + "/straatnaam_detail.jsonld", + "@type": "Straatnaam", + "identificator": { + "id": "https://data.vlaanderen.be/id/straatnaam/1", + "naamruimte": "https://data.vlaanderen.be/id/straatnaam", + "objectId": "1", + "versieId": "2011-04-29T13:34:14+02:00", + }, + "gemeente": { + "objectId": "11001", + "detail": "https://api.basisregisters.vlaanderen.be/v2/gemeenten/11001", + "gemeentenaam": {"geografischeNaam": {"spelling": "Aartselaar", "taal": "nl"}}, + }, + "straatnamen": [{"spelling": "Acacialaan", "taal": "nl"}], + "homoniemToevoegingen": [], + "straatnaamStatus": "inGebruik", +} + +adressen = { + "@context": "https://docs.basisregisters.vlaanderen.be/context/adres/2023-02-28" + "/adres_lijst.jsonld", + "adressen": [ + { + "@type": "Adres", + "identificator": { + "id": "https://data.vlaanderen.be/id/adres/307106", + "naamruimte": "https://data.vlaanderen.be/id/adres", + "objectId": "307106", + "versieId": "2011-04-29T14:50:10+02:00", + }, + "detail": "https://api.basisregisters.vlaanderen.be/v2/adressen/307106", + "huisnummer": "4", + "volledigAdres": { + "geografischeNaam": { + "spelling": "Acacialaan 4, 2630 Aartselaar", + "taal": "nl", + } + }, + "adresStatus": "inGebruik", + }, + { + "@type": "Adres", + "identificator": { + "id": "https://data.vlaanderen.be/id/adres/364340", + "naamruimte": "https://data.vlaanderen.be/id/adres", + "objectId": "364340", + "versieId": "2011-04-29T14:50:10+02:00", + }, + "detail": "https://api.basisregisters.vlaanderen.be/v2/adressen/364340", + "huisnummer": "11", + "busnummer": "1", + "volledigAdres": { + "geografischeNaam": { + "spelling": "Acacialaan 11, 2630 Aartselaar", + "taal": "nl", + } + }, + "adresStatus": "inGebruik", + }, + ], +} + +adres = { + "@context": "https://docs.basisregisters.vlaanderen.be/context/adres" + "/2023-02-28/adres_detail.jsonld", + "@type": "Adres", + "identificator": { + "id": "https://data.vlaanderen.be/id/adres/900746", + "naamruimte": "https://data.vlaanderen.be/id/adres", + "objectId": "900746", + "versieId": "2016-12-19T15:23:28+01:00" + }, + "gemeente": { + "objectId": "44021", + "detail": "https://api.basisregisters.vlaanderen.be/v2/gemeenten/44021", + "gemeentenaam": { + "geografischeNaam": { + "spelling": "Gent", + "taal": "nl" + } + } + }, + "postinfo": { + "objectId": "9000", + "detail": "https://api.basisregisters.vlaanderen.be/v2/postinfo/9000" + }, + "straatnaam": { + "objectId": "71608", + "detail": "https://api.basisregisters.vlaanderen.be/v2/straatnamen/71608", + "straatnaam": { + "geografischeNaam": { + "spelling": "Sint-Jansvest", + "taal": "nl" + } + } + }, + "huisnummer": "50", + "volledigAdres": { + "geografischeNaam": { + "spelling": "Sint-Jansvest 50, 9000 Gent", + "taal": "nl" + } + }, + "adresPositie": { + "geometrie": { + "type": "Point", + "gml": "105052.34 " + "193542.11" + }, + "positieGeometrieMethode": "aangeduidDoorBeheerder", + "positieSpecificatie": "perceel" + }, + "adresStatus": "inGebruik", + "officieelToegekend": True +} + +percelen = { + "@context": "https://docs.basisregisters.vlaanderen.be/context/perceel" + "/2023-02-28/perceel_lijst.jsonld", + "percelen": [ + { + "@type": "Perceel", + "identificator": { + "id": "https://data.vlaanderen.be/id/perceel/13013C0384-02H003", + "naamruimte": "https://data.vlaanderen.be/id/perceel", + "objectId": "13013C0384-02H003", + "versieId": "2004-02-13T05:34:17+01:00" + }, + "detail": "https://api.basisregisters.vlaanderen.be/v2/percelen/13013C0384-02H003", + "perceelStatus": "gerealiseerd" + } + ] +} + +perceel = { + "@context": "https://docs.basisregisters.vlaanderen.be/context/perceel" + "/2023-02-28/perceel_detail.jsonld", + "@type": "Perceel", + "identificator": { + "id": "https://data.vlaanderen.be/id/perceel/13013C0384-02H003", + "naamruimte": "https://data.vlaanderen.be/id/perceel", + "objectId": "13013C0384-02H003", + "versieId": "2004-02-13T05:34:17+01:00" + }, + "perceelStatus": "gerealiseerd", + "adressen": [ + { + "objectId": "200001", + "detail": "https://api.basisregisters.vlaanderen.be/v2/adressen/200001" + } + ] +} + +postinfos = { + "@context": "https://docs.basisregisters.vlaanderen.be/context" + "/postinfo/2023-02-28/postinfo_lijst.jsonld", + "postInfoObjecten": [ + { + "@type": "PostInfo", + "identificator": { + "id": "https://data.vlaanderen.be/id/postinfo/1000", + "naamruimte": "https://data.vlaanderen.be/id/postinfo", + "objectId": "1000", + "versieId": "2020-02-10T12:44:14+01:00" + }, + "detail": "https://api.basisregisters.vlaanderen.be/v2/postinfo/1000", + "postInfoStatus": "gerealiseerd", + "postnamen": [ + { + "geografischeNaam": { + "spelling": "BRUSSEL", + "taal": "nl" + } + } + ] + }, + { + "@type": "PostInfo", + "identificator": { + "id": "https://data.vlaanderen.be/id/postinfo/1020", + "naamruimte": "https://data.vlaanderen.be/id/postinfo", + "objectId": "1020", + "versieId": "2020-02-10T12:44:14+01:00" + }, + "detail": "https://api.basisregisters.vlaanderen.be/v2/postinfo/1020", + "postInfoStatus": "gerealiseerd", + "postnamen": [ + { + "geografischeNaam": { + "spelling": "Laken", + "taal": "nl" + } + } + ] + } + ] +} + +postinfo_1000 = { + "@context": "https://docs.basisregisters.vlaanderen.be/context" + "/postinfo/2023-02-28/postinfo_detail.jsonld", + "@type": "PostInfo", + "identificator": { + "id": "https://data.vlaanderen.be/id/postinfo/1000", + "naamruimte": "https://data.vlaanderen.be/id/postinfo", + "objectId": "1000", + "versieId": "2020-02-10T12:44:14+01:00" + }, + "gemeente": { + "objectId": "21004", + "detail": "https://api.basisregisters.vlaanderen.be/v2/gemeenten/21004", + "gemeentenaam": { + "geografischeNaam": { + "spelling": "Brussel", + "taal": "nl" + } + } + }, + "postnamen": [ + { + "geografischeNaam": { + "spelling": "BRUSSEL", + "taal": "nl" + } + } + ], + "postInfoStatus": "gerealiseerd" +} +postinfo_1020 = { + "@context": "https://docs.basisregisters.vlaanderen.be/context/postinfo/2023-02-28/postinfo_detail.jsonld", + "@type": "PostInfo", + "identificator": { + "id": "https://data.vlaanderen.be/id/postinfo/1020", + "naamruimte": "https://data.vlaanderen.be/id/postinfo", + "objectId": "1020", + "versieId": "2020-02-10T12:44:14+01:00" + }, + "gemeente": { + "objectId": "21004", + "detail": "https://api.basisregisters.vlaanderen.be/v2/gemeenten/21004", + "gemeentenaam": { + "geografischeNaam": { + "spelling": "Brussel", + "taal": "nl" + } + } + }, + "postnamen": [ + { + "geografischeNaam": { + "spelling": "Laken", + "taal": "nl" + } + } + ], + "postInfoStatus": "gerealiseerd" +} \ No newline at end of file diff --git a/crabpy_pyramid/tests/test_functional.py b/crabpy_pyramid/tests/test_functional.py index dfa3de8..da94f67 100644 --- a/crabpy_pyramid/tests/test_functional.py +++ b/crabpy_pyramid/tests/test_functional.py @@ -4,15 +4,28 @@ .. versionadded:: 0.1.0 ''' -import unittest import os import shutil +import unittest +from copy import deepcopy +import responses from pyramid import testing from webtest import TestApp import crabpy_pyramid from crabpy_pyramid import main +from crabpy_pyramid.tests.fixtures.adressenregister import adres +from crabpy_pyramid.tests.fixtures.adressenregister import adressen +from crabpy_pyramid.tests.fixtures.adressenregister import gemeente +from crabpy_pyramid.tests.fixtures.adressenregister import gemeenten +from crabpy_pyramid.tests.fixtures.adressenregister import perceel +from crabpy_pyramid.tests.fixtures.adressenregister import percelen +from crabpy_pyramid.tests.fixtures.adressenregister import postinfo_1000 +from crabpy_pyramid.tests.fixtures.adressenregister import postinfo_1020 +from crabpy_pyramid.tests.fixtures.adressenregister import postinfos +from crabpy_pyramid.tests.fixtures.adressenregister import straat +from crabpy_pyramid.tests.fixtures.adressenregister import straten def as_bool(value): @@ -46,13 +59,19 @@ def run_integration_tests(section): 'crabpy.crab.include': True, 'crabpy.crab.cache_config.permanent.backend': 'dogpile.cache.dbm', 'crabpy.crab.cache_config.permanent.expiration_time': 604800, - 'crabpy.crab.cache_config.permanent.arguments.filename': os.path.join(os.path.dirname(__file__), 'dogpile_data', 'crab_permanent.dbm'), + 'crabpy.crab.cache_config.permanent.arguments.filename': os.path.join( + os.path.dirname(__file__), 'dogpile_data', 'crab_permanent.dbm'), 'crabpy.crab.cache_config.long.backend': 'dogpile.cache.dbm', 'crabpy.crab.cache_config.long.expiration_time': 86400, - 'crabpy.crab.cache_config.long.arguments.filename': os.path.join(os.path.dirname(__file__), 'dogpile_data', 'crab_long.dbm'), + 'crabpy.crab.cache_config.long.arguments.filename': os.path.join( + os.path.dirname(__file__), 'dogpile_data', 'crab_long.dbm'), 'crabpy.crab.cache_config.short.backend': 'dogpile.cache.dbm', 'crabpy.crab.cache_config.short.expiration_time': 3600, - 'crabpy.crab.cache_config.short.arguments.filename': os.path.join(os.path.dirname(__file__), 'dogpile_data', 'crab_short.dbm'), + 'crabpy.crab.cache_config.short.arguments.filename': os.path.join( + os.path.dirname(__file__), 'dogpile_data', 'crab_short.dbm'), + 'crabpy.adressenregister.include': True, + 'crabpy.adressenregister.base_url': 'https://api.basisregisters.vlaanderen.be', + 'crabpy.adressenregister.api_key': '33c6eb56-47cc-4723-8a69-3ad0b9cc3acd', } @@ -393,3 +412,523 @@ def test_list_gewesten_not_cached(self): self.assertNotIn('ETag', res.headers) finally: crabpy_pyramid.GENERATE_ETAG_ROUTE_NAMES.add('list_gewesten') + + +class AdressenRegisterFunctionalTests(FunctionalTests): + def test_list_gewesten(self): + res = self.testapp.get("/adressenregister/gewesten") + self.assertEqual("200 OK", res.status) + self.assertCountEqual( + res.json, + [ + {"id": 1, "naam": "Brussels Hoofdstedelijk Gewest"}, + {"id": 2, "naam": "Vlaams Gewest"}, + {"id": 3, "naam": "Waals Gewest"}, + ], + ) + + def test_get_gewest_by_id(self): + res = self.testapp.get("/adressenregister/gewesten/2") + self.assertEqual("200 OK", res.status) + self.assertDictEqual( + res.json, + { + "bounding_box": [22279.17, 153050.23, 258873.3, 244022.31], + "centroid": [138165.09, 189297.53], + "id": 2, + "naam": "Vlaams Gewest", + }, + ) + + def test_get_gewest_by_unexisting_id(self): + res = self.testapp.get("/adressenregister/gewesten/0", status=404) + self.assertEqual("404 Not Found", res.status) + + def test_list_provincies(self): + res = self.testapp.get("/adressenregister/gewesten/2/provincies") + self.assertEqual("200 OK", res.status) + self.assertEqual(5, len(res.json)) + + def test_get_provincie_by_id(self): + res = self.testapp.get("/adressenregister/provincies/10000") + self.assertEqual("200 OK", res.status) + self.assertDictEqual( + {"gewest": {"id": 2}, "naam": "Antwerpen", "niscode": "10000"}, res.json + ) + + def test_get_provincie_by_unexisting_id(self): + res = self.testapp.get("/adressenregister/provincies/00000", status=404) + self.assertEqual("404 Not Found", res.status) + + def test_list_deelgemeenten(self): + res = self.testapp.get("/adressenregister/gewesten/2/deelgemeenten") + self.assertEqual("200 OK", res.status) + + def test_list_deelgemeenten_wrong_gewest(self): + res = self.testapp.get("/adressenregister/gewesten/1/deelgemeenten", status=404) + self.assertEqual("404 Not Found", res.status) + + def test_list_deelgemeenten_by_gemeente(self): + res = self.testapp.get("/adressenregister/gemeenten/11001/deelgemeenten") + self.assertEqual("200 OK", res.status) + + def test_list_deelgemeenten_by_unexisting_gemeente(self): + res = self.testapp.get( + "/adressenregister/gemeenten/99999/deelgemeenten", status=404 + ) + self.assertEqual("404 Not Found", res.status) + res = self.testapp.get( + "/adressenregister/gemeenten/9999/deelgemeenten", status=404 + ) + self.assertEqual("404 Not Found", res.status) + + def test_list_gemeenten_by_provincie(self): + with responses.RequestsMock() as rsps: + rsps.add( + method=rsps.GET, + json=gemeenten, + url="https://api.basisregisters.vlaanderen.be/v2/gemeenten?limit=500", + ) + res = self.testapp.get("/adressenregister/provincies/10000/gemeenten") + self.assertEqual("200 OK", res.status) + self.assertCountEqual( + res.json, + [ + { + "niscode": "11001", + "naam": "Aartselaar", + "uri": "https://data.vlaanderen.be/id/gemeente/11001", + } + ], + ) + + def test_list_gemeenten_by_provincie_404(self): + with responses.RequestsMock() as rsps: + rsps.add( + method=rsps.GET, + status=404, + url="https://api.basisregisters.vlaanderen.be/v2/gemeenten?limit=500", + ) + res = self.testapp.get( + "/adressenregister/provincies/10000/gemeenten", status=404 + ) + self.assertEqual("404 Not Found", res.status) + + def test_list_gemeenten_adressenregister(self): + with responses.RequestsMock() as rsps: + rsps.add( + method=rsps.GET, + json=gemeenten, + url="https://api.basisregisters.vlaanderen.be/v2/gemeenten?limit=500", + ) + res = self.testapp.get("/adressenregister/gewesten/2/gemeenten") + self.assertEqual("200 OK", res.status) + + def test_list_gemeenten_adressenregister_404(self): + with responses.RequestsMock() as rsps: + rsps.add( + method=rsps.GET, + status=404, + url="https://api.basisregisters.vlaanderen.be/v2/gemeenten?limit=500", + ) + res = self.testapp.get("/adressenregister/gewesten/2/gemeenten", status=404) + self.assertEqual("404 Not Found", res.status) + + def test_get_gemeente_adresregister_niscode(self): + with responses.RequestsMock() as rsps: + rsps.add( + method=rsps.GET, + json=gemeente, + url="https://api.basisregisters.vlaanderen.be/v2/gemeenten/11001", + ) + res = self.testapp.get("/adressenregister/gemeenten/11001") + self.assertEqual("200 OK", res.status) + self.assertDictEqual( + { + "naam": "Aartselaar", + "niscode": "11001", + "status": "inGebruik", + "taal": "nl", + "uri": "https://data.vlaanderen.be/id/gemeente/11001", + }, + res.json, + ) + + def test_get_gemeente_crab_unexisting_niscode(self): + with responses.RequestsMock() as rsps: + rsps.add( + method=rsps.GET, + url="https://api.basisregisters.vlaanderen.be/v2/gemeenten/00000", + status=404, + ) + res = self.testapp.get("/adressenregister/gemeenten/00000", status=404) + self.assertEqual("404 Not Found", res.status) + + def test_list_straten(self): + with responses.RequestsMock() as rsps: + rsps.add( + method=rsps.GET, + url="https://api.basisregisters.vlaanderen.be/v2/gemeenten/11001", + json=gemeente, + status=200, + ) + rsps.add( + method=rsps.GET, + url="https://api.basisregisters.vlaanderen.be/v2/straatnamen?nisCode=11001&limit=500", + json=straten, + status=200, + ) + res = self.testapp.get("/adressenregister/gemeenten/11001/straten") + self.assertCountEqual( + [ + { + "id": "1", + "naam": "Acacialaan", + "status": "inGebruik", + "uri": "https://data.vlaanderen.be/id/straatnaam/1", + }, + { + "id": "2", + "naam": "Adriaan Sanderslei", + "status": "inGebruik", + "uri": "https://data.vlaanderen.be/id/straatnaam/2", + }, + ], + res.json, + ) + self.assertEqual("200 OK", res.status) + + def test_list_straten_404(self): + with responses.RequestsMock() as rsps: + rsps.add( + method=rsps.GET, + url="https://api.basisregisters.vlaanderen.be/v2/gemeenten/11001", + status=404, + ) + res = self.testapp.get( + "/adressenregister/gemeenten/11001/straten", status=404 + ) + self.assertEqual("404 Not Found", res.status) + + def test_get_straat_by_id(self): + with responses.RequestsMock() as rsps: + rsps.add( + method=rsps.GET, + url="https://api.basisregisters.vlaanderen.be/v2/straatnamen/1", + json=straat, + status=200, + ) + res = self.testapp.get("/adressenregister/straten/1") + self.assertEqual("200 OK", res.status) + self.assertDictEqual( + { + "id": "1", + "naam": "Acacialaan", + "status": "inGebruik", + "uri": "https://data.vlaanderen.be/id/straatnaam/1", + }, + res.json, + ) + + def test_get_straat_by_unexisting_id(self): + with responses.RequestsMock() as rsps: + rsps.add( + method=rsps.GET, + url="https://api.basisregisters.vlaanderen.be/v2/straatnamen/0", + status=404, + ) + res = self.testapp.get("/adressenregister/straten/0", status=404) + self.assertEqual("404 Not Found", res.status) + + def test_get_adressen_by_straat(self): + with responses.RequestsMock() as rsps: + rsps.add( + method=rsps.GET, + url="https://api.basisregisters.vlaanderen.be/v2/adressen", + json=adressen, + status=200, + ) + res = self.testapp.get("/adressenregister/straten/1/adressen") + self.assertEqual("200 OK", res.status) + self.assertEqual(2, len(res.json)) + + def test_get_adressen_by_straat_404(self): + with responses.RequestsMock() as rsps: + rsps.add( + method=rsps.GET, + url="https://api.basisregisters.vlaanderen.be/v2/adressen", + status=404, + ) + res = self.testapp.get("/adressenregister/straten/1/adressen", status=404) + self.assertEqual("404 Not Found", res.status) + + def test_get_adres_by_straat_and_huisnummer(self): + with responses.RequestsMock() as rsps: + adressen_response = deepcopy(adressen) + adressen_response["adressen"] = [adressen["adressen"][0]] + rsps.add( + method=rsps.GET, + url="https://api.basisregisters.vlaanderen.be/v2/adressen", + json=adressen_response, + status=200, + ) + res = self.testapp.get("/adressenregister/straten/1/huisnummers/4") + self.assertEqual("200 OK", res.status) + self.assertCountEqual( + [ + { + "busnummer": "", + "huisnummer": "4", + "id": "307106", + "label": "Acacialaan 4, 2630 Aartselaar", + "status": "inGebruik", + "uri": "https://data.vlaanderen.be/id/adres/307106", + } + ], + res.json, + ) + + def test_get_adres_by_straat_and_huisnummer_404(self): + with responses.RequestsMock() as rsps: + adressen_response = deepcopy(adressen) + adressen_response["adressen"] = [adressen["adressen"][0]] + rsps.add( + method=rsps.GET, + url="https://api.basisregisters.vlaanderen.be/v2/adressen", + status=404, + ) + res = self.testapp.get( + "/adressenregister/straten/1/huisnummers/4", status=404 + ) + self.assertEqual("404 Not Found", res.status) + + def test_get_adres_by_straat_and_huisnummer_and_busnummer(self): + with responses.RequestsMock() as rsps: + adressen_response = deepcopy(adressen) + adressen_response["adressen"] = [adressen["adressen"][0]] + rsps.add( + method=rsps.GET, + url="https://api.basisregisters.vlaanderen.be/v2/adressen", + json=adressen_response, + status=200, + ) + res = self.testapp.get( + "/adressenregister/straten/1/huisnummers/4/busnummers/1" + ) + self.assertEqual("200 OK", res.status) + self.assertCountEqual( + [ + { + "busnummer": "", + "huisnummer": "4", + "id": "307106", + "label": "Acacialaan 4, 2630 Aartselaar", + "status": "inGebruik", + "uri": "https://data.vlaanderen.be/id/adres/307106", + }, + ], + res.json, + ) + + def test_get_adres_by_straat_and_huisnummer_and_busnummer_404(self): + with responses.RequestsMock() as rsps: + adressen_response = deepcopy(adressen) + adressen_response["adressen"] = [adressen["adressen"][0]] + rsps.add( + method=rsps.GET, + url="https://api.basisregisters.vlaanderen.be/v2/adressen", + json=adressen_response, + status=404, + ) + res = self.testapp.get( + "/adressenregister/straten/1" "/huisnummers/4/busnummers/1", status=404 + ) + self.assertEqual("404 Not Found", res.status) + + def test_get_adres_by_id(self): + with responses.RequestsMock() as rsps: + rsps.add( + method=rsps.GET, + url="https://api.basisregisters.vlaanderen.be/v2/adressen/900746", + json=adres, + status=200, + ) + res = self.testapp.get("/adressenregister/adressen/900746") + self.assertEqual("200 OK", res.status) + self.assertDictEqual( + { + "busnummer": "", + "huisnummer": "50", + "id": "900746", + "label": "Sint-Jansvest 50, 9000 Gent", + "status": "inGebruik", + "uri": "https://data.vlaanderen.be/id/adres/900746", + }, + res.json, + ) + + def test_get_adres_by_id_404(self): + with responses.RequestsMock() as rsps: + rsps.add( + method=rsps.GET, + url="https://api.basisregisters.vlaanderen.be/v2/adressen/900746", + status=404, + ) + res = self.testapp.get("/adressenregister/adressen/900746", status=404) + self.assertEqual("404 Not Found", res.status) + + def test_get_percelen_by_adres_id(self): + with responses.RequestsMock() as rsps: + rsps.add( + method=rsps.GET, + url="https://api.basisregisters.vlaanderen.be/v2/percelen", + json=percelen, + status=200, + ) + res = self.testapp.get("/adressenregister/adressen/200001/percelen") + self.assertEqual("200 OK", res.status) + self.assertListEqual( + [ + { + "id": "13013C0384-02H003", + "status": "gerealiseerd", + "uri": "https://data.vlaanderen.be/id/perceel/13013C0384-02H003", + } + ], + res.json, + ) + + def test_get_percelen_by_adres_id_404(self): + with responses.RequestsMock() as rsps: + rsps.add( + method=rsps.GET, + url="https://api.basisregisters.vlaanderen.be/v2/percelen", + status=404, + ) + res = self.testapp.get( + "/adressenregister/adressen/200001/percelen", status=404 + ) + self.assertEqual("404 Not Found", res.status) + + def test_get_perceel_by_id(self): + with responses.RequestsMock() as rsps: + rsps.add( + method=rsps.GET, + url="https://api.basisregisters.vlaanderen.be/v2/percelen/13013C0384-02H003", + json=perceel, + status=200, + ) + res = self.testapp.get("/adressenregister/percelen/13013C0384-02H003") + self.assertEqual("200 OK", res.status) + self.assertDictEqual( + { + "adressen": [{"id": "200001"}], + "id": "13013C0384-02H003", + "status": "gerealiseerd", + "uri": "https://data.vlaanderen.be/id/perceel/13013C0384-02H003", + }, + res.json, + ) + + def test_get_perceel_by_id_404(self): + with responses.RequestsMock() as rsps: + rsps.add( + method=rsps.GET, + url="https://api.basisregisters.vlaanderen.be/v2/percelen/13013C0384-02H003", + status=404, + ) + res = self.testapp.get( + "/adressenregister/percelen/13013C0384-02H003", status=404 + ) + self.assertEqual("404 Not Found", res.status) + + def test_adresregister_list_postinfo_by_gemeente(self): + with responses.RequestsMock() as rsps: + rsps.add( + method=rsps.GET, + url="https://api.basisregisters.vlaanderen.be/v2/postinfo", + json=postinfos, + status=200, + ) + rsps.add( + method=rsps.GET, + url="https://api.basisregisters.vlaanderen.be/v2/postinfo/1000", + json=postinfo_1000, + status=200, + ) + rsps.add( + method=rsps.GET, + url="https://api.basisregisters.vlaanderen.be/v2/postinfo/1020", + json=postinfo_1020, + status=200, + ) + res = self.testapp.get("/adressenregister/gemeenten/brussel/postinfo") + self.assertEqual("200 OK", res.status) + self.assertListEqual( + [ + { + "namen": ["BRUSSEL"], + "postcode": "1000", + "status": "gerealiseerd", + "uri": "https://data.vlaanderen.be/id/postinfo/1000", + }, + { + "namen": ["Laken"], + "postcode": "1020", + "status": "gerealiseerd", + "uri": "https://data.vlaanderen.be/id/postinfo/1020", + }, + ], + res.json, + ) + + def test_adresregister_list_postinfo_by_gemeente_404(self): + with responses.RequestsMock() as rsps: + rsps.add( + method=rsps.GET, + url="https://api.basisregisters.vlaanderen.be/v2/postinfo", + status=404, + ) + res = self.testapp.get( + "/adressenregister/gemeenten/brussel/postinfo", status=404 + ) + self.assertEqual("404 Not Found", res.status) + + def test_adresregister_get_postinfo_by_postcode(self): + with responses.RequestsMock() as rsps: + rsps.add( + method=rsps.GET, + url="https://api.basisregisters.vlaanderen.be/v2/postinfo/1000", + json=postinfo_1000, + status=200, + ) + + res = self.testapp.get("/adressenregister/postinfo/1000") + self.assertEqual("200 OK", res.status) + self.assertDictEqual( + { + "namen": ["BRUSSEL"], + "postcode": "1000", + "status": "gerealiseerd", + "uri": "https://data.vlaanderen.be/id/postinfo/1000", + }, + res.json, + ) + + def test_adresregister_get_postinfo_by_postcode_404(self): + with responses.RequestsMock() as rsps: + rsps.add( + method=rsps.GET, + url="https://api.basisregisters.vlaanderen.be/v2/postinfo/1000", + status=404, + ) + res = self.testapp.get("/adressenregister/postinfo/1000", status=404) + self.assertEqual("404 Not Found", res.status) + + def test_get_land_by_id(self): + res = self.testapp.get("/adressenregister/landen/BE") + self.assertEqual("200 OK", res.status) + + def test_get_land_by_unexisting_id(self): + res = self.testapp.get("/adressenregister/landen/MORDOR", status=404) + self.assertEqual("404 Not Found", res.status) diff --git a/crabpy_pyramid/views/adressenregister.py b/crabpy_pyramid/views/adressenregister.py new file mode 100644 index 0000000..f39776d --- /dev/null +++ b/crabpy_pyramid/views/adressenregister.py @@ -0,0 +1,349 @@ +import logging + +import pycountry +from crabpy.client import AdressenRegisterClientException +from crabpy.gateway.exception import GatewayResourceNotFoundException +from pyramid.httpexceptions import HTTPNotFound +from pyramid.view import view_config + +from crabpy_pyramid.utils import range_return +from crabpy_pyramid.utils import set_http_caching + +log = logging.getLogger(__name__) + + +@view_config( + route_name="adressenregister_list_gewesten", + renderer="adresreg_listjson", + accept="application/json", +) +def list_gewesten(request): + request = set_http_caching(request, "adressenregister", "long") + Gateway = request.adressenregister_gateway() + gewesten = Gateway.list_gewesten() + return range_return(request, gewesten) + + +@view_config( + route_name="adressenregister_get_gewest_by_id", + renderer="adresreg_itemjson", + accept="application/json", +) +def get_gewest_by_id(request): + request = set_http_caching(request, "adressenregister", "long") + Gateway = request.adressenregister_gateway() + gewest_id = int(request.matchdict.get("gewest_id")) + try: + return Gateway.get_gewest_by_id(gewest_id) + except GatewayResourceNotFoundException: + return HTTPNotFound() + + +@view_config( + route_name="adressenregister_list_provincies", + renderer="adresreg_listjson", + accept="application/json", +) +def list_provincies(request): + request = set_http_caching(request, "adressenregister", "permanent") + Gateway = request.adressenregister_gateway() + gewest_id = int(request.matchdict.get("gewest_id")) + provincies = Gateway.list_provincies(gewest_id) + return range_return(request, provincies) + + +@view_config( + route_name="adressenregister_get_provincie", + renderer="adresreg_itemjson", + accept="application/json", +) +def get_provincie(request): + request = set_http_caching(request, "adressenregister", "permanent") + Gateway = request.adressenregister_gateway() + provincie_id = int(request.matchdict.get("provincie_id")) + provincie = Gateway.get_provincie_by_id(provincie_id) + if provincie: + return provincie + else: + return HTTPNotFound() + + +@view_config( + route_name="adressenregister_list_deelgemeenten", + renderer="adresreg_listjson", + accept="application/json", +) +def list_deelgemeenten(request): + request = set_http_caching(request, "adressenregister", "permanent") + Gateway = request.adressenregister_gateway() + gewest_id = int(request.matchdict.get("gewest_id")) + if gewest_id != 2: + return HTTPNotFound() + deelgemeenten = Gateway.list_deelgemeenten(gewest_id) + return range_return(request, deelgemeenten) + + +@view_config( + route_name="adressenregister_list_deelgemeenten_by_gemeente", + renderer="adresreg_listjson", + accept="application/json", +) +def list_deelgemeenten_by_gemeente(request): + request = set_http_caching(request, "adressenregister", "permanent") + Gateway = request.adressenregister_gateway() + gemeente_id = request.matchdict.get("niscode") + try: + gemeente = Gateway.get_gemeente_by_niscode(gemeente_id) + except (GatewayResourceNotFoundException, AdressenRegisterClientException): + return HTTPNotFound() + deelgemeenten = Gateway.list_deelgemeenten_by_gemeente(gemeente) + return range_return(request, deelgemeenten) + + +@view_config( + route_name="adressenregister_get_deelgemeente_by_id", + renderer="adresreg_itemjson", + accept="application/json", +) +def get_deelgemeente_by_id(request): + request = set_http_caching(request, "adressenregister", "permanent") + Gateway = request.adressenregister_gateway() + deelgemeente_id = request.matchdict.get("deelgemeente_id") + try: + return Gateway.get_deelgemeente_by_id(deelgemeente_id) + except (GatewayResourceNotFoundException, AdressenRegisterClientException): + return HTTPNotFound() + + +@view_config( + route_name="adressenregister_list_gemeenten_by_provincie", + renderer="adresreg_listjson", + accept="application/json", +) +def list_gemeenten_by_provincie(request): + request = set_http_caching(request, "adressenregister", "long") + Gateway = request.adressenregister_gateway() + provincie_id = int(request.matchdict.get("provincie_id")) + try: + gemeenten = Gateway.list_gemeenten_by_provincie(provincie_id) + except (GatewayResourceNotFoundException, AdressenRegisterClientException): + return HTTPNotFound() + return range_return(request, gemeenten) + + +@view_config( + route_name="list_gemeenten_adressenregister", + renderer="adresreg_listjson", + accept="application/json", +) +def list_gemeenten_adressenregister(request): + request = set_http_caching(request, "adressenregister", "long") + Gateway = request.adressenregister_gateway() + gewest_id = request.matchdict.get("gewest_id") + try: + gemeenten = Gateway.list_gemeenten(gewest_id) + except (GatewayResourceNotFoundException, AdressenRegisterClientException): + return HTTPNotFound() + return range_return(request, gemeenten) + + +@view_config( + route_name="get_gemeente_adressenregister", + renderer="adresreg_itemjson", + accept="application/json", +) +def get_gemeente_adressenregister(request): + request = set_http_caching(request, "adressenregister", "long") + Gateway = request.adressenregister_gateway() + gemeente_id = request.matchdict.get("niscode") + try: + return Gateway.get_gemeente_by_niscode(gemeente_id) + except (GatewayResourceNotFoundException, AdressenRegisterClientException): + return HTTPNotFound() + + +@view_config( + route_name="adressenregister_list_straten", + renderer="adresreg_listjson", + accept="application/json", +) +def list_straten(request): + request = set_http_caching(request, "adressenregister", "short") + Gateway = request.adressenregister_gateway() + gemeente_id = request.matchdict.get("niscode") + try: + straten = Gateway.list_straten(gemeente_id) + except (GatewayResourceNotFoundException, AdressenRegisterClientException): + return HTTPNotFound() + return range_return(request, straten) + + +@view_config( + route_name="adressenregister_get_straat_by_id", + renderer="adresreg_itemjson", + accept="application/json", +) +def get_straat_by_id(request): + request = set_http_caching(request, "adressenregister", "short") + Gateway = request.adressenregister_gateway() + straat_id = request.matchdict.get("straat_id") + try: + return Gateway.get_straat_by_id(straat_id) + except (GatewayResourceNotFoundException, AdressenRegisterClientException): + return HTTPNotFound() + + +@view_config( + route_name="adressenregister_list_adressen", + renderer="adresreg_listjson", + accept="application/json", +) +def list_adressen_by_straat(request): + request = set_http_caching(request, "adressenregister", "short") + Gateway = request.adressenregister_gateway() + straat_id = request.matchdict.get("straat_id") + try: + straten = Gateway.list_adressen_with_params(straatnaamObjectId=straat_id) + except (GatewayResourceNotFoundException, AdressenRegisterClientException): + return HTTPNotFound() + return range_return(request, straten) + + +@view_config( + route_name="adressenregister_get_adres_by_straat_and_huisnummer", + renderer="adresreg_listjson", + accept="application/json", +) +def adressenregister_get_adres_by_straat_huisnummer(request): + request = set_http_caching(request, "adressenregister", "short") + Gateway = request.adressenregister_gateway() + straat_id = request.matchdict.get("straat_id") + huisnummer = request.matchdict.get("huisnummer") + try: + adressen = Gateway.list_adressen_with_params( + straatnaamObjectId=straat_id, huisnummer=huisnummer + ) + except (GatewayResourceNotFoundException, AdressenRegisterClientException): + return HTTPNotFound() + return range_return(request, adressen) + + +@view_config( + route_name="adressenregister_get_adres_by_straat_and_huisnummer_and_busnummer", + renderer="adresreg_listjson", + accept="application/json", +) +def adressenregister_get_adres_by_straat_huisnummer_busnummer(request): + request = set_http_caching(request, "adressenregister", "short") + Gateway = request.adressenregister_gateway() + straat_id = request.matchdict.get("straat_id") + huisnummer = request.matchdict.get("huisnummer") + busnummer = request.matchdict.get("busnummer") + try: + adressen = Gateway.list_adressen_with_params( + straatnaamObjectId=straat_id, huisnummer=huisnummer, busnummer=busnummer + ) + except (GatewayResourceNotFoundException, AdressenRegisterClientException): + return HTTPNotFound() + return range_return(request, adressen) + + +@view_config( + route_name="adressenregister_get_adres_by_id", + renderer="adresreg_itemjson", + accept="application/json", +) +def adressenregister_get_adres_by_id(request): + request = set_http_caching(request, "adressenregister", "long") + Gateway = request.adressenregister_gateway() + adres_id = request.matchdict.get("adres_id") + try: + return Gateway.get_adres_by_id(adres_id) + except (GatewayResourceNotFoundException, AdressenRegisterClientException): + return HTTPNotFound() + + +@view_config( + route_name="adressenregister_list_percelen_by_adres", + renderer="adresreg_listjson", + accept="application/json", +) +def adressenregister_list_percelen_by_adres(request): + request = set_http_caching(request, "adressenregister", "short") + Gateway = request.adressenregister_gateway() + adres_id = request.matchdict.get("adres_id") + try: + adressen = Gateway.list_percelen_with_params(adresObjectId=adres_id) + except (GatewayResourceNotFoundException, AdressenRegisterClientException): + return HTTPNotFound() + return range_return(request, adressen) + + +@view_config( + route_name="adressenregister_get_perceel_by_id", + renderer="adresreg_itemjson", + accept="application/json", +) +def adressenregister_get_perceel_by_id(request): + request = set_http_caching(request, "adressenregister", "short") + Gateway = request.adressenregister_gateway() + perceel_id = request.matchdict.get("perceel_id") + try: + return Gateway.get_perceel_by_id(perceel_id=perceel_id) + except (GatewayResourceNotFoundException, AdressenRegisterClientException): + return HTTPNotFound() + + +@view_config( + route_name="adressenregister_list_postinfo_by_gemeente", + renderer="adresreg_listjson", + accept="application/json", +) +def adressenregister_list_postinfo_by_gemeente(request): + request = set_http_caching(request, "adressenregister", "long") + Gateway = request.adressenregister_gateway() + gemeente_naam = request.matchdict.get("gemeente_naam") + try: + adressen = Gateway.get_postinfo_by_gemeentenaam(gemeente_naam) + except (GatewayResourceNotFoundException, AdressenRegisterClientException): + return HTTPNotFound() + return range_return(request, adressen) + + +@view_config( + route_name="adressenregister_get_postinfo_by_postcode", + renderer="adresreg_itemjson", + accept="application/json", +) +def adressenregister_get_postinfo_by_postcode(request): + request = set_http_caching(request, "adressenregister", "long") + Gateway = request.adressenregister_gateway() + postcode = request.matchdict.get("postcode") + try: + return Gateway.get_postinfo_by_id(postcode) + except (GatewayResourceNotFoundException, AdressenRegisterClientException): + return HTTPNotFound() + + +@view_config( + route_name="adressenregister_list_landen", + renderer="adresreg_listjson", + accept="application/json", +) +def list_landen(request): + set_http_caching(request, "adressenregister", "permanent") + return list(pycountry.countries) + + +@view_config( + route_name="adressenregister_get_land_by_id", + renderer="adresreg_itemjson", + accept="application/json", +) +def get_land_by_id(request): + request = set_http_caching(request, "adressenregister", "permanent") + land_id = request.matchdict.get("land_id") + land = pycountry.countries.get(alpha_2=land_id) + if land is None: + return HTTPNotFound() + return land diff --git a/requirements-dev.txt b/requirements-dev.txt index 7d86fd0..b43ef59 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -9,6 +9,7 @@ nose==1.3.7 nose-testconfig==0.10 coverage==5.3 WebTest==2.0.35 +responses==0.23.1 # Linting flake8==3.8.3 diff --git a/setup.py b/setup.py index e23cb14..f8d2757 100644 --- a/setup.py +++ b/setup.py @@ -27,9 +27,6 @@ classifiers=[ 'Development Status :: 5 - Production/Stable', "Programming Language :: Python", - 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3.8', "Framework :: Pyramid", "Topic :: Internet :: WWW/HTTP", diff --git a/tox.ini b/tox.ini index cda598e..a4d768a 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py27, py36, py37, py38, cover +envlist = py38, cover [testenv] commands = @@ -13,7 +13,7 @@ deps = [testenv:cover] basepython = - python3.6 + python3.8 commands = python setup.py develop pip install -r requirements.txt