diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 173e1562..260218fe 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -62,7 +62,7 @@ class LotDeviceForm(FlaskForm): return bool(self._devices) - def save(self): + def save(self, commit=True): trade = self._lot.trade if trade: for dev in self._devices: @@ -73,11 +73,17 @@ class LotDeviceForm(FlaskForm): self._lot.devices.update(self._devices) db.session.add(self._lot) - def remove(self): + if commit: + db.session.commit() + + def remove(self, commit=True): if self._devices: self._lot.devices.difference_update(self._devices) db.session.add(self._lot) + if commit: + db.session.commit() + class LotForm(FlaskForm): name = StringField('Name', [validators.length(min=1)]) diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 2e666f39..84af2d9b 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -139,7 +139,7 @@ class LotDeviceAddView(View): def dispatch_request(self): form = LotDeviceForm() if form.validate_on_submit(): - form.save() + form.save(commit=False) messages.success( 'Add devices to lot "{}" successfully!'.format(form._lot.name) ) @@ -159,7 +159,7 @@ class LotDeviceDeleteView(View): def dispatch_request(self): form = LotDeviceForm() if form.validate_on_submit(): - form.remove() + form.remove(commit=False) messages.success( 'Remove devices from lot "{}" successfully!'.format(form._lot.name) )