From 9d971cc14515337fd638c4d79a3f9f6224305551 Mon Sep 17 00:00:00 2001 From: Tim Van Campenhout Date: Mon, 3 Apr 2023 15:41:51 +0200 Subject: [PATCH 1/5] #185 add additional func voor adressenregister implementatie --- crabpy/gateway/adressenregister.py | 29 ++++++++++++++++++++++++++ tests/gateway/test_adressenregister.py | 15 +++++++++++++ 2 files changed, 44 insertions(+) diff --git a/crabpy/gateway/adressenregister.py b/crabpy/gateway/adressenregister.py index 4097b33..d96bcf9 100644 --- a/crabpy/gateway/adressenregister.py +++ b/crabpy/gateway/adressenregister.py @@ -354,6 +354,35 @@ def list_adressen_by_straat(self, straat): for adres in self.client.get_adressen(straatnaamObjectId=straat.id) ] + + @SHORT_CACHE.cache_on_arguments() + def list_adressen_with_params( + self, + gemeentenaam=None, + postcode=None, + straatnaam=None, + homoniem_toevoeging=None, + huisnummer=None, + busnummer=None, + niscode=None, + status=None, + straatnaamObjectId=None, + ): + return [ + Adres.from_list_response(adres, self) + for adres in self.client.get_adressen( + gemeentenaam=gemeentenaam, + postcode=postcode, + straatnaam=straatnaam, + homoniem_toevoeging=homoniem_toevoeging, + huisnummer=huisnummer, + busnummer=busnummer, + niscode=niscode, + status=status, + straatnaamObjectId=straatnaamObjectId, + ) + ] + @SHORT_CACHE.cache_on_arguments() def list_adressen_by_perceel(self, perceel): """ diff --git a/tests/gateway/test_adressenregister.py b/tests/gateway/test_adressenregister.py index 126de94..ca37186 100644 --- a/tests/gateway/test_adressenregister.py +++ b/tests/gateway/test_adressenregister.py @@ -493,6 +493,21 @@ def test_list_adressen_by_straat(self, gateway, client): assert res[0].label == "Goorbaan 59, 2230 Herselt" assert res[0].status == "inGebruik" + def test_list_adressen_by_straat_and_huisnummer(self, gateway, client): + client.get_adressen.return_value = [ + create_client_list_adressen_item(), + ] + res = gateway.list_adressen_with_params( + straatnaamObjectId=1, + huisnummer="59", + ) + assert len(res) == 1 + assert res[0].id == "200001" + assert res[0].huisnummer == "59" + assert res[0].label == "Goorbaan 59, 2230 Herselt" + assert res[0].status == "inGebruik" + + def test_list_adressen_by_perceel(self, gateway, client): client.get_adres.return_value = create_client_get_adres_item() client.get_perceel.return_value = create_client_get_perceel_item() From 094fb8a26498f5806c352f3386c0b8b0033ae9d9 Mon Sep 17 00:00:00 2001 From: Tim Van Campenhout Date: Mon, 3 Apr 2023 16:06:01 +0200 Subject: [PATCH 2/5] #185 add additional func voor adressenregister implementatie --- crabpy/gateway/adressenregister.py | 26 +++++++++++++++++++++++++- tests/gateway/test_adressenregister.py | 5 +++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/crabpy/gateway/adressenregister.py b/crabpy/gateway/adressenregister.py index d96bcf9..53aed46 100644 --- a/crabpy/gateway/adressenregister.py +++ b/crabpy/gateway/adressenregister.py @@ -354,6 +354,15 @@ def list_adressen_by_straat(self, straat): for adres in self.client.get_adressen(straatnaamObjectId=straat.id) ] + @LONG_CACHE.cache_on_arguments() + def get_adres_by_id(self, adres_id): + """ + Retrieve a `adres` by the Id. + + :param integer adres_id: The id of the `adres`. + :rtype: :class:`Adres` + """ + return Adres.from_get_response(self.client.get_adres(adres_id), self) @SHORT_CACHE.cache_on_arguments() def list_adressen_with_params( @@ -368,6 +377,20 @@ def list_adressen_with_params( status=None, straatnaamObjectId=None, ): + """ + List all `adressen` with the given parameters. + + :param gemeentenaam: string + :param postcode:integer + :param straatnaam: string + :param homoniem_toevoeging: string + :param huisnummer: string + :param busnummer: string + :param niscode: string + :param status: string + :param straatnaamObjectId: integer + :return: :rtype: Adres + """ return [ Adres.from_list_response(adres, self) for adres in self.client.get_adressen( @@ -708,7 +731,8 @@ def from_list_response(cls, adres, gateway): def from_get_response(cls, adres, gateway): res = Adres( id_=adres["identificator"]["objectId"], - gateway=gateway) + gateway=gateway + ) res._source_json = adres return res diff --git a/tests/gateway/test_adressenregister.py b/tests/gateway/test_adressenregister.py index ca37186..a7c89b0 100644 --- a/tests/gateway/test_adressenregister.py +++ b/tests/gateway/test_adressenregister.py @@ -521,6 +521,11 @@ def test_list_adressen_by_perceel(self, gateway, client): assert len(res) == 1 assert res[0].id == "763445" + def test_get_adres_by_id(self, gateway, client): + client.get_adres.return_value = create_client_get_adres_item() + res = gateway.get_adres_by_id(763445) + assert res.id == '763445' + def test_get_perceel_by_id(self, gateway, client): client.get_perceel.return_value = create_client_get_perceel_item() res = gateway.get_perceel_by_id("1") From d3a36e6312efd09b0872d17ee8c4de4d660310b7 Mon Sep 17 00:00:00 2001 From: Tim Van Campenhout Date: Mon, 3 Apr 2023 16:11:24 +0200 Subject: [PATCH 3/5] #185 add status voor adres --- crabpy/gateway/adressenregister.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crabpy/gateway/adressenregister.py b/crabpy/gateway/adressenregister.py index 53aed46..13f7ad7 100644 --- a/crabpy/gateway/adressenregister.py +++ b/crabpy/gateway/adressenregister.py @@ -715,6 +715,8 @@ def __init__( self.postinfo = postinfo if busnummer is not AUTO: self.busnummer = busnummer + if status is not AUTO: + self.status = status @classmethod def from_list_response(cls, adres, gateway): @@ -744,6 +746,10 @@ def label(self): def huisnummer(self): return self._source_json["huisnummer"] + @LazyProperty + def status(self): + return self._source_json["adresStatus"] + @LazyProperty def straat(self): return Straat( From 2eb5077a86ed31bf38fb24096c66cdeeaed9a03a Mon Sep 17 00:00:00 2001 From: Tim Van Campenhout Date: Mon, 3 Apr 2023 16:36:05 +0200 Subject: [PATCH 4/5] #185 add percelen with param func --- crabpy/client.py | 6 +++--- crabpy/gateway/adressenregister.py | 20 ++++++++++++++++++++ tests/gateway/test_adressenregister.py | 24 ++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/crabpy/client.py b/crabpy/client.py index 2ed6f90..d152eab 100644 --- a/crabpy/client.py +++ b/crabpy/client.py @@ -200,12 +200,12 @@ def get_adressen( def get_perceel(self, perceel_id): return self._get(f"/v2/percelen/{perceel_id}") - def get_percelen(self, status=None, adresOjbectId=None): + def get_percelen(self, status=None, adresObjectId=None): params = {} if status is not None: params["status"] = status - if adresOjbectId is not None: - params["adresOjbectId"] = adresOjbectId + if adresObjectId is not None: + params["adresOjbectId"] = adresObjectId return self._get_list(f"/v2/percelen", "percelen", params=params) def get_gebouw(self, gebouw_id): diff --git a/crabpy/gateway/adressenregister.py b/crabpy/gateway/adressenregister.py index 13f7ad7..67d521c 100644 --- a/crabpy/gateway/adressenregister.py +++ b/crabpy/gateway/adressenregister.py @@ -406,6 +406,26 @@ def list_adressen_with_params( ) ] + @SHORT_CACHE.cache_on_arguments() + def list_percelen_with_params(self, status=None, adresObjectId=None): + """ + List all `percelen` with the given parameters. + + :param status: str + :param adresOjbectId: str + :return: :rtype: + """ + + return [ + Perceel.from_list_response(perceel, self) + for perceel + in self.client.get_percelen( + status=status, + adresObjectId=adresObjectId + ) + ] + + @SHORT_CACHE.cache_on_arguments() def list_adressen_by_perceel(self, perceel): """ diff --git a/tests/gateway/test_adressenregister.py b/tests/gateway/test_adressenregister.py index a7c89b0..067041f 100644 --- a/tests/gateway/test_adressenregister.py +++ b/tests/gateway/test_adressenregister.py @@ -161,6 +161,20 @@ def create_client_get_adres_item(): } +def create_client_get_perceel_list_item(): + return { + "@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" + } + + def create_client_get_perceel_item(): return { "identificator": { @@ -507,6 +521,16 @@ def test_list_adressen_by_straat_and_huisnummer(self, gateway, client): assert res[0].label == "Goorbaan 59, 2230 Herselt" assert res[0].status == "inGebruik" + def test_list_percelen_by_adres(self, gateway, client): + client.get_percelen.return_value = [ + create_client_get_perceel_list_item(), + ] + res = gateway.list_percelen_with_params( + adresObjectId=200001, + ) + assert len(res) == 1 + assert res[0].id == "13013C0384-02H003" + assert res[0].status == "gerealiseerd" def test_list_adressen_by_perceel(self, gateway, client): client.get_adres.return_value = create_client_get_adres_item() From c8a7aedd048882986b9e262c19e07743d5cd5e08 Mon Sep 17 00:00:00 2001 From: Tim Van Campenhout Date: Mon, 3 Apr 2023 17:17:07 +0200 Subject: [PATCH 5/5] #185 add postcode get by id --- crabpy/gateway/adressenregister.py | 27 +++++++++++++++++--------- tests/gateway/test_adressenregister.py | 7 +++++++ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/crabpy/gateway/adressenregister.py b/crabpy/gateway/adressenregister.py index 67d521c..900ca2c 100644 --- a/crabpy/gateway/adressenregister.py +++ b/crabpy/gateway/adressenregister.py @@ -16,7 +16,6 @@ LOG = logging.getLogger(__name__) AUTO = object() - LONG_CACHE = make_region() SHORT_CACHE = make_region() @@ -258,6 +257,17 @@ def get_postinfo_by_gemeentenaam(self, gemeente_naam): for postinfo in self.client.get_postinfos(gemeentenaam=gemeente_naam) ] + @LONG_CACHE.cache_on_arguments() + def get_postinfo_by_id(self, postcode): + """ + Retrieve a `postinfo` by crab id. + + :param integer gemeente_id: The crab id of the municipality. + :rtype: :class:`Postinfo` + """ + return Postinfo.from_get_response(self.client.get_postinfo(postcode), self) + + @LONG_CACHE.cache_on_arguments() def list_deelgemeenten(self, gewest=2): """ @@ -425,7 +435,6 @@ def list_percelen_with_params(self, status=None, adresObjectId=None): ) ] - @SHORT_CACHE.cache_on_arguments() def list_adressen_by_perceel(self, perceel): """ @@ -560,12 +569,12 @@ def taal(self): def naam(self, taal="nl"): for _taal in [taal, self.taal]: naam = next( - ( - gemeentenaam["spelling"] - for gemeentenaam in self._source_json["gemeentenamen"] - if gemeentenaam["taal"] == _taal - ), None - ) + ( + gemeentenaam["spelling"] + for gemeentenaam in self._source_json["gemeentenamen"] + if gemeentenaam["taal"] == _taal + ), None + ) if naam: return naam @@ -978,4 +987,4 @@ def __str__(self): return f"Postinfo {self.id}" def __repr__(self): - return f"Postinfo(id={self.id})" \ No newline at end of file + return f"Postinfo(id={self.id})" diff --git a/tests/gateway/test_adressenregister.py b/tests/gateway/test_adressenregister.py index 067041f..51ede31 100644 --- a/tests/gateway/test_adressenregister.py +++ b/tests/gateway/test_adressenregister.py @@ -770,3 +770,10 @@ def test_get_postinfo(self, gateway, client): postinfo_fr = Postinfo("7850", gateway) assert postinfo_fr.namen("fr") == ["Enghien"] assert postinfo_fr.id == "7850" + + def test_get_postinfo_by_postcode(self, gateway, client): + client.get_postinfo.return_value = create_client_get_post_info() + postinfo = gateway.get_postinfo_by_id("7850") + assert postinfo.namen("nl") == ["EDINGEN", "Lettelingen", "Mark"] + assert postinfo.id == "7850" + assert postinfo.status == "gerealiseerd"