From 6dd54d964525078402e335a4106e61111dab687a Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 5 Oct 2021 12:17:07 +0200 Subject: [PATCH] deactivate devices --- ereuse_devicehub/resources/action/schemas.py | 5 +++++ ereuse_devicehub/resources/device/models.py | 4 +++- ereuse_devicehub/resources/device/views.py | 7 ++++--- tests/test_action.py | 2 +- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/ereuse_devicehub/resources/action/schemas.py b/ereuse_devicehub/resources/action/schemas.py index 8671b5a8..3d8070a9 100644 --- a/ereuse_devicehub/resources/action/schemas.py +++ b/ereuse_devicehub/resources/action/schemas.py @@ -833,6 +833,11 @@ class TransferOwnershipBlockchain(Trade): class Delete(ActionWithMultipleDevices): __doc__ = m.Delete.__doc__ + @post_load + def deactivate_device(self, data): + for dev in data['devices']: + dev.active = False + class Migrate(ActionWithMultipleDevices): __doc__ = m.Migrate.__doc__ diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index a0917bef..fd04bfa5 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -126,6 +126,7 @@ class Device(Thing): allocated.comment = "device is allocated or not." devicehub_id = db.Column(db.CIText(), nullable=True, unique=True, default=create_code) devicehub_id.comment = "device have a unique code." + active = db.Column(Boolean, default=True) _NON_PHYSICAL_PROPS = { 'id', @@ -149,7 +150,8 @@ class Device(Thing): 'sku', 'image', 'allocated', - 'devicehub_id' + 'devicehub_id', + 'active' } __table_args__ = ( diff --git a/ereuse_devicehub/resources/device/views.py b/ereuse_devicehub/resources/device/views.py index b046b49d..52805c77 100644 --- a/ereuse_devicehub/resources/device/views.py +++ b/ereuse_devicehub/resources/device/views.py @@ -70,6 +70,7 @@ class Filters(query.Query): # due to having multiple paths to the same lot = query.Join((Device.id == LotDeviceDescendants.device_id), LotQ) + active = True class Sorting(query.Sort): @@ -104,7 +105,7 @@ class DeviceView(View): return super().get(id) def patch(self, id): - dev = Device.query.filter_by(id=id, owner_id=g.user.id).one() + dev = Device.query.filter_by(id=id, owner_id=g.user.id, active=True).one() if isinstance(dev, Computer): resource_def = app.resources['Computer'] # TODO check how to handle the 'actions_one' @@ -129,12 +130,12 @@ class DeviceView(View): return self.one_private(id) def one_public(self, id: int): - device = Device.query.filter_by(devicehub_id=id).one() + device = Device.query.filter_by(devicehub_id=id, active=True).one() return render_template('devices/layout.html', device=device, states=states) @auth.Auth.requires_auth def one_private(self, id: str): - device = Device.query.filter_by(devicehub_id=id, owner_id=g.user.id).first() + device = Device.query.filter_by(devicehub_id=id, owner_id=g.user.id, active=True).first() if not device: return self.one_public(id) return self.schema.jsonify(device) diff --git a/tests/test_action.py b/tests/test_action.py index 5173a605..ecd4cfed 100644 --- a/tests/test_action.py +++ b/tests/test_action.py @@ -2512,7 +2512,7 @@ def test_delete_devices(user: UserClient): action, _ = user.post(res=models.Action, data=request) - user.get(res=Device, item=snap['device']['devicehubID']) + user.get(res=Device, item=snap['device']['devicehubID'], status=404) db_device = Device.query.filter_by(id=snap['device']['id']).one() action_delete = sorted(db_device.actions, key=lambda x: x.created)[-1]