From 50856671ed6a384f01c0a64a53f3d4d7777c2c95 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 23 Dec 2022 18:33:49 +0100 Subject: [PATCH] reliable and unreliable device. Active and deactive devices and snapshots --- ereuse_devicehub/inventory/forms.py | 20 ++++-- ereuse_devicehub/parser/models.py | 12 +++- ereuse_devicehub/resources/action/models.py | 2 +- ereuse_devicehub/resources/device/models.py | 69 ++++++++++++++++++++- ereuse_devicehub/resources/device/sync.py | 2 +- 5 files changed, 92 insertions(+), 13 deletions(-) diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 655bffec..39321d8f 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -203,7 +203,9 @@ class FilterForm(FlaskForm): if filter_type: self.devices = self.devices.filter(Device.type.in_(filter_type)) - return self.devices.order_by(Device.updated.desc()) + return self.devices.filter(Device.active == True).order_by( + Device.updated.desc() + ) class LotForm(FlaskForm): @@ -1704,8 +1706,11 @@ class UserTrustsForm(FlaskForm): ) def __init__(self, snapshot_uuid, *args, **kwargs): - self.snapshot = Snapshot.query.filter_by(uuid=snapshot_uuid).first() - self.device = self.snapshot.device if self.snapshot.device else None + self.snapshot = Snapshot.query.filter_by(uuid=snapshot_uuid).one() + self.device = None + if self.snapshot.device: + self.device = self.snapshot.device + self.snapshot_type.kwargs['default'] = self.snapshot.get_new_device() super().__init__(*args, **kwargs) @@ -1725,7 +1730,7 @@ class UserTrustsForm(FlaskForm): except Exception: self._unic = ( Device.query.filter_by( - hid=self.device.hid, owner=g.user, placeholder=None + hid=self.device.hid, owner=g.user, placeholder=None, active=True ).count() < 2 ) @@ -1736,6 +1741,9 @@ class UserTrustsForm(FlaskForm): if not self.snapshot or not self.device: return False + if not self.snapshot.active: + return False + if not hasattr(self.device, 'system_uuid'): return False @@ -1761,10 +1769,10 @@ class UserTrustsForm(FlaskForm): return if self.snapshot_type.data == 'update' and not self.unic(): - self.device.merge() + self.device.reliable() if self.snapshot_type.data == 'new_device' and self.unic(): - self.device.split() + self.device.unreliable() if commit: db.session.commit() diff --git a/ereuse_devicehub/parser/models.py b/ereuse_devicehub/parser/models.py index 8caa0620..6389b12b 100644 --- a/ereuse_devicehub/parser/models.py +++ b/ereuse_devicehub/parser/models.py @@ -38,10 +38,15 @@ class SnapshotsLog(Thing): db.session.commit() def get_status(self): - return Severity(self.severity) + if self.snapshot: + if not self.snapshot.active: + return Severity(2) + return Severity(self.severity) + + return '' def get_device(self): - if self.snapshot: + if self.snapshot and self.snapshot.active: if self.snapshot.device.binding: return self.snapshot.device.binding.device.devicehub_id return self.snapshot.device.devicehub_id @@ -67,6 +72,9 @@ class SnapshotsLog(Thing): if not self.snapshot: return '' + if not self.snapshot.active: + return '' + if not self.snapshot.device: return '' diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index 5f141570..b0ec76c6 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -678,7 +678,7 @@ class Snapshot(JoinedWithOneDeviceMixin, ActionWithOneDevice): sid = Column(CIText(), nullable=True) settings_version = Column(CIText(), nullable=True) is_server_erase = Column(Boolean(), nullable=True) - active = Column(Boolean(), nullable=True) + active = Column(Boolean(), default=True, nullable=False) def get_last_lifetimes(self): """We get the lifetime and serial_number of the first disk""" diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index 735b68a0..bc406feb 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -877,14 +877,77 @@ class Device(Thing): } return types.get(self.type, '') - def split(self): + def unreliable(self): self.user_trusts = False + i = 0 + snapshot1 = None + + for ac in self.actions: + if ac.type == 'Snapshot': + if i == 0: + snapshot1 = ac + if i > 0: + ac.active = False + i += 1 + + if not snapshot1: + return + + self.reset_components(snapshot1) + return - def merge(self): - self.user_trusts = True + def reliable(self): + # self.user_trusts = True + computers = Computer.query.filter_by( + hid=self.hid, + owner_id=g.user.id, + active=True, + placeholder=None, + ).order_by(Device.created.asc()) + + i = 0 + computer1 = None + for d in computers: + if i == 0: + d.user_trusts = True + computer1 = d + i += 1 + continue + + d.user_trusts = True + d.active = False + d.binding.device.active = False + for ac in d.actions: + if ac.type == 'Snapshot': + ac.active = False + + for c in d.components: + c.parent = None + + if not computer1: + return + + snapshot1 = None + for ac in computer1.actions_one: + if ac.type == 'Snapshot': + snapshot1 = ac + break + + if not snapshot1: + return + + self.reset_components(snapshot1) + return + def reset_components(self, snapshot): + for c in snapshot.components: + if c.parent is None: + c.parent = snapshot.device + + snapshot.device.components = snapshot.components + def __lt__(self, other): return self.id < other.id diff --git a/ereuse_devicehub/resources/device/sync.py b/ereuse_devicehub/resources/device/sync.py index a82be1b1..8abe0550 100644 --- a/ereuse_devicehub/resources/device/sync.py +++ b/ereuse_devicehub/resources/device/sync.py @@ -125,7 +125,7 @@ class Sync: if component.hid: db_component = Device.query.filter_by( - hid=component.hid, owner_id=g.user.id, placeholder=None + hid=component.hid, owner_id=g.user.id, placeholder=None, active=True ).first() is_new = False if not db_component: