diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 00fe0240..d8677dd2 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -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,14 +70,16 @@ class LotDeviceForm(FlaskForm): if trade not in dev.actions: trade.devices.add(dev) - self._lot.devices.update(self._devices) - db.session.add(self._lot) - db.session.commit() + if self._devices: + self._lot.devices.update(self._devices) + db.session.add(self._lot) + db.session.commit() def remove(self): - self._lot.devices.difference_update(self._devices) - db.session.add(self._lot) - db.session.commit() + if self._devices: + self._lot.devices.difference_update(self._devices) + db.session.add(self._lot) + db.session.commit() class LotForm(FlaskForm): diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index c0059183..62cd97cb 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -135,9 +135,14 @@ 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) + next_url = request.referrer or url_for('inventory.devices.devicelist') + return flask.redirect(next_url) class LotDeviceDeleteView(View): @@ -149,9 +154,14 @@ 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) + next_url = request.referrer or url_for('inventory.devices.devicelist') + return flask.redirect(next_url) class LotCreateView(View):