validate errors of allocate in scheme instead of signal

This commit is contained in:
Cayo Puigdefabregas 2020-11-22 18:47:58 +01:00
parent 0e53919607
commit ee70288432
3 changed files with 42 additions and 31 deletions

View file

@ -1530,36 +1530,6 @@ def update_parent(target: Union[EraseBasic, Test, Install], device: Device, _, _
target.parent = device.parent
@event.listens_for(Allocate.devices, 'append')
def update_allocated(target: Allocate, device, initiatort):
"""Mark one device as allocated."""
# for device in target.devices:
actions = [a for a in device.actions]
actions.sort(key=lambda x: x.created)
actions.reverse()
allocate = None
#import pdb; pdb.set_trace()
for a in actions:
if isinstance(a, Allocate):
allocate = a
break
if isinstance(a, Deallocate):
break
if allocate:
txt = "You need deallocate before allocate this device again"
same_allocate = [
allocate.code == target.code,
allocate.start_time == target.start_time,
allocate.end_users == target.end_users
]
assert all(same_allocate), txt
import pdb; pdb.set_trace()
target.allocated = True
class InvalidRangeForPrice(ValueError):
pass

View file

@ -75,10 +75,47 @@ class Allocate(ActionWithMultipleDevices):
end_users = Integer(validate=[Range(min=1, error="Value must be greater than 0")],
required=True)
@validates_schema
def validate_allocate(self, data: dict):
for device in data['devices']:
actions = [a for a in device.actions]
actions.sort(key=lambda x: x.created)
actions.reverse()
allocate = None
for a in actions:
if isinstance(a, m.Allocate):
allocate = a
break
if isinstance(a, m.Deallocate):
break
if allocate:
txt = "You need deallocate before allocate this device again"
same_allocate = [
allocate.code == data['code'],
allocate.start_time == data['start_time'],
allocate.end_users == data['end_users']
]
if not all(same_allocate):
raise ValidationError(txt)
device.allocated = True
class Deallocate(ActionWithMultipleDevices):
__doc__ = m.Deallocate.__doc__
@validates_schema
def validate_deallocate(self, data: dict):
txt = "Sorry some of this devices are actually deallocate"
for device in data['devices']:
if hasattr(device, 'allocated') and device.allocated:
device.allocated = False
else:
raise ValidationError(txt)
class EraseBasic(ActionWithOneDevice):
__doc__ = m.EraseBasic.__doc__

View file

@ -106,6 +106,9 @@ class Device(Thing):
image = db.Column(db.URL)
image.comment = "An image of the device."
allocated = db.Column(Boolean, default=False)
allocated.comment = "An image of the device."
_NON_PHYSICAL_PROPS = {
'id',
'type',
@ -125,7 +128,8 @@ class Device(Thing):
'variant',
'version',
'sku',
'image'
'image',
'allocated'
}
__table_args__ = (