From 3e33b7d63bf8f3a3e2ec6569e1caee002bffb884 Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Wed, 13 Apr 2022 09:44:30 +0200 Subject: [PATCH 1/6] 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): From 4ffbeec29d71f7f0adafaf62646498bbed197fe6 Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Thu, 12 May 2022 14:23:01 +0200 Subject: [PATCH 2/6] Refactor form initialization --- ereuse_devicehub/inventory/views.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 795e8266..92a16ff8 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -49,27 +49,22 @@ class DeviceListMix(GenericMixView): if lot_id: lot = lots.filter(Lot.id == lot_id).one() - form_new_action = NewActionForm(lot=lot.id) - form_new_allocate = AllocateForm(lot=lot.id) - form_new_datawipe = DataWipeForm(lot=lot.id) form_new_trade = TradeForm( lot=lot.id, user_to=g.user.email, user_from=g.user.email, ) else: - form_new_action = NewActionForm() - form_new_allocate = AllocateForm() - form_new_datawipe = DataWipeForm() form_new_trade = '' + form_new_action = NewActionForm(lot=lot_id) self.context.update( { 'devices': devices, 'form_tag_device': TagDeviceForm(), 'form_new_action': form_new_action, - 'form_new_allocate': form_new_allocate, - 'form_new_datawipe': form_new_datawipe, + 'form_new_allocate': AllocateForm(lot=lot_id), + 'form_new_datawipe': DataWipeForm(lot=lot_id), 'form_new_trade': form_new_trade, 'form_filter': form_filter, 'form_print_labels': PrintLabelsForm(), From 9e779d279c7abfba6cb2b4cdedae8182dc7fa242 Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Thu, 12 May 2022 14:42:55 +0200 Subject: [PATCH 3/6] Allow retrieving all devices of a user --- ereuse_devicehub/inventory/forms.py | 7 ++++--- ereuse_devicehub/inventory/views.py | 8 +++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 062d185c..fffefa39 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -133,6 +133,7 @@ class FilterForm(FlaskForm): super().__init__(*args, **kwargs) self.lots = lots self.lot_id = lot_id + self.unassigned = kwargs.pop('unassigned', False) self._get_types() def _get_types(self): @@ -148,9 +149,9 @@ class FilterForm(FlaskForm): device_ids = (d.id for d in self.lot.devices) self.devices = Device.query.filter(Device.id.in_(device_ids)) else: - self.devices = Device.query.filter(Device.owner_id == g.user.id).filter_by( - lots=None - ) + self.devices = Device.query.filter(Device.owner_id == g.user.id) + if self.unassigned: + self.devices = self.devices.filter_by(lots=None) def search(self): self.filter_from_lots() diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 92a16ff8..e11e9d2c 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -40,10 +40,10 @@ logger = logging.getLogger(__name__) class DeviceListMix(GenericMixView): template_name = 'inventory/device_list.html' - def get_context(self, lot_id): + def get_context(self, lot_id, unassigned=False): super().get_context() lots = self.context['lots'] - form_filter = FilterForm(lots, lot_id) + form_filter = FilterForm(lots, lot_id, unassigned=unassigned) devices = form_filter.search() lot = None @@ -71,6 +71,7 @@ class DeviceListMix(GenericMixView): 'lot': lot, 'tags': self.get_user_tags(), 'list_devices': self.get_selected_devices(form_new_action), + 'unassigned_devices': unassigned, } ) @@ -93,7 +94,8 @@ class DeviceListMix(GenericMixView): class DeviceListView(DeviceListMix): def dispatch_request(self, lot_id=None): - self.get_context(lot_id) + unassigned = request.args.get('unassigned', False) + self.get_context(lot_id, unassigned) return flask.render_template(self.template_name, **self.context) From 290c260511968df70ddd8721f71feae3cb05d41b Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Thu, 12 May 2022 14:43:41 +0200 Subject: [PATCH 4/6] Update breadcrum: All devices VS Unassigned --- ereuse_devicehub/templates/inventory/device_list.html | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ereuse_devicehub/templates/inventory/device_list.html b/ereuse_devicehub/templates/inventory/device_list.html index a3c13576..1c129d97 100644 --- a/ereuse_devicehub/templates/inventory/device_list.html +++ b/ereuse_devicehub/templates/inventory/device_list.html @@ -7,7 +7,11 @@