diff options
author | Lars Wirzenius <lars.wirzenius@codethink.co.uk> | 2014-06-23 16:04:33 +0000 |
---|---|---|
committer | Lars Wirzenius <lars.wirzenius@codethink.co.uk> | 2014-06-23 16:47:43 +0000 |
commit | e530deefe02b7f6de2748f7d99f713bc9d4abfe5 (patch) | |
tree | ab74624c4e5dddc56bcda7d88176b8f9098466c9 | |
parent | 376c03c53bc86b88f14ef46c036795eb52186805 (diff) | |
download | lorry-controller-e530deefe02b7f6de2748f7d99f713bc9d4abfe5.tar.gz |
Move shared code to a base class, less code duplication
-rw-r--r-- | lorrycontroller/movetopbottom.py | 44 |
1 files changed, 22 insertions, 22 deletions
diff --git a/lorrycontroller/movetopbottom.py b/lorrycontroller/movetopbottom.py index dcb79a4..7c1664a 100644 --- a/lorrycontroller/movetopbottom.py +++ b/lorrycontroller/movetopbottom.py @@ -21,10 +21,7 @@ import bottle import lorrycontroller -class MoveToTop(lorrycontroller.LorryControllerRoute): - - http_method = 'POST' - path = '/1.0/move-to-top' +class MoveBase(object): def run(self, **kwargs): logging.info('%s %s called', self.http_method, self.path) @@ -32,27 +29,30 @@ class MoveToTop(lorrycontroller.LorryControllerRoute): statedb = self.open_statedb() with statedb: lorry_infos = statedb.get_all_lorries_info() - if lorry_infos: - topmost = lorry_infos[0] - timestamp = min(0, topmost['last_run'] - 1) - statedb.set_lorry_last_run(path, timestamp) - return 'Lorry %s moved to top of run-queue' % path + timestamp = self.get_new_timestamp(lorry_infos) + statedb.set_lorry_last_run(path, timestamp) + return self.msg % path -class MoveToBottom(lorrycontroller.LorryControllerRoute): +class MoveToTop(MoveBase, lorrycontroller.LorryControllerRoute): + + http_method = 'POST' + path = '/1.0/move-to-top' + msg = 'Lorry %s moved to the top of the run queue' + + def get_new_timestamp(self, lorry_infos): + topmost = lorry_infos[0] + timestamp = min(0, topmost['last_run'] - 1) + return timestamp + + +class MoveToBottom(MoveBase, lorrycontroller.LorryControllerRoute): http_method = 'POST' path = '/1.0/move-to-bottom' + msg = 'Lorry %s moved to the bottom of the run queue' - def run(self, **kwargs): - logging.info('%s %s called', self.http_method, self.path) - path = bottle.request.forms.path - statedb = self.open_statedb() - with statedb: - lorry_infos = statedb.get_all_lorries_info() - if lorry_infos: - bottommost = lorry_infos[-1] - timestamp = ( - bottommost['last_run'] + bottommost['interval'] + 1) - statedb.set_lorry_last_run(path, timestamp) - return 'Lorry %s moved to bototm of run-queue' % path + def get_new_timestamp(self, lorry_infos): + bottommost = lorry_infos[-1] + timestamp = bottommost['last_run'] + bottommost['interval'] + 1 + return timestamp |