reliable and unreliable device. Active and deactive devices and snapshots

This commit is contained in:
Cayo Puigdefabregas 2022-12-23 18:33:49 +01:00
parent 14ce3892ac
commit 50856671ed
5 changed files with 92 additions and 13 deletions

View File

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

View File

@ -38,10 +38,15 @@ class SnapshotsLog(Thing):
db.session.commit()
def get_status(self):
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 ''

View File

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

View File

@ -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
def merge(self):
self.user_trusts = True
self.reset_components(snapshot1)
return
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

View File

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