From e1c46e4725fb0e8acc79341ad79a97fb94e0bf0a Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 30 Dec 2021 13:52:06 +0100 Subject: [PATCH] adding form class for addinglot --- ereuse_devicehub/inventory/forms.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 ereuse_devicehub/inventory/forms.py diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py new file mode 100644 index 00000000..6ababca8 --- /dev/null +++ b/ereuse_devicehub/inventory/forms.py @@ -0,0 +1,29 @@ +from flask_wtf import FlaskForm +from wtforms import StringField, validators +from flask_login import current_user + +from ereuse_devicehub.resources.device.models import Device +from ereuse_devicehub.resources.lot.models import Lot + + +class LotDeviceAddForm(FlaskForm): + lot = StringField(u'Lot', [validators.UUID()]) + devices = StringField(u'Devices', [validators.length(min=1)]) + + def validate(self, extra_validators=None): + is_valid = super().validate(extra_validators) + + if not is_valid: + return False + + self.lot = Lot.query.filter(Lot.id == self.lot.data).filter( + Lot.owner_id == current_user.id).one() + + devices = set(self.devices.data.split(",")) + self.devices = set(Device.query.filter(Device.id.in_(devices)).filter( + Device.owner_id == current_user.id).all()) + + if not self.devices: + return False + + return True