fix bug in validation actions

This commit is contained in:
Cayo Puigdefabregas 2022-04-21 14:01:33 +02:00
parent fd74804f35
commit e37fa49c3e
1 changed files with 39 additions and 14 deletions

View File

@ -497,7 +497,7 @@ class TagDeviceForm(FlaskForm):
db.session.commit() db.session.commit()
class NewActionForm(FlaskForm): class ActionFormMix(FlaskForm):
name = StringField( name = StringField(
'Name', 'Name',
[validators.length(max=50)], [validators.length(max=50)],
@ -529,8 +529,14 @@ class NewActionForm(FlaskForm):
if not is_valid: if not is_valid:
return False return False
if self.type.data in [None, '']:
return False
if not self.devices.data:
return False
self._devices = OrderedSet() self._devices = OrderedSet()
if self.devices.data:
devices = set(self.devices.data.split(",")) devices = set(self.devices.data.split(","))
self._devices = OrderedSet( self._devices = OrderedSet(
Device.query.filter(Device.id.in_(devices)) Device.query.filter(Device.id.in_(devices))
@ -572,7 +578,20 @@ class NewActionForm(FlaskForm):
return self.type.data return self.type.data
class AllocateForm(NewActionForm): class NewActionForm(ActionFormMix):
def validate(self, extra_validators=None):
is_valid = super().validate(extra_validators)
if not is_valid:
return False
if self.type.data in ['Allocate', 'Deallocate', 'Trade', 'DataWipe']:
return False
return True
class AllocateForm(ActionFormMix):
start_time = DateField('Start time') start_time = DateField('Start time')
end_time = DateField('End time') end_time = DateField('End time')
final_user_code = StringField('Final user code', [validators.length(max=50)]) final_user_code = StringField('Final user code', [validators.length(max=50)])
@ -582,6 +601,9 @@ class AllocateForm(NewActionForm):
def validate(self, extra_validators=None): def validate(self, extra_validators=None):
is_valid = super().validate(extra_validators) is_valid = super().validate(extra_validators)
if self.type.data not in ['Allocate', 'Deallocate']:
return False
start_time = self.start_time.data start_time = self.start_time.data
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:
@ -650,7 +672,7 @@ class DataWipeDocumentForm(Form):
return self._obj return self._obj
class DataWipeForm(NewActionForm): class DataWipeForm(ActionFormMix):
document = FormField(DataWipeDocumentForm) document = FormField(DataWipeDocumentForm)
def save(self): def save(self):
@ -677,7 +699,7 @@ class DataWipeForm(NewActionForm):
return self.instance return self.instance
class TradeForm(NewActionForm): class TradeForm(ActionFormMix):
user_from = StringField( user_from = StringField(
'Supplier', 'Supplier',
[validators.Optional()], [validators.Optional()],
@ -724,6 +746,9 @@ class TradeForm(NewActionForm):
email_from = self.user_from.data email_from = self.user_from.data
email_to = self.user_to.data email_to = self.user_to.data
if self.type.data != "Trade":
return False
if not self.confirm.data and not self.code.data: if not self.confirm.data and not self.code.data:
self.code.errors = ["If you don't want to confirm, you need a code"] self.code.errors = ["If you don't want to confirm, you need a code"]
is_valid = False is_valid = False