Skip to content

Commit

Permalink
Merge pull request #213 from OnroerendErfgoed/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
claeyswo authored May 4, 2023
2 parents 1c4fe6d + 71f04bb commit 57f2ae1
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 53 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
1.2.0 (04-05-2023)
------------------

- uniformiseer alles naar niscodes en check dat alle niscodes strings zijn (#208)
- errors checken en fine tunen (#209)

1.1.0 (03-05-2023)
------------------

Expand Down
20 changes: 10 additions & 10 deletions crabpy/data/provincies.json
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
[
{
"gewest": 2,
"gewest": "2000",
"niscode": "10000",
"naam": "Antwerpen"
},
{
"gewest": 2,
"gewest": "2000",
"niscode": "20001",
"naam": "Vlaams-Brabant"
},
{
"gewest": 2,
"gewest": "2000",
"niscode": "30000",
"naam": "West-Vlaanderen"
},
{
"gewest": 2,
"gewest": "2000",
"niscode": "40000",
"naam": "Oost-Vlaanderen"
},
{
"gewest": 2,
"gewest": "2000",
"niscode": "70000",
"naam": "Limburg"
},
{
"gewest": 3,
"gewest": "3000",
"niscode": "20002",
"naam": "Waals-Brabant"
},
{
"gewest": 3,
"gewest": "3000",
"niscode": "50000",
"naam": "Henegouwen"
},
{
"gewest": 3,
"gewest": "3000",
"niscode": "60000",
"naam": "Luik"
},
{
"gewest": 3,
"gewest": "3000",
"niscode": "80000",
"naam": "Luxemburg"
},
{
"gewest": 3,
"gewest": "3000",
"niscode": "90000",
"naam": "Namen"
}
Expand Down
64 changes: 33 additions & 31 deletions crabpy/gateway/adressenregister.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from dogpile.util import compat

from crabpy.client import AdressenRegisterClient
from crabpy.gateway.exception import GatewayResourceNotFoundException

LOG = logging.getLogger(__name__)
AUTO = object()
Expand Down Expand Up @@ -141,7 +140,7 @@ def provincie_from_json_data(data):
return Provincie(
niscode=data["niscode"],
naam=data["naam"],
gewest=data["gewest"],
gewest_niscode=data["gewest"],
gateway=self,
)

Expand All @@ -160,27 +159,30 @@ def provincie_from_json_data(data):
def list_gewesten(self):
return self.gewesten

def get_gewest_by_id(self, id_):
def get_gewest_by_niscode(self, niscode):
"""
Get a `gewest` by id.
Get a `gewest` by niscode.
:param integer id_: The id of a `gewest`.
:param string niscode: The niscode of a `gewest`.
:rtype: A :class:`Gewest`.
"""
for gewest in self.gewesten:
if gewest.id == id_:
if gewest.niscode == niscode:
return gewest
raise GatewayResourceNotFoundException()
return None

def list_provincies(self, gewest=2):
def list_provincies(self, gewest_niscode="2000"):
"""
List all `provincies` in a `gewest`.
:param gewest: The :class:`Gewest` for which the \
`provincies` are wanted.
:param gewest_niscode: The niscode for which the `provincies` are wanted.
:rtype: A :class:`list` of :class:`Provincie`.
"""
return [provincie for provincie in self.provincies if provincie.gewest == gewest]
return [
provincie
for provincie in self.provincies
if provincie.gewest_niscode == gewest_niscode
]

def get_provincie_by_id(self, niscode):
"""
Expand All @@ -189,7 +191,7 @@ def get_provincie_by_id(self, niscode):
:param str niscode: The niscode of the provincie.
:rtype: :class:`Provincie`
"""
niscode = str(niscode)
niscode = niscode
for provincie in self.provincies:
if provincie.niscode == niscode:
return provincie
Expand All @@ -205,14 +207,14 @@ def list_gemeenten_by_provincie(self, provincie):
"""
if not isinstance(provincie, Provincie):
provincie = self.get_provincie_by_id(provincie)
provincie_niscode = str(provincie.niscode)
provincie_niscode = provincie.niscode
return [
gemeente
for gemeente in self.gemeenten
if gemeente.provincie_niscode == provincie_niscode
]

def list_gemeenten(self, gewest=2):
def list_gemeenten(self, gewest_niscode="2000"):
"""
List all `gemeenten` in a `gewest`.
Expand All @@ -221,16 +223,16 @@ def list_gemeenten(self, gewest=2):
:rtype: A :class:`list` of :class:`Gemeente`.
"""
# Brussel is a special case, because it has no provinces
if str(gewest) == "1":
if gewest_niscode == "4000":
return [
gemeente
for gemeente in self.gemeenten
if gemeente.niscode.startswith("21")
]
provincie_niscodes = [
provincie.niscode
for provincie in self.list_provincies(gewest=gewest)
if provincie.gewest == gewest
for provincie in self.list_provincies(gewest_niscode=gewest_niscode)
if provincie.gewest_niscode == gewest_niscode
]
return [
gemeente
Expand All @@ -242,11 +244,11 @@ def get_gemeente_by_niscode(self, niscode):
"""
Retrieve a `gemeente` by the NIScode.
:param integer niscode: The NIScode of the gemeente.
:param string niscode: The NIScode of the gemeente.
:rtype: :class:`Gemeente`
"""
return next(
(gemeente for gemeente in self.gemeenten if gemeente.niscode == str(niscode)),
(gemeente for gemeente in self.gemeenten if gemeente.niscode == niscode),
None,
)

Expand All @@ -268,12 +270,12 @@ def get_postinfo_by_id(self, postcode):
"""
Retrieve a `postinfo` by crab id.
:param integer gemeente_id: The crab id of the municipality.
:param integer postcode: The postcode the municipality.
:rtype: :class:`Postinfo`
"""
return Postinfo.from_get_response(self.client.get_postinfo(postcode), self)

def list_deelgemeenten(self, gewest=2):
def list_deelgemeenten(self, gewest_niscode="2000"):
"""
List all `deelgemeenten` in a `gewest`.
Expand All @@ -283,8 +285,8 @@ def list_deelgemeenten(self, gewest=2):
"""
first_niscode_digits = [
provincie.niscode[0]
for provincie in self.list_provincies(gewest=gewest)
if provincie.gewest == gewest or gewest is None
for provincie in self.list_provincies(gewest_niscode=gewest_niscode)
if provincie.gewest_niscode == gewest_niscode or gewest_niscode is None
]
return [
deelgemeente
Expand Down Expand Up @@ -345,7 +347,7 @@ def get_straat_by_id(self, straat_id):
"""
Retrieve a `straat` by the Id.
:param integer straat_id: The id of the `straat`.
:param string straat_id: The id of the `straat`.
:rtype: :class:`Straat`
"""
return Straat.from_get_response(self.client.get_straatnaam(straat_id), self)
Expand All @@ -371,7 +373,7 @@ def get_adres_by_id(self, adres_id):
"""
Retrieve a `adres` by the Id.
:param integer adres_id: The id of the `adres`.
:param string adres_id: The id of the `adres`.
:rtype: :class:`Adres`
"""
return Adres.from_get_response(self.client.get_adres(adres_id), self)
Expand Down Expand Up @@ -400,7 +402,7 @@ def list_adressen_with_params(
:param busnummer: string
:param niscode: string
:param status: string
:param straatnaamObjectId: integer
:param straatnaamObjectId: string
:return: :rtype: Adres
"""
return [
Expand Down Expand Up @@ -497,11 +499,11 @@ def __init__(self, id_, niscode, naam, centroid, bounding_box, gateway=None):

@LazyProperty
def provincies(self):
return self.gateway.list_provincies(gewest=self.id)
return self.gateway.list_provincies(gewest_niscode=self.niscode)

@LazyProperty
def gemeenten(self):
return self.gateway.list_gemeenten(self.id)
return self.gateway.list_gemeenten(self.niscode)

def __str__(self):
if self.naam is not None:
Expand All @@ -520,11 +522,11 @@ class Provincie(GatewayObject):
.. versionadded:: 0.4.0
"""

def __init__(self, niscode, naam, gewest, gateway):
def __init__(self, niscode, naam, gewest_niscode, gateway):
super().__init__(gateway)
self.niscode = niscode
self.naam = naam
self.gewest = gewest
self.gewest_niscode = gewest_niscode

@LazyProperty
def gemeenten(self):
Expand Down Expand Up @@ -578,7 +580,7 @@ def provincie(self):

@LazyProperty
def gewest(self):
return self.gateway.get_gewest_by_id(self.provincie.gewest)
return self.gateway.get_gewest_by_niscode(self.provincie.gewest_niscode)

def __str__(self):
return f"{self.naam} ({self.niscode})"
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

setup(
name="crabpy",
version="1.1.0",
version="1.2.0",
description="Interact with geographical webservices by Informatie Vlaanderen.",
long_description=open("README.rst").read() + "\n\n" + open("CHANGES.rst").read(),
author="Onroerend Erfgoed",
Expand Down
16 changes: 5 additions & 11 deletions tests/gateway/test_adressenregister.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from crabpy.gateway.adressenregister import Postinfo
from crabpy.gateway.adressenregister import Provincie
from crabpy.gateway.adressenregister import Straat
from crabpy.gateway.exception import GatewayResourceNotFoundException


@pytest.fixture()
Expand Down Expand Up @@ -334,7 +333,7 @@ def test_list_gewesten(self, gateway):
]

def test_get_gewest_by_id(self, gateway):
res = gateway.get_gewest_by_id(2)
res = gateway.get_gewest_by_niscode("2000")
assert isinstance(res, Gewest)
assert res.id == 2
assert res.naam == "Vlaams Gewest"
Expand All @@ -360,8 +359,7 @@ def test_list_gemeenten_by_provincie(self, gateway, client):
assert res[0].niscode == "11001"

def test_get_gewest_by_unexisting_id(self, gateway):
with pytest.raises(GatewayResourceNotFoundException):
gateway.get_gewest_by_id(5)
assert gateway.get_gewest_by_niscode("5000") is None

def test_list_gemeenten_default(self, gateway, client):
res = gateway.list_gemeenten()
Expand All @@ -375,10 +373,6 @@ def test_list_gemeenten_default(self, gateway, client):
assert "71002" in niscodes

def test_get_gemeente_by_niscode(self, gateway, client):
res = gateway.get_gemeente_by_niscode(57096)
assert res.niscode == "57096"
assert res.naam() == "Moeskroen"
assert res.provincie.niscode == "50000"
res = gateway.get_gemeente_by_niscode("57096")
assert res.niscode == "57096"
assert res.naam() == "Moeskroen"
Expand Down Expand Up @@ -508,7 +502,7 @@ def test_get_gebouw_by_id(self, gateway, client):

class TestGewest:
def test_gemeenten(self, gateway, client):
vlaanderen = gateway.get_gewest_by_id(2)
vlaanderen = gateway.get_gewest_by_niscode("2000")
res = vlaanderen.gemeenten
assert len(res) == 300
niscodes = [gemeente.niscode for gemeente in res]
Expand All @@ -520,7 +514,7 @@ def test_gemeenten(self, gateway, client):
assert "71002" in niscodes

def test_provincies(self, gateway):
vlaanderen = gateway.get_gewest_by_id(2)
vlaanderen = gateway.get_gewest_by_niscode("2000")
provincies = vlaanderen.provincies
assert len(provincies) == 5

Expand Down Expand Up @@ -572,7 +566,7 @@ def test_gemeente_brussel(self, gateway, client):
assert gemeente.provincie_niscode is None

def test_gemeenten_brussels_gewest(self, gateway, client):
gemeenten = gateway.list_gemeenten("1")
gemeenten = gateway.list_gemeenten("4000")
assert 19 == len(gemeenten)
assert "21001" == gemeenten[0].niscode
assert "Anderlecht" == gemeenten[0].naam()
Expand Down

0 comments on commit 57f2ae1

Please sign in to comment.