add message with result when a devices is add or remove of a lot

This commit is contained in:
Cayo Puigdefabregas 2022-03-02 10:36:26 +01:00
parent 6389956adc
commit 15d913efea
2 changed files with 27 additions and 13 deletions

View File

@ -46,9 +46,11 @@ class LotDeviceForm(FlaskForm):
return False return False
self._lot = ( self._lot = (
Lot.query.filter(Lot.id == self.lot.data) Lot.query.outerjoin(Trade)
.filter(Lot.owner_id == g.user.id) .filter(Lot.id == self.lot.data)
.one() .filter(or_(Trade.user_from == g.user,
Trade.user_to == g.user,
Lot.owner_id == g.user.id)).one()
) )
devices = set(self.devices.data.split(",")) devices = set(self.devices.data.split(","))
@ -68,11 +70,13 @@ class LotDeviceForm(FlaskForm):
if trade not in dev.actions: if trade not in dev.actions:
trade.devices.add(dev) trade.devices.add(dev)
if self._devices:
self._lot.devices.update(self._devices) self._lot.devices.update(self._devices)
db.session.add(self._lot) db.session.add(self._lot)
db.session.commit() db.session.commit()
def remove(self): def remove(self):
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)
db.session.commit() db.session.commit()

View File

@ -135,6 +135,11 @@ class LotDeviceAddView(View):
form = LotDeviceForm() form = LotDeviceForm()
if form.validate_on_submit(): if form.validate_on_submit():
form.save() form.save()
messages.success(
'Add devices to lot "{}" successfully!'.format(form._lot.name)
)
else:
messages.error('Error adding devices to lot!')
next_url = request.referrer or url_for('inventory.devices.devicelist') next_url = request.referrer or url_for('inventory.devices.devicelist')
return flask.redirect(next_url) return flask.redirect(next_url)
@ -149,6 +154,11 @@ class LotDeviceDeleteView(View):
form = LotDeviceForm() form = LotDeviceForm()
if form.validate_on_submit(): if form.validate_on_submit():
form.remove() form.remove()
messages.success(
'Remove devices from lot "{}" successfully!'.format(form._lot.name)
)
else:
messages.error('Error removing devices from lot!')
next_url = request.referrer or url_for('inventory.devices.devicelist') next_url = request.referrer or url_for('inventory.devices.devicelist')
return flask.redirect(next_url) return flask.redirect(next_url)