add checks of device.allocated

This commit is contained in:
Cayo Puigdefabregas 2022-05-05 11:31:13 +02:00
parent ce32db6d37
commit 9eda5c0466

View file

@ -593,10 +593,14 @@ class NewActionForm(ActionFormMix):
class AllocateForm(ActionFormMix): class AllocateForm(ActionFormMix):
start_time = DateField('Start time') start_time = DateField('Start time')
end_time = DateField('End time') end_time = DateField('End time', [validators.Optional()])
final_user_code = StringField('Final user code', [validators.length(max=50)]) final_user_code = StringField(
transaction = StringField('Transaction', [validators.length(max=50)]) 'Final user code', [validators.Optional(), validators.length(max=50)]
end_users = IntegerField('End users') )
transaction = StringField(
'Transaction', [validators.Optional(), validators.length(max=50)]
)
end_users = IntegerField('End users', [validators.Optional()])
def validate(self, extra_validators=None): def validate(self, extra_validators=None):
is_valid = super().validate(extra_validators) is_valid = super().validate(extra_validators)
@ -608,13 +612,29 @@ class AllocateForm(ActionFormMix):
end_time = self.end_time.data end_time = self.end_time.data
if start_time and end_time and end_time < start_time: if start_time and end_time and end_time < start_time:
error = ['The action cannot finish before it starts.'] error = ['The action cannot finish before it starts.']
self.start_time.errors = error
self.end_time.errors = error self.end_time.errors = error
is_valid = False is_valid = False
if not self.end_users.data: if is_valid and not end_time:
self.end_users.errors = ["You need to specify a number of users"] self.end_time.data = self.start_time.data
is_valid = False
if self.type.data == 'Allocate':
txt = "You need deallocate before allocate this device again"
for device in self._devices:
if device.allocated:
self.devices.errors = [txt]
return False
device.allocated = True
if self.type.data == 'Deallocate':
txt = "Sorry some of this devices are actually deallocate"
for device in self._devices:
if not device.allocated:
self.devices.errors = [txt]
return False
device.allocated = False
return is_valid return is_valid