diff options
Diffstat (limited to 'lorrycontroller/showlorry.py')
-rw-r--r-- | lorrycontroller/showlorry.py | 35 |
1 files changed, 23 insertions, 12 deletions
diff --git a/lorrycontroller/showlorry.py b/lorrycontroller/showlorry.py index fc336a5..19f9429 100644 --- a/lorrycontroller/showlorry.py +++ b/lorrycontroller/showlorry.py @@ -24,32 +24,43 @@ import bottle import lorrycontroller -class ShowLorry(lorrycontroller.LorryControllerRoute): +class ShowLorryBase(object): + + def get_lorry_info_with_job_lists(self, statedb, path): + obj= statedb.get_lorry_info(path) + obj['jobs'] = statedb.get_jobs_for_lorry(path) + obj['failed_jobs'] = statedb.get_failed_jobs_for_lorry(path) + return obj + + +class ShowLorry(ShowLorryBase, lorrycontroller.LorryControllerRoute): http_method = 'GET' path = '/1.0/lorry/<path:path>' def run(self, **kwargs): logging.info('%s %s called', self.http_method, self.path) - statedb = self.open_statedb() - try: - return statedb.get_lorry_info(kwargs['path']) - except lorrycontroller.LorryNotFoundError as e: - bottle.abort(404, str(e)) + with self.open_statedb() as statedb: + try: + return self.get_lorry_info_with_job_lists(statedb, kwargs['path']) + except lorrycontroller.LorryNotFoundError as e: + bottle.abort(404, str(e)) -class ShowLorryHTML(lorrycontroller.LorryControllerRoute): +class ShowLorryHTML(ShowLorryBase, lorrycontroller.LorryControllerRoute): http_method = 'GET' path = '/1.0/lorry-html/<path:path>' def run(self, **kwargs): logging.info('%s %s called', self.http_method, self.path) - statedb = self.open_statedb() - try: - lorry_info = statedb.get_lorry_info(kwargs['path']) - except lorrycontroller.LorryNotFoundError as e: - bottle.abort(404, str(e)) + + with self.open_statedb() as statedb: + try: + lorry_info = self.get_lorry_info_with_job_lists( + statedb, kwargs['path']) + except lorrycontroller.LorryNotFoundError as e: + bottle.abort(404, str(e)) renderer = lorrycontroller.StatusRenderer() shower = lorrycontroller.JobShower() |