use commit=True for default as django pattern, when we save or remove one model

This commit is contained in:
Cayo Puigdefabregas 2022-03-07 11:37:32 +01:00
parent 54a938071b
commit e90ac224c1
2 changed files with 10 additions and 4 deletions

View File

@ -62,7 +62,7 @@ class LotDeviceForm(FlaskForm):
return bool(self._devices) return bool(self._devices)
def save(self): def save(self, commit=True):
trade = self._lot.trade trade = self._lot.trade
if trade: if trade:
for dev in self._devices: for dev in self._devices:
@ -73,11 +73,17 @@ class LotDeviceForm(FlaskForm):
self._lot.devices.update(self._devices) self._lot.devices.update(self._devices)
db.session.add(self._lot) db.session.add(self._lot)
def remove(self): if commit:
db.session.commit()
def remove(self, commit=True):
if self._devices: if self._devices:
self._lot.devices.difference_update(self._devices) self._lot.devices.difference_update(self._devices)
db.session.add(self._lot) db.session.add(self._lot)
if commit:
db.session.commit()
class LotForm(FlaskForm): class LotForm(FlaskForm):
name = StringField('Name', [validators.length(min=1)]) name = StringField('Name', [validators.length(min=1)])

View File

@ -139,7 +139,7 @@ class LotDeviceAddView(View):
def dispatch_request(self): def dispatch_request(self):
form = LotDeviceForm() form = LotDeviceForm()
if form.validate_on_submit(): if form.validate_on_submit():
form.save() form.save(commit=False)
messages.success( messages.success(
'Add devices to lot "{}" successfully!'.format(form._lot.name) 'Add devices to lot "{}" successfully!'.format(form._lot.name)
) )
@ -159,7 +159,7 @@ class LotDeviceDeleteView(View):
def dispatch_request(self): def dispatch_request(self):
form = LotDeviceForm() form = LotDeviceForm()
if form.validate_on_submit(): if form.validate_on_submit():
form.remove() form.remove(commit=False)
messages.success( messages.success(
'Remove devices from lot "{}" successfully!'.format(form._lot.name) 'Remove devices from lot "{}" successfully!'.format(form._lot.name)
) )