From 252c5285d834a77e10902a8eb5bc55dc8c9cd971 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 9 May 2022 11:12:34 +0200 Subject: [PATCH 01/15] refactorice --- ereuse_devicehub/inventory/forms.py | 2 +- ereuse_devicehub/inventory/views.py | 48 ++++++++++++++++------------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index d58fcaf9..1132b4ba 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -52,7 +52,7 @@ from ereuse_devicehub.resources.user.exceptions import InsufficientPermission from ereuse_devicehub.resources.user.models import User DEVICES = { - "All": ["All"], + "All": ["Devices", "Components"], "Computer": [ "Desktop", "Laptop", diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 2df8fd69..ab93902a 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -40,32 +40,13 @@ logger = logging.getLogger(__name__) class DeviceListMix(GenericMixView): template_name = 'inventory/device_list.html' - def get_context(self, lot_id): - super().get_context() - lots = self.context['lots'] - form_filter = FilterForm() - filter_types = 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()) - ) - + def _get_devices(self, lots, lot): if lot_id: lot = lots.filter(Lot.id == lot_id).one() devices = lot.devices if "All" not in filter_types: devices = [dev for dev in lot.devices if dev.type in filter_types] devices = sorted(devices, key=lambda x: x.updated, reverse=True) - 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: if "All" in filter_types: devices = ( @@ -81,6 +62,31 @@ class DeviceListMix(GenericMixView): .order_by(Device.updated.desc()) ) + return devices + + def get_context(self, lot_id): + super().get_context() + lots = self.context['lots'] + form_filter = FilterForm() + filter_types = 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() + 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() @@ -92,7 +98,7 @@ class DeviceListMix(GenericMixView): self.context.update( { - 'devices': devices, + 'devices': self._get_devices(lots, lot), 'form_tag_device': TagDeviceForm(), 'form_new_action': form_new_action, 'form_new_allocate': form_new_allocate, From 04e4f4ff74ec460c83ebb5b0fcbc00e1f35a1c12 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 9 May 2022 13:43:02 +0200 Subject: [PATCH 02/15] add generics filters --- ereuse_devicehub/inventory/forms.py | 113 +++++++++++++++++++++++++--- ereuse_devicehub/inventory/views.py | 30 +------- 2 files changed, 104 insertions(+), 39 deletions(-) diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 1132b4ba..8ef0835a 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -52,16 +52,30 @@ from ereuse_devicehub.resources.user.exceptions import InsufficientPermission from ereuse_devicehub.resources.user.models import User DEVICES = { - "All": ["Devices", "Components"], + "All": ["All Devices", "All Components"], "Computer": [ + "All Computers", "Desktop", "Laptop", "Server", ], - "Monitor": ["ComputerMonitor", "Monitor", "TelevisionSet", "Projector"], - "Mobile, tablet & smartphone": ["Mobile", "Tablet", "Smartphone", "Cellphone"], - "DataStorage": ["HardDrive", "SolidStateDrive"], + "Monitor": [ + "All Monitors", + "ComputerMonitor", + "Monitor", + "TelevisionSet", + "Projector", + ], + "Mobile, tablet & smartphone": [ + "All Mobile", + "Mobile", + "Tablet", + "Smartphone", + "Cellphone", + ], + "DataStorage": ["All DataStorage", "HardDrive", "SolidStateDrive"], "Accessories & Peripherals": [ + "All Peripherals", "GraphicCard", "Motherboard", "NetworkAdapter", @@ -75,26 +89,101 @@ DEVICES = { ], } +COMPUTERS = ['Desktop', 'Laptop', 'Server'] + +COMPONENTS = [ + 'GraphicCard', + 'DataStorage', + 'HardDrive', + 'DataStorage', + 'SolidStateDrive', + 'Motherboard', + 'NetworkAdapter', + 'Processor', + 'RamModule', + 'SoundCard', + 'Display', + 'Battery', + 'Camera', +] + +MONITORS = ["ComputerMonitor", "Monitor", "TelevisionSet", "Projector"] +MOBILE = ["Mobile", "Tablet", "Smartphone", "Cellphone"] +DATASTORAGE = ["HardDrive", "SolidStateDrive"] +PERIPHERALS = [ + "GraphicCard", + "Motherboard", + "NetworkAdapter", + "Processor", + "RamModule", + "SoundCard", + "Battery", + "Keyboard", + "Mouse", + "MemoryCardReader", +] + class FilterForm(FlaskForm): filter = SelectField( - '', choices=DEVICES, default="Computer", render_kw={'class': "form-select"} + '', choices=DEVICES, default="All Computers", render_kw={'class': "form-select"} ) - def __init__(self, *args, **kwargs): + def __init__(self, lots, lot_id, *args, **kwargs): super().__init__(*args, **kwargs) + self.lots = lots + self.lot_id = lot_id + self._get_types() + + def _get_types(self): types_of_devices = [item for sublist in DEVICES.values() for item in sublist] dev = request.args.get('filter') - self.device = dev if dev in types_of_devices else None - if self.device: - self.filter.data = self.device + self.device_type = dev if dev in types_of_devices else None + if self.device_type: + self.filter.data = self.device_type def search(self): - if self.device: - return [self.device] + # Filter from lots + if self.lot_id: + self.lot = self.lots.filter(Lot.id == self.lot_id).one() + 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 + ) - return ['Desktop', 'Laptop', 'Server'] + filter_type = None + if self.device_type: + filter_type = [self.device_type] + else: + # Case without Filter + filter_type = COMPUTERS + + # Generic Filters + if "All Devices" == self.device_type: + filter_type = None + + if "All Components" == self.device_type: + filter_type = COMPONENTS + + elif "All Monitors" == self.device_type: + filter_type = MONITORS + + elif "All Mobile" == self.device_type: + filter_type = MOBILE + + elif "All DataStorage" == self.device_type: + filter_type = DATASTORAGE + + elif "All Peripherals" == self.device_type: + filter_type = PERIPHERALS + + if filter_type: + self.devices = self.devices.filter(Device.type.in_(filter_type)) + + return self.devices.order_by(Device.updated.desc()) class LotForm(FlaskForm): diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index ab93902a..a09f4f5b 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -40,35 +40,11 @@ logger = logging.getLogger(__name__) class DeviceListMix(GenericMixView): template_name = 'inventory/device_list.html' - def _get_devices(self, lots, lot): - if lot_id: - lot = lots.filter(Lot.id == lot_id).one() - devices = lot.devices - if "All" not in filter_types: - devices = [dev for dev in lot.devices if dev.type in filter_types] - devices = sorted(devices, key=lambda x: x.updated, reverse=True) - else: - if "All" in filter_types: - devices = ( - Device.query.filter(Device.owner_id == current_user.id) - .filter_by(lots=None) - .order_by(Device.updated.desc()) - ) - else: - devices = ( - Device.query.filter(Device.owner_id == current_user.id) - .filter_by(lots=None) - .filter(Device.type.in_(filter_types)) - .order_by(Device.updated.desc()) - ) - - return devices - def get_context(self, lot_id): super().get_context() lots = self.context['lots'] - form_filter = FilterForm() - filter_types = form_filter.search() + form_filter = FilterForm(lots, lot_id) + devices = form_filter.search() lot = None tags = ( Tag.query.filter(Tag.owner_id == current_user.id) @@ -98,7 +74,7 @@ class DeviceListMix(GenericMixView): self.context.update( { - 'devices': self._get_devices(lots, lot), + 'devices': devices, 'form_tag_device': TagDeviceForm(), 'form_new_action': form_new_action, 'form_new_allocate': form_new_allocate, From 651c0cb9d88dd11cc07a5e4cdba9232a284c45bd Mon Sep 17 00:00:00 2001 From: RubenPX Date: Mon, 9 May 2022 13:57:17 +0200 Subject: [PATCH 03/15] Fix text select lots instead devices --- ereuse_devicehub/templates/inventory/device_list.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ereuse_devicehub/templates/inventory/device_list.html b/ereuse_devicehub/templates/inventory/device_list.html index ef8f7aef..2b1d5cec 100644 --- a/ereuse_devicehub/templates/inventory/device_list.html +++ b/ereuse_devicehub/templates/inventory/device_list.html @@ -84,7 +84,7 @@