diff --git a/crabpy_pyramid/renderers/crab.py b/crabpy_pyramid/renderers/crab.py index 3f75fb6..5fd8860 100644 --- a/crabpy_pyramid/renderers/crab.py +++ b/crabpy_pyramid/renderers/crab.py @@ -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() @@ -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) @@ -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) diff --git a/crabpy_pyramid/routes/crab.py b/crabpy_pyramid/routes/crab.py index ca793f9..22e2e60 100644 --- a/crabpy_pyramid/routes/crab.py +++ b/crabpy_pyramid/routes/crab.py @@ -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') diff --git a/crabpy_pyramid/tests/test_functional.py b/crabpy_pyramid/tests/test_functional.py index 8a745bb..cfeb62e 100644 --- a/crabpy_pyramid/tests/test_functional.py +++ b/crabpy_pyramid/tests/test_functional.py @@ -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' diff --git a/crabpy_pyramid/views/crab.py b/crabpy_pyramid/views/crab.py index 0117903..fc9130c 100644 --- a/crabpy_pyramid/views/crab.py +++ b/crabpy_pyramid/views/crab.py @@ -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__) @@ -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()