From d17114a0b09862f560b6bd8354b7619650299c58 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 3 Jan 2022 11:32:12 +0100 Subject: [PATCH] remove device from a lot --- ereuse_devicehub/inventory/forms.py | 26 +++++++++------- ereuse_devicehub/inventory/views.py | 23 +++++++++++--- ereuse_devicehub/static/js/main_inventory.js | 5 +++ .../templates/inventory/removinglot.html | 31 ++++++++++++++----- 4 files changed, 61 insertions(+), 24 deletions(-) diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 6ee56861..722509f4 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -1,6 +1,5 @@ from flask_wtf import FlaskForm from wtforms import StringField, validators -from flask_login import current_user from flask import g from ereuse_devicehub.db import db @@ -8,7 +7,7 @@ from ereuse_devicehub.resources.device.models import Device from ereuse_devicehub.resources.lot.models import Lot -class LotDeviceAddForm(FlaskForm): +class LotDeviceForm(FlaskForm): lot = StringField(u'Lot', [validators.UUID()]) devices = StringField(u'Devices', [validators.length(min=1)]) @@ -18,22 +17,26 @@ class LotDeviceAddForm(FlaskForm): if not is_valid: return False - self.lot = Lot.query.filter(Lot.id == self.lot.data).filter( - Lot.owner_id == current_user.id).one() + self._lot = Lot.query.filter(Lot.id == self.lot.data).filter( + Lot.owner_id == g.user.id).one() devices = set(self.devices.data.split(",")) - self.devices = set(Device.query.filter(Device.id.in_(devices)).filter( - Device.owner_id == current_user.id).all()) + self._devices = set(Device.query.filter(Device.id.in_(devices)).filter( + Device.owner_id == g.user.id).all()) - if not self.devices: + if not self._devices: return False return True def save(self): - self.lot.devices.update(self.devices) - g.user = current_user - db.session.add(self.lot) + self._lot.devices.update(self._devices) + db.session.add(self._lot) + db.session.commit() + + def remove(self): + self._lot.devices.difference_update(self._devices) + db.session.add(self._lot) db.session.commit() @@ -45,7 +48,7 @@ class LotForm(FlaskForm): self.lot = None if id: self.lot = Lot.query.filter(Lot.id == id).filter( - Lot.owner_id == current_user.id).one() + Lot.owner_id == g.user.id).one() super().__init__(*args, **kwargs) if self.lot and not self.name.data: self.name.data = self.lot.name @@ -59,6 +62,5 @@ class LotForm(FlaskForm): else: self.lot = Lot(name=name) - g.user = current_user db.session.add(self.lot) db.session.commit() diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 1f21c37b..6a6fc37e 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -5,7 +5,7 @@ from flask_login import login_required, current_user from ereuse_devicehub.resources.lot.models import Lot from ereuse_devicehub.resources.device.models import Device -from ereuse_devicehub.inventory.forms import LotDeviceAddForm, LotForm +from ereuse_devicehub.inventory.forms import LotDeviceForm, LotForm devices = Blueprint('inventory.devices', __name__, url_prefix='/inventory') @@ -29,7 +29,7 @@ class DeviceListView(View): context = {'devices': devices, 'lots': lots, - 'form_lot_device': LotDeviceAddForm(), + 'form_lot_device': LotDeviceForm(), 'lot': lot} return flask.render_template(self.template_name, **context) @@ -40,11 +40,25 @@ class LotDeviceAddView(View): template_name = 'inventory/device_list.html' def dispatch_request(self): - form = LotDeviceAddForm() + form = LotDeviceForm() if form.validate_on_submit(): form.save() - next_url = url_for('inventory.devices.lotdevicelist', id=form.lot.id) + next_url = url_for('inventory.devices.lotdevicelist', id=form.lot.data) + return flask.redirect(next_url) + + +class LotDeviceDeleteView(View): + methods = ['POST'] + decorators = [login_required] + template_name = 'inventory/device_list.html' + + def dispatch_request(self): + form = LotDeviceForm() + if form.validate_on_submit(): + form.remove() + + next_url = url_for('inventory.devices.lotdevicelist', id=form.lot.data) return flask.redirect(next_url) @@ -69,5 +83,6 @@ class LotView(View): devices.add_url_rule('/device/', view_func=DeviceListView.as_view('devicelist')) devices.add_url_rule('/lot//device/', view_func=DeviceListView.as_view('lotdevicelist')) devices.add_url_rule('/lot/devices/add/', view_func=LotDeviceAddView.as_view('lot_devices_add')) +devices.add_url_rule('/lot/devices/del/', view_func=LotDeviceDeleteView.as_view('lot_devices_del')) devices.add_url_rule('/lot/add', view_func=LotView.as_view('lot_add')) devices.add_url_rule('/lot//', view_func=LotView.as_view('lot_edit')) diff --git a/ereuse_devicehub/static/js/main_inventory.js b/ereuse_devicehub/static/js/main_inventory.js index be9f1ef8..dd121e40 100644 --- a/ereuse_devicehub/static/js/main_inventory.js +++ b/ereuse_devicehub/static/js/main_inventory.js @@ -9,10 +9,15 @@ function deviceSelect() { if (devices_id == "") { $("#addingLotModal .text-danger").show(); $("#addingLotModal .btn-primary").hide(); + $("#removeLotModal .text-danger").show(); + $("#removeLotModal .btn-primary").hide(); } else { $("#addingLotModal .text-danger").hide(); $("#addingLotModal .btn-primary").removeClass('d-none'); $("#addingLotModal .btn-primary").show(); + $("#removeLotModal .text-danger").hide(); + $("#removeLotModal .btn-primary").removeClass('d-none'); + $("#removeLotModal .btn-primary").show(); } $.map($(".devicesList"), function(x) { $(x).val(devices_id); diff --git a/ereuse_devicehub/templates/inventory/removinglot.html b/ereuse_devicehub/templates/inventory/removinglot.html index 46adf198..81ba6be6 100644 --- a/ereuse_devicehub/templates/inventory/removinglot.html +++ b/ereuse_devicehub/templates/inventory/removinglot.html @@ -2,16 +2,31 @@