validate errors of allocate in scheme instead of signal
This commit is contained in:
parent
0e53919607
commit
ee70288432
|
@ -1530,36 +1530,6 @@ def update_parent(target: Union[EraseBasic, Test, Install], device: Device, _, _
|
||||||
target.parent = device.parent
|
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):
|
class InvalidRangeForPrice(ValueError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
|
@ -75,10 +75,47 @@ class Allocate(ActionWithMultipleDevices):
|
||||||
end_users = Integer(validate=[Range(min=1, error="Value must be greater than 0")],
|
end_users = Integer(validate=[Range(min=1, error="Value must be greater than 0")],
|
||||||
required=True)
|
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):
|
class Deallocate(ActionWithMultipleDevices):
|
||||||
__doc__ = m.Deallocate.__doc__
|
__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):
|
class EraseBasic(ActionWithOneDevice):
|
||||||
__doc__ = m.EraseBasic.__doc__
|
__doc__ = m.EraseBasic.__doc__
|
||||||
|
|
|
@ -106,6 +106,9 @@ class Device(Thing):
|
||||||
image = db.Column(db.URL)
|
image = db.Column(db.URL)
|
||||||
image.comment = "An image of the device."
|
image.comment = "An image of the device."
|
||||||
|
|
||||||
|
allocated = db.Column(Boolean, default=False)
|
||||||
|
allocated.comment = "An image of the device."
|
||||||
|
|
||||||
_NON_PHYSICAL_PROPS = {
|
_NON_PHYSICAL_PROPS = {
|
||||||
'id',
|
'id',
|
||||||
'type',
|
'type',
|
||||||
|
@ -125,7 +128,8 @@ class Device(Thing):
|
||||||
'variant',
|
'variant',
|
||||||
'version',
|
'version',
|
||||||
'sku',
|
'sku',
|
||||||
'image'
|
'image',
|
||||||
|
'allocated'
|
||||||
}
|
}
|
||||||
|
|
||||||
__table_args__ = (
|
__table_args__ = (
|
||||||
|
|
Reference in a new issue