deactivate devices

This commit is contained in:
Cayo Puigdefabregas 2021-10-05 12:17:07 +02:00
parent ec2d286ed3
commit 6dd54d9645
4 changed files with 13 additions and 5 deletions

View File

@ -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__

View File

@ -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__ = (

View File

@ -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)

View File

@ -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]