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

View file

@ -135,6 +135,11 @@ class LotDeviceAddView(View):
form = LotDeviceForm()
if form.validate_on_submit():
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')
return flask.redirect(next_url)
@ -149,6 +154,11 @@ class LotDeviceDeleteView(View):
form = LotDeviceForm()
if form.validate_on_submit():
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')
return flask.redirect(next_url)