diff --git a/CHANGES.rst b/CHANGES.rst index a01f0c2..a0b3a31 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,9 @@ +1.5.0 (23-05-2023) +------------------ + +- Nice to haves (#184) +- Support voor py3.10+ (#187) + 1.4.0 (09-05-2023) ------------------ diff --git a/crabpy_pyramid/__init__.py b/crabpy_pyramid/__init__.py index 8ce1a46..3735c65 100644 --- a/crabpy_pyramid/__init__.py +++ b/crabpy_pyramid/__init__.py @@ -1,8 +1,6 @@ -# -*- coding: utf-8 -*- - import logging import os -from collections import Sequence +from collections.abc import Sequence from crabpy.client import AdressenRegisterClient from crabpy.client import crab_factory diff --git a/crabpy_pyramid/routes/adressenregister.py b/crabpy_pyramid/routes/adressenregister.py index b826ef9..e11f472 100644 --- a/crabpy_pyramid/routes/adressenregister.py +++ b/crabpy_pyramid/routes/adressenregister.py @@ -98,11 +98,15 @@ def includeme(config): "adressenregister_get_perceel_by_id", "/adressenregister/percelen/{perceel_id}", ) - + crabpy_pyramid.add_route( + config, + "adressenregister_get_perceel_by_id_parts", + "/adressenregister/percelen/{perceel_id_part1}/{perceel_id_part2}", + ) crabpy_pyramid.add_route( config, "adressenregister_list_postinfo_by_gemeente", - "/adressenregister/gemeenten/{gemeente_naam}/postinfo", + "/adressenregister/gemeenten/{gemeente_naam_niscode}/postinfo", ) crabpy_pyramid.add_route( config, diff --git a/crabpy_pyramid/tests/test_functional.py b/crabpy_pyramid/tests/test_functional.py index a0abc68..23633c1 100644 --- a/crabpy_pyramid/tests/test_functional.py +++ b/crabpy_pyramid/tests/test_functional.py @@ -451,8 +451,8 @@ def test_get_provincie_by_id(self): res = self.testapp.get("/adressenregister/provincies/10000") self.assertEqual("200 OK", res.status) self.assertDictEqual( - {"gewest": {'niscode': '2000'}, "naam": "Antwerpen", "niscode": "10000"}, - res.json + {"gewest": {"niscode": "2000"}, "naam": "Antwerpen", "niscode": "10000"}, + res.json, ) def test_get_provincie_by_unexisting_id(self): @@ -518,7 +518,7 @@ def test_get_gemeente_adresregister_niscode(self): ) def test_get_gemeente_crab_unexisting_niscode(self): - res = self.testapp.get("/adressenregister/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): @@ -688,7 +688,6 @@ def test_error_other_then_404_400(self): res.text ) - def test_get_adres_by_straat_and_huisnummer_and_busnummer_404(self): with responses.RequestsMock() as rsps: adressen_response = deepcopy(adressen) @@ -789,6 +788,26 @@ def test_get_perceel_by_id(self): res.json, ) + def test_get_perceel_by_id_parts(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( @@ -801,7 +820,7 @@ def test_get_perceel_by_id_404(self): ) self.assertEqual("404 Not Found", res.status) - def test_adresregister_list_postinfo_by_gemeente(self): + def test_adresregister_list_postinfo_by_gemeente_naam(self): with responses.RequestsMock() as rsps: rsps.add( method=rsps.GET, @@ -841,6 +860,46 @@ def test_adresregister_list_postinfo_by_gemeente(self): res.json, ) + def test_adresregister_list_postinfo_by_gemeente_niscode(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/21004/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( @@ -879,7 +938,7 @@ def test_adresregister_get_postinfo_by_postcode_404(self): rsps.add( method=rsps.GET, url="https://api.basisregisters.vlaanderen.be/v2/postinfo/1000", - headers={'accept': 'application/json'}, + headers={"accept": "application/json"}, status=404, ) res = self.testapp.get("/adressenregister/postinfo/1000", status=404) diff --git a/crabpy_pyramid/views/adressenregister.py b/crabpy_pyramid/views/adressenregister.py index 45c3ed8..416fe5c 100644 --- a/crabpy_pyramid/views/adressenregister.py +++ b/crabpy_pyramid/views/adressenregister.py @@ -1,4 +1,5 @@ import logging +import re import pycountry from crabpy.client import AdressenRegisterClientException @@ -291,6 +292,21 @@ def adressenregister_get_perceel_by_id(request): return handle_gateway_response(Gateway.get_perceel_by_id, perceel_id=perceel_id) +@view_config( + route_name="adressenregister_get_perceel_by_id_parts", + renderer="adresreg_itemjson", + accept="application/json", +) +def adressenregister_get_perceel_by_id_parts(request): + request = set_http_caching(request, "adressenregister", "short") + Gateway = request.adressenregister_gateway() + perceel_id = ( + f'{request.matchdict.get("perceel_id_part1")}' + f'-{request.matchdict.get("perceel_id_part2")}' + ) + return handle_gateway_response(Gateway.get_perceel_by_id, perceel_id=perceel_id) + + @view_config( route_name="adressenregister_list_postinfo_by_gemeente", renderer="adresreg_listjson", @@ -299,9 +315,15 @@ def adressenregister_get_perceel_by_id(request): 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") + gemeente_param = request.matchdict.get("gemeente_naam_niscode") + niscode_pattern = re.compile(r"^\d{5}$") + if niscode_pattern.match(gemeente_param): + gemeente = handle_gateway_response( + Gateway.get_gemeente_by_niscode, gemeente_param + ) + gemeente_param = gemeente.naam() adressen = handle_gateway_response( - Gateway.get_postinfo_by_gemeentenaam, gemeente_naam + Gateway.get_postinfo_by_gemeentenaam, gemeente_param ) return range_return(request, adressen) diff --git a/requirements.txt b/requirements.txt index 74a430b..3595705 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ # Tested and approved with these versions pyramid==2.0.0 -crabpy==1.2.2 +crabpy==1.2.4 pycountry==18.12.8 ; python_version < '3.0' pycountry==22.3.5 ; python_version >= '3.4' diff --git a/setup.py b/setup.py index f9356f7..28bf258 100644 --- a/setup.py +++ b/setup.py @@ -21,7 +21,7 @@ testing_extras = tests_requires + [] setup(name='crabpy_pyramid', - version='1.4.0', + version='1.5.0', description='Bindings for the CRABpy webservices and the Pyramid framework.', long_description=README + '\n\n' + CHANGES, classifiers=[