web: hosts: Add Host ID validation
parent
d51cb1d8d3
commit
26532cf76f
|
@ -63,19 +63,26 @@ class HostListController(controllers.BaseController):
|
||||||
|
|
||||||
class HostController(controllers.BaseController):
|
class HostController(controllers.BaseController):
|
||||||
|
|
||||||
def GET(self, request, host_id):
|
@staticmethod
|
||||||
host = request.db.query(model.Host).get(host_id)
|
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:
|
if not host:
|
||||||
raise milla.HTTPNotFound
|
raise milla.HTTPNotFound
|
||||||
|
return host
|
||||||
|
|
||||||
|
def GET(self, request, host_id):
|
||||||
|
host = self.get_host(request.db, host_id)
|
||||||
|
|
||||||
response = request.ResponseClass()
|
response = request.ResponseClass()
|
||||||
response.set_payload(None, host)
|
response.set_payload(None, host)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def PUT(self, request, host_id):
|
def PUT(self, request, host_id):
|
||||||
host = request.db.query(model.Host).get(host_id)
|
host = self.get_host(request.db, host_id)
|
||||||
if not host:
|
|
||||||
raise milla.HTTPNotFound
|
|
||||||
|
|
||||||
if request.content_type == 'application/json':
|
if request.content_type == 'application/json':
|
||||||
data = request.json
|
data = request.json
|
||||||
|
@ -95,9 +102,7 @@ class HostController(controllers.BaseController):
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def DELETE(self, request, host_id):
|
def DELETE(self, request, host_id):
|
||||||
host = request.db.query(model.Host).get(host_id)
|
host = self.get_host(request.db, host_id)
|
||||||
if not host:
|
|
||||||
raise milla.HTTPNotFound
|
|
||||||
|
|
||||||
request.db.delete(host)
|
request.db.delete(host)
|
||||||
request.db.commit()
|
request.db.commit()
|
||||||
|
|
Loading…
Reference in New Issue