diff --git a/src/rouse/web/hosts.py b/src/rouse/web/hosts.py index d25d5e7..96b6e95 100644 --- a/src/rouse/web/hosts.py +++ b/src/rouse/web/hosts.py @@ -63,19 +63,26 @@ class HostListController(controllers.BaseController): class HostController(controllers.BaseController): - def GET(self, request, host_id): - host = request.db.query(model.Host).get(host_id) + @staticmethod + def get_host(db, host_id): + try: + int(host_id) + except ValueError: + raise milla.HTTPNotFound + host = db.query(model.Host).get(host_id) if not host: raise milla.HTTPNotFound + return host + + def GET(self, request, host_id): + host = self.get_host(request.db, host_id) response = request.ResponseClass() response.set_payload(None, host) return response def PUT(self, request, host_id): - host = request.db.query(model.Host).get(host_id) - if not host: - raise milla.HTTPNotFound + host = self.get_host(request.db, host_id) if request.content_type == 'application/json': data = request.json @@ -95,9 +102,7 @@ class HostController(controllers.BaseController): return response def DELETE(self, request, host_id): - host = request.db.query(model.Host).get(host_id) - if not host: - raise milla.HTTPNotFound + host = self.get_host(request.db, host_id) request.db.delete(host) request.db.commit()