precommit

This commit is contained in:
Cayo Puigdefabregas 2022-03-10 14:09:17 +01:00
parent 3f7fd1b6ec
commit 07d0ab4171
3 changed files with 34 additions and 10 deletions

View File

@ -137,15 +137,34 @@ class FilterForm(FlaskForm):
class LotDeviceForm(FlaskForm): class LotDeviceForm(FlaskForm):
devices = StringField('Devices', [validators.length(min=1)]) devices = StringField(
lot = SelectField('Lot', choices=[]) 'Devices', [validators.length(min=1)], render_kw={'class': "d-none"}
)
lot = SelectField('Lot', choices=[], render_kw={'class': "form-select"})
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.action = kwargs.pop('action', None)
self._devices = kwargs.pop('_devices', None)
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
# TODO # import pdb; pdb.set_trace()
self.lot.choices = [ self._lots = Lot.query.filter(Lot.owner_id == g.user.id)
(lot.id, lot.name) for lot in Lot.query.filter(Lot.owner_id == g.user.id) lots = []
] if self._devices:
lots = [set(dev.lots) for dev in self._devices]
if self.action == 'remove' and lots:
x = lots[0]
common_lots = x.intersection(*lots[1:])
self.lot.choices = [
(lot.id, lot.name) for lot in self._lots if lot in common_lots
]
elif self.action == 'add' and lots:
x = lots[0]
self.lot.choices = [
(lot.id, lot.name) for lot in self._lots if lot not in lots
]
else:
self.lot.choices = [(lot.id, lot.name) for lot in self._lots]
def validate(self, extra_validators=None): def validate(self, extra_validators=None):
is_valid = super().validate(extra_validators) is_valid = super().validate(extra_validators)
@ -1056,7 +1075,7 @@ class TradeDocumentForm(FlaskForm):
class LotDeviceShowForm(FlaskForm): class LotDeviceShowForm(FlaskForm):
devices = StringField(render_kw={'class': "devicesList"}) devices = StringField(render_kw={'class': "devicesList d-none"})
def validate(self, extra_validators=None): def validate(self, extra_validators=None):
is_valid = super().validate(extra_validators) is_valid = super().validate(extra_validators)

View File

@ -193,7 +193,9 @@ class LotDeviceDeleteShowView(GenericMixView):
return flask.redirect(next_url) return flask.redirect(next_url)
lots = self.get_lots() lots = self.get_lots()
form_lot = LotDeviceForm(devices=form.devices) form_lot = LotDeviceForm(
devices=form.devices, action='remove', _devices=form._devices
)
context = {'form': form_lot, 'title': self.title, 'lots': lots} context = {'form': form_lot, 'title': self.title, 'lots': lots}
return flask.render_template(self.template_name, **context) return flask.render_template(self.template_name, **context)
@ -214,7 +216,7 @@ class LotDeviceDeleteView(View):
else: else:
messages.error('Error removing devices from lot!') messages.error('Error removing devices from lot!')
next_url = request.referrer or url_for('inventory.devices.devicelist') next_url = url_for('inventory.devices.lotdevicelist', lot_id=form._lot.id)
return flask.redirect(next_url) return flask.redirect(next_url)

View File

@ -97,7 +97,10 @@
{% for f in form_lot_device_del %} {% for f in form_lot_device_del %}
{{ f }} {{ f }}
{% endfor %} {% endfor %}
<input type="submit" value="Remove selected devices from a lot" /> <a href="javascript:void()" onclick="document.review.submit()" class="dropdown-item">
<i class="bi bi-x"></i>
Remove selected devices from a lot
</a>
</form> </form>
</li> </li>
</ul> </ul>