From db92c390d9155f2c4623f288275c58be3ad69980 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 18 Dec 2020 20:10:58 +0100 Subject: [PATCH] fixed migration and insert_hash function --- .../versions/3eb50297c365_add_hash.py | 15 +++++----- ereuse_devicehub/resources/hash_reports.py | 29 +++++++++++++++++++ 2 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 ereuse_devicehub/resources/hash_reports.py diff --git a/ereuse_devicehub/migrations/versions/3eb50297c365_add_hash.py b/ereuse_devicehub/migrations/versions/3eb50297c365_add_hash.py index 32be4e8d..e6eff98d 100644 --- a/ereuse_devicehub/migrations/versions/3eb50297c365_add_hash.py +++ b/ereuse_devicehub/migrations/versions/3eb50297c365_add_hash.py @@ -5,12 +5,13 @@ Revises: 378b6b147b46 Create Date: 2020-12-18 16:26:15.453694 """ -from alembic import context -from alembic import op -import sqlalchemy as sa -import sqlalchemy_utils + import citext -import teal +import sqlalchemy as sa + +from alembic import op +from alembic import context +from sqlalchemy.dialects import postgresql # revision identifiers, used by Alembic. @@ -29,10 +30,10 @@ def get_inv(): def upgrade(): # Report Hash table op.create_table('report_hash', - sa.Column('id', sa.BigInteger(), nullable=False), + sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), sa.Column('created', sa.TIMESTAMP(timezone=True), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False, comment='When Devicehub created this.'), - sa.Column('hash', citext.CIText(), nullable=False), + sa.Column('hash3', citext.CIText(), nullable=False), sa.PrimaryKeyConstraint('id'), schema=f'{get_inv()}' ) diff --git a/ereuse_devicehub/resources/hash_reports.py b/ereuse_devicehub/resources/hash_reports.py new file mode 100644 index 00000000..26bf5bd2 --- /dev/null +++ b/ereuse_devicehub/resources/hash_reports.py @@ -0,0 +1,29 @@ +"""Hash implementation and save in database +""" +import hashlib + +from citext import CIText +from sqlalchemy import Column +from sqlalchemy.dialects.postgresql import UUID +from uuid import uuid4 + +from ereuse_devicehub.db import db + + +class ReportHash(db.Model): + """Save the hash than is create when one report is download. + """ + id = Column(UUID(as_uuid=True), primary_key=True, default=uuid4) + id.comment = """The identifier of the device for this database. Used only + internally for software; users should not use this. + """ + hash3 = db.Column(CIText(), nullable=False) + hash3.comment = """The normalized name of the hash.""" + + +def insert_hash(bfile=b'hello'): + hash3 = hashlib.sha3_256(bfile).hexdigest() + db_hash = ReportHash(hash3=hash3) + db.session.add(db_hash) + db.session.commit() + db.session.flush()