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)