From 3e33b7d63bf8f3a3e2ec6569e1caee002bffb884 Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Wed, 13 Apr 2022 09:44:30 +0200 Subject: [PATCH] Refactor DeviceListMix: split code in smaller methods --- ereuse_devicehub/inventory/views.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index a09f4f5b..795e8266 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -46,11 +46,6 @@ class DeviceListMix(GenericMixView): form_filter = FilterForm(lots, lot_id) devices = form_filter.search() lot = None - tags = ( - Tag.query.filter(Tag.owner_id == current_user.id) - .filter(Tag.device_id.is_(None)) - .order_by(Tag.id.asc()) - ) if lot_id: lot = lots.filter(Lot.id == lot_id).one() @@ -67,10 +62,6 @@ class DeviceListMix(GenericMixView): form_new_allocate = AllocateForm() form_new_datawipe = DataWipeForm() form_new_trade = '' - action_devices = form_new_action.devices.data - list_devices = [] - if action_devices: - list_devices.extend([int(x) for x in action_devices.split(",")]) self.context.update( { @@ -83,13 +74,27 @@ class DeviceListMix(GenericMixView): 'form_filter': form_filter, 'form_print_labels': PrintLabelsForm(), 'lot': lot, - 'tags': tags, - 'list_devices': list_devices, + 'tags': self.get_user_tags(), + 'list_devices': self.get_selected_devices(form_new_action), } ) return self.context + def get_user_tags(self): + return ( + Tag.query.filter(Tag.owner_id == current_user.id) + .filter(Tag.device_id.is_(None)) + .order_by(Tag.id.asc()) + ) + + def get_selected_devices(self, action_form): + """Retrieve selected devices (when action form is submited)""" + action_devices = action_form.devices.data + if action_devices: + return [int(x) for x in action_devices.split(",")] + return [] + class DeviceListView(DeviceListMix): def dispatch_request(self, lot_id=None):