diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 86221aaf..089f4b48 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -137,15 +137,34 @@ class FilterForm(FlaskForm): class LotDeviceForm(FlaskForm): - devices = StringField('Devices', [validators.length(min=1)]) - lot = SelectField('Lot', choices=[]) + devices = StringField( + 'Devices', [validators.length(min=1)], render_kw={'class': "d-none"} + ) + lot = SelectField('Lot', choices=[], render_kw={'class': "form-select"}) def __init__(self, *args, **kwargs): + self.action = kwargs.pop('action', None) + self._devices = kwargs.pop('_devices', None) super().__init__(*args, **kwargs) - # TODO - self.lot.choices = [ - (lot.id, lot.name) for lot in Lot.query.filter(Lot.owner_id == g.user.id) - ] + # import pdb; pdb.set_trace() + self._lots = Lot.query.filter(Lot.owner_id == g.user.id) + lots = [] + if self._devices: + lots = [set(dev.lots) for dev in self._devices] + + if self.action == 'remove' and lots: + x = lots[0] + common_lots = x.intersection(*lots[1:]) + self.lot.choices = [ + (lot.id, lot.name) for lot in self._lots if lot in common_lots + ] + elif self.action == 'add' and lots: + x = lots[0] + self.lot.choices = [ + (lot.id, lot.name) for lot in self._lots if lot not in lots + ] + else: + self.lot.choices = [(lot.id, lot.name) for lot in self._lots] def validate(self, extra_validators=None): is_valid = super().validate(extra_validators) @@ -1056,7 +1075,7 @@ class TradeDocumentForm(FlaskForm): class LotDeviceShowForm(FlaskForm): - devices = StringField(render_kw={'class': "devicesList"}) + devices = StringField(render_kw={'class': "devicesList d-none"}) def validate(self, extra_validators=None): is_valid = super().validate(extra_validators) diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 05aa8656..4164b93c 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -193,7 +193,9 @@ class LotDeviceDeleteShowView(GenericMixView): return flask.redirect(next_url) lots = self.get_lots() - form_lot = LotDeviceForm(devices=form.devices) + form_lot = LotDeviceForm( + devices=form.devices, action='remove', _devices=form._devices + ) context = {'form': form_lot, 'title': self.title, 'lots': lots} return flask.render_template(self.template_name, **context) @@ -214,7 +216,7 @@ class LotDeviceDeleteView(View): else: messages.error('Error removing devices from lot!') - next_url = request.referrer or url_for('inventory.devices.devicelist') + next_url = url_for('inventory.devices.lotdevicelist', lot_id=form._lot.id) return flask.redirect(next_url) diff --git a/ereuse_devicehub/templates/inventory/device_list.html b/ereuse_devicehub/templates/inventory/device_list.html index 9d7986b6..82988be3 100644 --- a/ereuse_devicehub/templates/inventory/device_list.html +++ b/ereuse_devicehub/templates/inventory/device_list.html @@ -97,7 +97,10 @@ {% for f in form_lot_device_del %} {{ f }} {% endfor %} - + + + Remove selected devices from a lot +