web: hosts: Add Host ID validation
parent
d51cb1d8d3
commit
26532cf76f
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue