diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 089f4b48..e29900ab 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -152,7 +152,7 @@ class LotDeviceForm(FlaskForm): if self._devices: lots = [set(dev.lots) for dev in self._devices] - if self.action == 'remove' and lots: + if self.action == 'del' and lots: x = lots[0] common_lots = x.intersection(*lots[1:]) self.lot.choices = [ @@ -160,8 +160,9 @@ class LotDeviceForm(FlaskForm): ] elif self.action == 'add' and lots: x = lots[0] + common_lots = x.union(*lots[1:]) self.lot.choices = [ - (lot.id, lot.name) for lot in self._lots if lot not in lots + (lot.id, lot.name) for lot in self._lots if lot not in common_lots ] else: self.lot.choices = [(lot.id, lot.name) for lot in self._lots] @@ -1076,10 +1077,17 @@ class TradeDocumentForm(FlaskForm): class LotDeviceShowForm(FlaskForm): devices = StringField(render_kw={'class': "devicesList d-none"}) + action = StringField(render_kw={'class': "d-none"}) def validate(self, extra_validators=None): is_valid = super().validate(extra_validators) + if not self.devices.data: + return False + + if self.action.data not in ['add', 'del']: + return False + device_ids = self.devices.data.split(",") self._devices = Device.query.filter(Device.id.in_(device_ids)).all() diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 4164b93c..609e487e 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -120,7 +120,8 @@ class DeviceListMix(GenericMixView): 'form_new_datawipe': form_new_datawipe, 'form_new_trade': form_new_trade, 'form_filter': form_filter, - 'form_lot_device_del': LotDeviceShowForm(), + 'form_lot_device_del': LotDeviceShowForm(action='del'), + 'form_lot_device_add': LotDeviceShowForm(action='add'), 'lot': lot, 'tags': tags, 'list_devices': list_devices, @@ -175,31 +176,6 @@ class LotDeviceAddView(View): return flask.redirect(next_url) -class LotDeviceDeleteShowView(GenericMixView): - methods = ['POST'] - decorators = [login_required] - template_name = 'inventory/removeDeviceslot2.html' - title = 'Remove from a lot' - - def dispatch_request(self, lot_id=None): - # import pdb; pdb.set_trace() - next_url = request.referrer or url_for('inventory.devices.devicelist') - form = LotDeviceShowForm() - if not form.validate_on_submit(): - messages.error('Error, you need select one or more devices!') - if lot_id: - next_url = url_for('inventory.devices.lotdevicelist', lot_id=form.id) - - return flask.redirect(next_url) - - lots = self.get_lots() - 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) - - class LotDeviceDeleteView(View): methods = ['POST'] decorators = [login_required] @@ -220,6 +196,36 @@ class LotDeviceDeleteView(View): return flask.redirect(next_url) +class LotDeviceView(GenericMixView): + methods = ['POST'] + decorators = [login_required] + template_name = 'inventory/removeDeviceslot2.html' + title = 'Remove from a lot' + + def dispatch_request(self, lot_id=None): + # import pdb; pdb.set_trace() + url = url_for('inventory.devices.lot_devices_del') + next_url = request.referrer or url_for('inventory.devices.devicelist') + form = LotDeviceShowForm() + if not form.validate_on_submit(): + messages.error('Error, you need select one or more devices!') + if lot_id: + next_url = url_for('inventory.devices.lotdevicelist', lot_id=form.id) + + return flask.redirect(next_url) + + if form.action.data == 'add': + self.title = 'Add devices to a lot' + url = url_for('inventory.devices.lot_devices_add') + + lots = self.get_lots() + form_lot = LotDeviceForm( + devices=form.devices, action=form.action.data, _devices=form._devices + ) + context = {'form': form_lot, 'title': self.title, 'lots': lots, 'url': url} + return flask.render_template(self.template_name, **context) + + class LotCreateView(GenericMixView): methods = ['GET', 'POST'] decorators = [login_required] @@ -703,10 +709,7 @@ devices.add_url_rule( devices.add_url_rule( '/lot/devices/del/', view_func=LotDeviceDeleteView.as_view('lot_devices_del') ) -devices.add_url_rule( - '/lot/devices/del/show', - view_func=LotDeviceDeleteShowView.as_view('lot_devices_del_show'), -) +devices.add_url_rule('/lot/devices/', view_func=LotDeviceView.as_view('lot_devices')) devices.add_url_rule('/lot/add/', view_func=LotCreateView.as_view('lot_add')) devices.add_url_rule( '/lot//del/', view_func=LotDeleteView.as_view('lot_del') diff --git a/ereuse_devicehub/static/js/main_inventory.js b/ereuse_devicehub/static/js/main_inventory.js index 86c25a82..6b129ee2 100644 --- a/ereuse_devicehub/static/js/main_inventory.js +++ b/ereuse_devicehub/static/js/main_inventory.js @@ -180,3 +180,11 @@ function export_file(type_file) { $("#exportAlertModal").click(); } } + +function lot_devices_del() { + $('#lot_devices_del').submit(); +} + +function lot_devices_add() { + $('#lot_devices_add').submit(); +} diff --git a/ereuse_devicehub/templates/inventory/device_list.html b/ereuse_devicehub/templates/inventory/device_list.html index 82988be3..eeb6d226 100644 --- a/ereuse_devicehub/templates/inventory/device_list.html +++ b/ereuse_devicehub/templates/inventory/device_list.html @@ -93,11 +93,22 @@
  • -
    + + {% for f in form_lot_device_add %} + {{ f }} + {% endfor %} + + + Add selected Devices to a lot + +
    +
  • +
  • +
    {% for f in form_lot_device_del %} {{ f }} {% endfor %} - + Remove selected devices from a lot