From 598b91e22b78f40e0589462145cae9c272a67547 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 7 Mar 2022 13:07:26 +0100 Subject: [PATCH] filter search in lists --- ereuse_devicehub/inventory/forms.py | 20 +++++++++++++++++++ ereuse_devicehub/inventory/views.py | 15 +++++--------- .../templates/inventory/device_list.html | 16 ++++++++++----- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 7702be79..945270b4 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -35,6 +35,26 @@ from ereuse_devicehub.resources.action.models import Trade from sqlalchemy import or_ +DEVICES = [ + ("Computer", "Computer"), + ("Monitor", "Monitor"), +] + + +class FilterForm(FlaskForm): + filter = SelectField('', choices=DEVICES, default="Comupter", + render_kw={'class': "form-select"}) + + def search(self): + + type_device = { + None: ['Desktop', 'Laptop', 'Server'], + 'Computer': ['Computer'], + 'Monitor': ['Monitor'], + } + return type_device.get(request.args.get('filter')) or type_device[None] + + class LotDeviceForm(FlaskForm): lot = StringField('Lot', [validators.UUID()]) devices = StringField('Devices', [validators.length(min=1)]) diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 2329fa22..9c4d2e10 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -13,6 +13,7 @@ from ereuse_devicehub import messages from ereuse_devicehub.inventory.forms import ( AllocateForm, DataWipeForm, + FilterForm, LotDeviceForm, LotForm, NewActionForm, @@ -40,15 +41,9 @@ class DeviceListMix(View): template_name = 'inventory/device_list.html' def get_context(self, lot_id): - # TODO @cayop adding filter - # https://github.com/eReuse/devicehub-teal/blob/testing/ereuse_devicehub/resources/device/views.py#L56 - import pdb; pdb.set_trace() - type_device = { - None: ['Desktop', 'Laptop', 'Server'], - 'Computer': ['Computer'], - 'Monitor': ['Monitor'], - } - filter_types = type_device[request.get('filter')] + form_filter = FilterForm() + filter_types = form_filter.search() + lots = Lot.query.outerjoin(Trade) \ .filter(or_(Trade.user_from == g.user, Trade.user_to == g.user, @@ -61,7 +56,6 @@ class DeviceListMix(View): ) if lot_id: - # import pdb; pdb.set_trace() lot = lots.filter(Lot.id == lot_id).one() devices = [dev for dev in lot.devices if dev.type in filter_types] devices = sorted(devices, key=lambda x: x.updated, reverse=True) @@ -98,6 +92,7 @@ class DeviceListMix(View): 'form_new_allocate': form_new_allocate, 'form_new_datawipe': form_new_datawipe, 'form_new_trade': form_new_trade, + 'form_filter': form_filter, 'lot': lot, 'tags': tags, 'list_devices': list_devices, diff --git a/ereuse_devicehub/templates/inventory/device_list.html b/ereuse_devicehub/templates/inventory/device_list.html index 4170318d..ae545734 100644 --- a/ereuse_devicehub/templates/inventory/device_list.html +++ b/ereuse_devicehub/templates/inventory/device_list.html @@ -280,11 +280,17 @@
Computers
- - +
+
+ {% for f in form_filter %} + {{ f }} + {% endfor %} +
+
+ +
+
+