2022-04-04 11:27:04 +00:00
|
|
|
from citext import CIText
|
2022-04-12 08:24:32 +00:00
|
|
|
from flask import g
|
2022-04-04 11:27:04 +00:00
|
|
|
from sqlalchemy import BigInteger, Column, Sequence, SmallInteger
|
|
|
|
from sqlalchemy.dialects.postgresql import UUID
|
2022-08-05 14:42:07 +00:00
|
|
|
from sqlalchemy.orm import backref
|
2022-04-04 11:27:04 +00:00
|
|
|
|
|
|
|
from ereuse_devicehub.db import db
|
2022-05-18 09:00:04 +00:00
|
|
|
from ereuse_devicehub.resources.action.models import Snapshot
|
2022-07-06 16:37:27 +00:00
|
|
|
from ereuse_devicehub.resources.device.models import Placeholder
|
2022-04-04 11:27:04 +00:00
|
|
|
from ereuse_devicehub.resources.enums import Severity
|
|
|
|
from ereuse_devicehub.resources.models import Thing
|
2022-04-12 08:24:32 +00:00
|
|
|
from ereuse_devicehub.resources.user.models import User
|
2022-04-04 11:27:04 +00:00
|
|
|
|
|
|
|
|
2022-05-18 09:00:04 +00:00
|
|
|
class SnapshotsLog(Thing):
|
2022-05-18 15:59:05 +00:00
|
|
|
"""A Snapshot log."""
|
2022-04-04 11:27:04 +00:00
|
|
|
|
2022-05-18 09:00:04 +00:00
|
|
|
id = Column(BigInteger, Sequence('snapshots_log_seq'), primary_key=True)
|
2022-04-04 11:27:04 +00:00
|
|
|
severity = Column(SmallInteger, default=Severity.Info, nullable=False)
|
2022-05-18 10:46:57 +00:00
|
|
|
version = Column(CIText(), default='', nullable=True)
|
2022-05-18 09:00:04 +00:00
|
|
|
description = Column(CIText(), default='', nullable=True)
|
|
|
|
sid = Column(CIText(), nullable=True)
|
2022-05-18 15:59:05 +00:00
|
|
|
snapshot_uuid = Column(UUID(as_uuid=True), nullable=True)
|
|
|
|
snapshot_id = Column(UUID(as_uuid=True), db.ForeignKey(Snapshot.id), nullable=True)
|
2022-04-12 08:24:32 +00:00
|
|
|
owner_id = db.Column(
|
|
|
|
UUID(as_uuid=True),
|
|
|
|
db.ForeignKey(User.id),
|
|
|
|
nullable=False,
|
|
|
|
default=lambda: g.user.id,
|
|
|
|
)
|
2022-05-18 15:59:05 +00:00
|
|
|
snapshot = db.relationship(Snapshot, primaryjoin=snapshot_id == Snapshot.id)
|
2022-04-12 08:24:32 +00:00
|
|
|
owner = db.relationship(User, primaryjoin=owner_id == User.id)
|
2022-04-04 11:27:04 +00:00
|
|
|
|
|
|
|
def save(self, commit=False):
|
|
|
|
db.session.add(self)
|
|
|
|
|
|
|
|
if commit:
|
|
|
|
db.session.commit()
|
2022-05-18 16:26:01 +00:00
|
|
|
|
|
|
|
def get_status(self):
|
2022-05-20 11:52:32 +00:00
|
|
|
return Severity(self.severity)
|
2022-05-19 16:16:47 +00:00
|
|
|
|
|
|
|
def get_device(self):
|
|
|
|
if self.snapshot:
|
2022-08-05 14:52:56 +00:00
|
|
|
if self.snapshot.device.binding:
|
|
|
|
return self.snapshot.device.binding.device.devicehub_id
|
2022-05-19 16:16:47 +00:00
|
|
|
return self.snapshot.device.devicehub_id
|
|
|
|
|
|
|
|
return ''
|
2022-07-06 16:37:27 +00:00
|
|
|
|
2022-09-22 17:14:11 +00:00
|
|
|
def get_original_dhid(self):
|
|
|
|
if self.snapshot:
|
|
|
|
dev = self.snapshot.device
|
|
|
|
if dev.dhid_bk:
|
|
|
|
return dev.dhid_bk
|
|
|
|
|
|
|
|
return self.get_device()
|
|
|
|
|
|
|
|
def get_type_device(self):
|
|
|
|
if self.snapshot:
|
|
|
|
if self.snapshot.device.binding:
|
|
|
|
return self.snapshot.device.binding.status
|
|
|
|
|
|
|
|
return ''
|
|
|
|
|
|
|
|
def get_new_device(self):
|
|
|
|
if not self.snapshot:
|
|
|
|
return ''
|
|
|
|
|
|
|
|
if not self.snapshot.device:
|
|
|
|
return ''
|
|
|
|
|
|
|
|
snapshots = []
|
|
|
|
for s in self.snapshot.device.actions:
|
|
|
|
if s == self.snapshot:
|
2022-11-29 17:09:53 +00:00
|
|
|
break
|
2022-09-22 17:14:11 +00:00
|
|
|
if s.type == self.snapshot.type:
|
|
|
|
snapshots.append(s)
|
|
|
|
return snapshots and 'Update' or 'New Device'
|
|
|
|
|
2022-11-28 10:58:42 +00:00
|
|
|
def get_system_uuid(self):
|
|
|
|
try:
|
|
|
|
return self.snapshot.device.system_uuid or ''
|
|
|
|
except AttributeError:
|
|
|
|
return ''
|
|
|
|
|
2022-11-30 15:46:10 +00:00
|
|
|
def get_version(self):
|
|
|
|
if not self.snapshot:
|
|
|
|
return self.version
|
|
|
|
settings_version = self.snapshot.settings_version or ''
|
|
|
|
settings_version = "".join([x[0] for x in settings_version.split(' ') if x])
|
|
|
|
|
|
|
|
return "{} ({})".format(self.version, settings_version)
|
|
|
|
|
2022-07-06 16:37:27 +00:00
|
|
|
|
|
|
|
class PlaceholdersLog(Thing):
|
|
|
|
"""A Placeholder log."""
|
|
|
|
|
2022-07-07 14:18:51 +00:00
|
|
|
id = Column(BigInteger, Sequence('placeholders_log_seq'), primary_key=True)
|
2022-07-06 16:37:27 +00:00
|
|
|
source = Column(CIText(), default='', nullable=True)
|
|
|
|
type = Column(CIText(), default='', nullable=True)
|
2022-07-07 11:10:05 +00:00
|
|
|
severity = Column(SmallInteger, default=Severity.Info, nullable=False)
|
2022-07-06 16:37:27 +00:00
|
|
|
|
2022-07-07 11:10:05 +00:00
|
|
|
placeholder_id = Column(BigInteger, db.ForeignKey(Placeholder.id), nullable=True)
|
2022-07-06 16:37:27 +00:00
|
|
|
placeholder = db.relationship(
|
2022-08-05 14:42:07 +00:00
|
|
|
Placeholder,
|
|
|
|
backref=backref(
|
|
|
|
'placeholder_logs', lazy=True, cascade="all, delete-orphan", uselist=True
|
|
|
|
),
|
|
|
|
primaryjoin=placeholder_id == Placeholder.id,
|
2022-07-06 16:37:27 +00:00
|
|
|
)
|
|
|
|
owner_id = db.Column(
|
|
|
|
UUID(as_uuid=True),
|
|
|
|
db.ForeignKey(User.id),
|
|
|
|
nullable=False,
|
|
|
|
default=lambda: g.user.id,
|
|
|
|
)
|
|
|
|
owner = db.relationship(User, primaryjoin=owner_id == User.id)
|
|
|
|
|
|
|
|
def save(self, commit=False):
|
|
|
|
db.session.add(self)
|
|
|
|
|
|
|
|
if commit:
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def phid(self):
|
|
|
|
if self.placeholder:
|
|
|
|
return self.placeholder.phid
|
|
|
|
|
|
|
|
return ''
|
|
|
|
|
|
|
|
@property
|
|
|
|
def dhid(self):
|
|
|
|
if self.placeholder:
|
2022-07-07 11:10:05 +00:00
|
|
|
return self.placeholder.device.devicehub_id
|
2022-07-06 16:37:27 +00:00
|
|
|
|
|
|
|
return ''
|
2022-07-07 11:10:05 +00:00
|
|
|
|
|
|
|
def get_status(self):
|
|
|
|
return Severity(self.severity)
|