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,14 +70,16 @@ class LotDeviceForm(FlaskForm):
if trade not in dev.actions: if trade not in dev.actions:
trade.devices.add(dev) trade.devices.add(dev)
self._lot.devices.update(self._devices) if self._devices:
db.session.add(self._lot) self._lot.devices.update(self._devices)
db.session.commit() db.session.add(self._lot)
db.session.commit()
def remove(self): def remove(self):
self._lot.devices.difference_update(self._devices) if self._devices:
db.session.add(self._lot) self._lot.devices.difference_update(self._devices)
db.session.commit() db.session.add(self._lot)
db.session.commit()
class LotForm(FlaskForm): class LotForm(FlaskForm):

View File

@ -135,9 +135,14 @@ 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)
class LotDeviceDeleteView(View): class LotDeviceDeleteView(View):
@ -149,9 +154,14 @@ 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)
class LotCreateView(View): class LotCreateView(View):