Skip to content

Commit

Permalink
Merge pull request #139 from OnroerendErfgoed/feature/138_getpostkant…
Browse files Browse the repository at this point in the history
…onbyhuisnummerid

#138 getpostkantonbyhuisnummerid
  • Loading branch information
claeyswo committed Feb 1, 2021
2 parents 57470ff + 8c6ddaf commit 41157b6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
15 changes: 13 additions & 2 deletions crabpy_pyramid/renderers/crab.py
Expand Up @@ -4,10 +4,10 @@
.. versionadded:: 0.1.0
"""
from crabpy.gateway import crab
import pycountry
import gettext

import pycountry
from crabpy.gateway import crab
from pyramid.renderers import JSON

json_list_renderer = JSON()
Expand Down Expand Up @@ -514,6 +514,16 @@ def item_land_adapter(obj, request):
}


def item_postkanton_adapter(obj, request):
"""
Adapter for rendering an item of
:class: `pycountry.db.Data` to json.
"""
return {
'postcode': obj.id,
}


json_item_renderer.add_adapter(crab.Gewest, item_gewest_adapter)
json_item_renderer.add_adapter(crab.Provincie, item_provincie_adapter)
json_item_renderer.add_adapter(crab.Gemeente, item_gemeente_adapter)
Expand All @@ -526,3 +536,4 @@ def item_land_adapter(obj, request):
json_item_renderer.add_adapter(crab.Subadres, item_subadres_adapter)
json_item_renderer.add_adapter(crab.Adrespositie, item_adrespositie_adapter)
json_item_renderer.add_adapter(pycountry.db.Data, item_land_adapter)
json_item_renderer.add_adapter(crab.Postkanton, item_postkanton_adapter)
3 changes: 3 additions & 0 deletions crabpy_pyramid/routes/crab.py
Expand Up @@ -97,3 +97,6 @@ def includeme(config):
crabpy_pyramid.add_route(config,
'get_land_by_id',
'/crab/landen/{land_id}')
crabpy_pyramid.add_route(config,
'get_postkanton_by_huisnummer',
'/crab/huisnummers/{huisnummer_id}/postkanton')
8 changes: 8 additions & 0 deletions crabpy_pyramid/tests/test_functional.py
Expand Up @@ -348,6 +348,14 @@ def test_get_land_by_unexisting_id(self):
res = self.testapp.get('/crab/landen/MORDOR', status=404)
self.assertEqual('404 Not Found', res.status)

def test_get_postkanton_by_huisnummer(self):
res = self.testapp.get('/crab/huisnummers/5/postkanton')
self.assertEqual('200 OK', res.status)

def test_get_postkanton_by_huisnummer_unexisting(self):
res = self.testapp.get('/crab/huisnummers/99999999/postkanton', status=404)
self.assertEqual('404 Not Found', res.status)

@unittest.skipUnless(
run_crab_integration_tests(),
'No CRAB Integration tests required'
Expand Down
24 changes: 19 additions & 5 deletions crabpy_pyramid/views/crab.py
Expand Up @@ -4,15 +4,15 @@
.. versionadded:: 0.1.0
"""
from pyramid.view import view_config
from crabpy_pyramid.utils import range_return, set_http_caching
import pycountry
import logging

import pycountry
from crabpy.gateway.exception import GatewayResourceNotFoundException

from pyramid.httpexceptions import HTTPNotFound
from pyramid.view import view_config

import logging
from crabpy_pyramid.utils import range_return
from crabpy_pyramid.utils import set_http_caching

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -410,3 +410,17 @@ def get_land_by_id(request):
if land is None:
return HTTPNotFound()
return land


@view_config(
route_name='get_postkanton_by_huisnummer',
renderer='crab_itemjson', accept='application/json'
)
def get_postkanton_by_huisnummer(request):
request = set_http_caching(request, 'crab', 'short')
Gateway = request.crab_gateway()
huisnummer_id = request.matchdict.get('huisnummer_id')
try:
return Gateway.get_postkanton_by_huisnummer(huisnummer_id)
except GatewayResourceNotFoundException:
return HTTPNotFound()

0 comments on commit 41157b6

Please sign in to comment.