fix migration

This commit is contained in:
Cayo Puigdefabregas 2022-12-13 14:29:52 +01:00
parent 27910f2c20
commit da1feef746
2 changed files with 26 additions and 78 deletions

View File

@ -5,11 +5,11 @@ Revises: 564952310b17
Create Date: 2022-12-13 10:14:45.500087 Create Date: 2022-12-13 10:14:45.500087
""" """
from alembic import context, op
import sqlalchemy as sa
import citext
import hashlib import hashlib
import citext
import sqlalchemy as sa
from alembic import context, op
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
revision = '93daff872771' revision = '93daff872771'
@ -54,12 +54,7 @@ def upgrade():
upgrade_data() upgrade_data()
op.alter_column( op.alter_column('computer', 'user_trusts', nullable=False, schema=f'{get_inv()}')
'computer',
'user_trusts',
nullable=False,
schema=f'{get_inv()}'
)
def downgrade(): def downgrade():

View File

@ -183,6 +183,7 @@ class Device(Thing):
phid_bk = db.Column(db.CIText(), nullable=True, unique=False) phid_bk = db.Column(db.CIText(), nullable=True, unique=False)
active = db.Column(Boolean, default=True) active = db.Column(Boolean, default=True)
family = db.Column(db.CIText()) family = db.Column(db.CIText())
chid = db.Column(db.CIText())
_NON_PHYSICAL_PROPS = { _NON_PHYSICAL_PROPS = {
'id', 'id',
@ -212,6 +213,7 @@ class Device(Thing):
'active', 'active',
'phid_bk', 'phid_bk',
'dhid_bk', 'dhid_bk',
'chid',
} }
__table_args__ = ( __table_args__ = (
@ -745,76 +747,14 @@ class Device(Thing):
return "" return ""
def set_new_hid(self):
"""
The order is important
# product_vendor, is a manufacturer
# product_family, is a new field of lshw
# product_chassis, is a type of chassis
# product_number, is a model
# product_version,
# product_sku,
# product_serial, is a serial number
# product_uuid,
# board_vendor,
# board_number,
# board_serial,
# board_version
"""
with suppress(TypeError):
family = (self.family or '').replace(' ', '_')
model = ((self.model or '').replace(' ', '_'),)
chassis = self.type
if hasattr(self, 'chassis'):
chassis = self.chassis and self.chassis.name or ''
version = (self.version or '').replace(' ', '_')
sku = (self.sku or '').replace(' ', '_')
system_uuid = ''
if hasattr(self, 'system_uuid'):
system_uuid = str(self.system_uuid or '')
if not system_uuid or not self.manufacturer:
return ''
board = None
board_serial_number = ''
board_version = ''
board_manufacturer = ''
board_model = ''
if hasattr(self, 'components'):
for c in self.components:
if c.type == 'Motherboard':
board = c
break
if board:
board_manufacturer = (board.manufacturer or '').replace(' ', '_')
board_model = (board.model or '').replace(' ', '_')
board_serial_number = (board.serial_number or '').replace(' ', '_')
board_version = (board.version or '').replace(' ', '_')
self.hid = '-'.join(
[
(self.manufacturer or '').replace(' ', '_'),
family,
chassis,
model,
version,
sku,
self.serial_number,
system_uuid,
board_manufacturer,
board_model,
board_serial_number,
board_version,
]
).lower()
def get_hid(self):
if self.hid:
return hashlib.sha3_512(self.hid.encode()).hexdigest()
def get_from_db(self): def get_from_db(self):
try:
from modules.device.utils import get_from_db
return get_from_db(self)
except Exception:
pass
if not self.hid: if not self.hid:
return return
@ -826,11 +766,23 @@ class Device(Thing):
).first() ).first()
def set_hid(self): def set_hid(self):
try:
from modules.device.utils import set_hid
self.hid = set_hid(self)
return
except Exception:
pass
with suppress(TypeError): with suppress(TypeError):
self.hid = Naming.hid( self.hid = Naming.hid(
self.type, self.manufacturer, self.model, self.serial_number self.type, self.manufacturer, self.model, self.serial_number
) )
def set_chid(self):
if self.hid:
self.chid = hashlib.sha3_256(self.hid.encode()).hexdigest()
def last_action_of(self, *types): def last_action_of(self, *types):
"""Gets the last action of the given types. """Gets the last action of the given types.
@ -1107,6 +1059,7 @@ class Computer(Device):
receiver_id = db.Column(UUID(as_uuid=True), db.ForeignKey(User.id), nullable=True) receiver_id = db.Column(UUID(as_uuid=True), db.ForeignKey(User.id), nullable=True)
receiver = db.relationship(User, primaryjoin=receiver_id == User.id) receiver = db.relationship(User, primaryjoin=receiver_id == User.id)
system_uuid = db.Column(UUID(as_uuid=True), nullable=True) system_uuid = db.Column(UUID(as_uuid=True), nullable=True)
user_trusts = db.Column(Boolean(), default=True)
def __init__(self, *args, **kwargs) -> None: def __init__(self, *args, **kwargs) -> None:
if args: if args: