From e90ac224c102b0f15d35aeacff0a558e46d546d4 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 7 Mar 2022 11:37:32 +0100 Subject: [PATCH] use commit=True for default as django pattern, when we save or remove one model --- ereuse_devicehub/inventory/forms.py | 10 ++++++++-- ereuse_devicehub/inventory/views.py | 4 ++-- 2 files changed, 10 insertions(+), 4 deletions(-) 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) )