This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
2020-12-18 19:10:58 +00:00
|
|
|
"""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.
|
|
|
|
"""
|
2020-12-22 12:04:16 +00:00
|
|
|
created = db.Column(db.TIMESTAMP(timezone=True),
|
|
|
|
nullable=False,
|
|
|
|
index=True,
|
|
|
|
server_default=db.text('CURRENT_TIMESTAMP'))
|
|
|
|
created.comment = """When Devicehub created this."""
|
2020-12-18 19:10:58 +00:00
|
|
|
hash3 = db.Column(CIText(), nullable=False)
|
|
|
|
hash3.comment = """The normalized name of the hash."""
|
|
|
|
|
|
|
|
|
2020-12-18 19:11:50 +00:00
|
|
|
def insert_hash(bfile):
|
2020-12-18 19:10:58 +00:00
|
|
|
hash3 = hashlib.sha3_256(bfile).hexdigest()
|
|
|
|
db_hash = ReportHash(hash3=hash3)
|
|
|
|
db.session.add(db_hash)
|
|
|
|
db.session.commit()
|
|
|
|
db.session.flush()
|