add chid_dpp and phid_dpp in models

This commit is contained in:
Cayo Puigdefabregas 2023-01-19 13:33:32 +01:00
parent 8ed1e2296d
commit 9053c89f47
3 changed files with 144 additions and 47 deletions

View File

@ -5,12 +5,11 @@ Revises: 4b7f77f121bf
Create Date: 2023-01-19 12:01:54.102326
"""
from alembic import op, context
import sqlalchemy as sa
import citext
import sqlalchemy as sa
from alembic import context, op
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision = '8334535d56fa'
down_revision = '4b7f77f121bf'
@ -26,53 +25,107 @@ def get_inv():
def upgrade():
op.create_table('proof',
sa.Column('updated', sa.TIMESTAMP(timezone=True), server_default=sa.text('CURRENT_TIMESTAMP'),
nullable=False,
comment='The last time Devicehub recorded a change for \n this thing.\n '),
sa.Column('created', sa.TIMESTAMP(timezone=True), server_default=sa.text('CURRENT_TIMESTAMP'),
nullable=False, comment='When Devicehub created this.'),
sa.Column('id', sa.BigInteger(), nullable=False),
sa.Column('type', sa.Unicode(), nullable=False),
sa.Column('documentId', citext.CIText(), nullable=True),
sa.Column('documentSignature', citext.CIText(), nullable=True),
sa.Column('timestamp', sa.BigInteger(), nullable=False),
sa.Column('device_id', sa.BigInteger(), nullable=False),
sa.Column('snapshot_id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('issuer_id', postgresql.UUID(as_uuid=True), nullable=False),
sa.ForeignKeyConstraint(['snapshot_id'], [f'{get_inv()}.snapshot.id'], ),
sa.ForeignKeyConstraint(['device_id'], [f'{get_inv()}.device.id'], ),
sa.ForeignKeyConstraint(['issuer_id'], [f'common.user.id'], ),
sa.PrimaryKeyConstraint('id'),
schema=f'{get_inv()}'
)
op.create_table(
'proof',
sa.Column(
'updated',
sa.TIMESTAMP(timezone=True),
server_default=sa.text('CURRENT_TIMESTAMP'),
nullable=False,
comment='The last time Devicehub recorded a change for \n this thing.\n ',
),
sa.Column(
'created',
sa.TIMESTAMP(timezone=True),
server_default=sa.text('CURRENT_TIMESTAMP'),
nullable=False,
comment='When Devicehub created this.',
),
sa.Column('id', sa.BigInteger(), nullable=False),
sa.Column('type', sa.Unicode(), nullable=False),
sa.Column('documentId', citext.CIText(), nullable=True),
sa.Column('documentSignature', citext.CIText(), nullable=True),
sa.Column('timestamp', sa.BigInteger(), nullable=False),
sa.Column('device_id', sa.BigInteger(), nullable=False),
sa.Column('snapshot_id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('issuer_id', postgresql.UUID(as_uuid=True), nullable=False),
sa.ForeignKeyConstraint(
['snapshot_id'],
[f'{get_inv()}.snapshot.id'],
),
sa.ForeignKeyConstraint(
['device_id'],
[f'{get_inv()}.device.id'],
),
sa.ForeignKeyConstraint(
['issuer_id'],
[f'common.user.id'],
),
sa.PrimaryKeyConstraint('id'),
schema=f'{get_inv()}',
)
# op.create_index(op.f('ix_proof_created'), 'proof', ['created'], unique=False, schema=f'{get_inv()}')
# op.create_index(op.f('ix_proof_timestamp'), 'proof', ['timestamp'], unique=False, schema=f'{get_inv()}')
op.add_column('device', sa.Column('chid_dpp', citext.CIText(), nullable=True), schema=f'{get_inv()}')
op.add_column('snapshot', sa.Column('phid_dpp', citext.CIText(), nullable=True), schema=f'{get_inv()}')
op.add_column('snapshot', sa.Column('json_wb', citext.CIText(), nullable=True), schema=f'{get_inv()}')
op.add_column('snapshot', sa.Column('json_hw', citext.CIText(), nullable=True), schema=f'{get_inv()}')
op.add_column(
'device',
sa.Column('chid_dpp', citext.CIText(), nullable=True),
schema=f'{get_inv()}',
)
op.add_column(
'snapshot',
sa.Column('phid_dpp', citext.CIText(), nullable=True),
schema=f'{get_inv()}',
)
op.add_column(
'snapshot',
sa.Column('json_wb', citext.CIText(), nullable=True),
schema=f'{get_inv()}',
)
op.add_column(
'snapshot',
sa.Column('json_hw', citext.CIText(), nullable=True),
schema=f'{get_inv()}',
)
op.create_table('dpp',
sa.Column('updated', sa.TIMESTAMP(timezone=True), server_default=sa.text('CURRENT_TIMESTAMP'),
nullable=False,
comment='The last time Devicehub recorded a change for \n this thing.\n '),
sa.Column('created', sa.TIMESTAMP(timezone=True), server_default=sa.text('CURRENT_TIMESTAMP'),
nullable=False, comment='When Devicehub created this.'),
sa.Column('id', sa.BigInteger(), nullable=False),
sa.Column('documentId', citext.CIText(), nullable=True),
sa.Column('documentSignature', citext.CIText(), nullable=True),
sa.Column('timestamp', sa.BigInteger(), nullable=False),
sa.Column('device_id', sa.BigInteger(), nullable=False),
sa.Column('snapshot_id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('issuer_id', postgresql.UUID(as_uuid=True), nullable=False),
sa.ForeignKeyConstraint(['snapshot_id'], [f'{get_inv()}.snapshot.id'], ),
sa.ForeignKeyConstraint(['device_id'], [f'{get_inv()}.device.id'], ),
sa.ForeignKeyConstraint(['issuer_id'], [f'common.user.id'], ),
sa.Column('key', sa.Unicode(), nullable=False),
sa.PrimaryKeyConstraint('id'),
schema=f'{get_inv()}'
)
op.create_table(
'dpp',
sa.Column(
'updated',
sa.TIMESTAMP(timezone=True),
server_default=sa.text('CURRENT_TIMESTAMP'),
nullable=False,
comment='The last time Devicehub recorded a change for \n this thing.\n ',
),
sa.Column(
'created',
sa.TIMESTAMP(timezone=True),
server_default=sa.text('CURRENT_TIMESTAMP'),
nullable=False,
comment='When Devicehub created this.',
),
sa.Column('id', sa.BigInteger(), nullable=False),
sa.Column('documentId', citext.CIText(), nullable=True),
sa.Column('documentSignature', citext.CIText(), nullable=True),
sa.Column('timestamp', sa.BigInteger(), nullable=False),
sa.Column('device_id', sa.BigInteger(), nullable=False),
sa.Column('snapshot_id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('issuer_id', postgresql.UUID(as_uuid=True), nullable=False),
sa.ForeignKeyConstraint(
['snapshot_id'],
[f'{get_inv()}.snapshot.id'],
),
sa.ForeignKeyConstraint(
['device_id'],
[f'{get_inv()}.device.id'],
),
sa.ForeignKeyConstraint(
['issuer_id'],
[f'common.user.id'],
),
sa.Column('key', sa.Unicode(), nullable=False),
sa.PrimaryKeyConstraint('id'),
schema=f'{get_inv()}',
)
def downgrade():

View File

@ -11,6 +11,7 @@ Within the above general classes are subclasses in A order.
"""
import copy
import json
from collections import Iterable
from contextlib import suppress
from datetime import datetime, timedelta, timezone
@ -678,6 +679,45 @@ class Snapshot(JoinedWithOneDeviceMixin, ActionWithOneDevice):
sid = Column(CIText(), nullable=True)
settings_version = Column(CIText(), nullable=True)
is_server_erase = Column(Boolean(), nullable=True)
json_wb = Column(CIText(), nullable=False)
json_wb.comment = "original json of the workbench"
json_hw = Column(CIText(), nullable=False)
json_hw.comment = (
"json with alphabetic ordered of the hardware than exist in json_wb"
)
phid_dpp = Column(CIText(), nullable=False)
phid_dpp.comment = "hash of json_hw this with the chid if the device conform the DPP, (Digital PassPort)"
def create_json_hw(self, json_wb):
"""
Create a json with the hardware without actions of the original json, (json_wb).
This json need have an alphabetic order.
Next is necessary create a hash of this json and put it intu phid field.
And last save in text the correct json_wb and json_hw in the respective fields
"""
if not json_wb:
return
json_hw = {}
for k, v in json_wb.items():
if k == 'device':
json_hw['device'] = copy.copy(v)
json_hw['device'].pop('actions', None)
json_hw['device'].pop('actions_one', None)
if k == 'components':
components = []
for component in v:
c = component
c.pop('actions', None)
c.pop('actions_one', None)
components.append(c)
# if 'manufacturer', 'model', 'serialNumber' key filter broken'
# key_filter = itemgetter('type', 'manufacturer', 'model', 'serialNumber')
key_filter = itemgetter('type')
json_hw['components'] = sorted(components, key=key_filter)
self.json_wb = json.dumps(json_wb)
self.json_hw = json.dumps(json_hw)
self.phid_dpp = hashlib.sha3_256(self.json_hw.encode('utf-8')).hexdigest()
def get_last_lifetimes(self):
"""We get the lifetime and serial_number of the first disk"""

View File

@ -117,6 +117,8 @@ class Device(Thing):
"""
+ HID_CONVERSION_DOC
)
chid_dpp = Column(Unicode(), check_lower('chid'), unique=False)
chid_dpp.comment = "Chid for identify one device front the DLT"
model = Column(Unicode(), check_lower('model'))
model.comment = """The model of the device in lower case.
@ -212,6 +214,7 @@ class Device(Thing):
'active',
'phid_bk',
'dhid_bk',
'chid_dpp',
}
__table_args__ = (
@ -750,6 +753,7 @@ class Device(Thing):
self.hid = Naming.hid(
self.type, self.manufacturer, self.model, self.serial_number
)
self.chid_dpp = hashlib.sha3_256(self.hid.encode('utf-8')).hexdigest()
def last_action_of(self, *types):
"""Gets the last action of the given types.