From ee702884329e0ad73fd32c80a58e4d25a8367774 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Sun, 22 Nov 2020 18:47:58 +0100 Subject: [PATCH] validate errors of allocate in scheme instead of signal --- ereuse_devicehub/resources/action/models.py | 30 ---------------- ereuse_devicehub/resources/action/schemas.py | 37 ++++++++++++++++++++ ereuse_devicehub/resources/device/models.py | 6 +++- 3 files changed, 42 insertions(+), 31 deletions(-) diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index b856f221..4b14fda9 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -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 diff --git a/ereuse_devicehub/resources/action/schemas.py b/ereuse_devicehub/resources/action/schemas.py index 0ac2586e..f39b5e6f 100644 --- a/ereuse_devicehub/resources/action/schemas.py +++ b/ereuse_devicehub/resources/action/schemas.py @@ -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__ diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index 399bab92..54f30324 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -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__ = (