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.
devicehub-teal/ereuse_devicehub/modules/dpp/models.py

117 lines
3.9 KiB
Python
Raw Normal View History

2023-03-01 17:06:41 +00:00
from citext import CIText
2023-01-19 11:26:25 +00:00
from flask import g
2023-01-18 17:56:04 +00:00
from sortedcontainers import SortedSet
2023-01-19 11:26:25 +00:00
from sqlalchemy import BigInteger, Column, ForeignKey, Sequence, Unicode
2023-01-18 17:56:04 +00:00
from sqlalchemy.dialects.postgresql import UUID
2023-01-19 11:26:25 +00:00
from sqlalchemy.orm import backref, relationship
from sqlalchemy.util import OrderedSet
2023-01-18 17:56:04 +00:00
2023-08-22 08:41:48 +00:00
from ereuse_devicehub.resources.action.models import Action, Snapshot
2023-01-19 11:26:25 +00:00
from ereuse_devicehub.resources.device.models import Device
from ereuse_devicehub.resources.models import STR_SM_SIZE, Thing
from ereuse_devicehub.resources.user.models import User
2023-05-24 08:27:31 +00:00
from ereuse_devicehub.teal.db import CASCADE_OWN
2023-01-18 17:56:04 +00:00
2023-10-03 12:37:49 +00:00
2023-10-20 10:29:49 +00:00
ALGORITHM = "sha3-256"
2023-10-03 12:37:49 +00:00
2023-01-18 17:56:04 +00:00
PROOF_ENUM = {
'Register': 'Register',
'IssueDPP': 'IssueDPP',
'proof_of_recycling': 'proof_of_recycling',
2023-02-20 18:16:43 +00:00
'Erase': 'Erase',
2023-08-22 08:41:48 +00:00
'EWaste': 'EWaste',
2023-01-18 17:56:04 +00:00
}
2023-10-03 12:37:49 +00:00
_sorted_proofs = {
'order_by': lambda: Proof.created,
'collection_class': SortedSet
}
2023-01-18 17:56:04 +00:00
class Proof(Thing):
2023-06-21 10:48:36 +00:00
id = Column(BigInteger, Sequence('proof_seq'), primary_key=True)
2023-01-18 17:56:04 +00:00
id.comment = """The identifier of the device for this database. Used only
internally for software; users should not use this."""
2023-03-01 17:06:41 +00:00
documentId = Column(CIText(), nullable=True)
2023-06-21 10:48:36 +00:00
documentId.comment = "is the uuid of snapshot"
2023-03-01 17:06:41 +00:00
documentSignature = Column(CIText(), nullable=True)
2023-06-21 10:48:36 +00:00
documentSignature.comment = "is the snapshot.json_hw with the signature of the user"
2023-08-22 08:41:48 +00:00
normalizeDoc = Column(CIText(), nullable=True)
2023-01-18 17:56:04 +00:00
timestamp = Column(BigInteger, nullable=False)
type = Column(Unicode(STR_SM_SIZE), nullable=False)
2023-01-19 11:26:25 +00:00
issuer_id = Column(
UUID(as_uuid=True),
ForeignKey(User.id),
nullable=False,
default=lambda: g.user.id,
)
issuer = relationship(
User,
backref=backref('issuered_proofs', lazy=True, collection_class=set),
primaryjoin=User.id == issuer_id,
)
2023-01-18 17:56:04 +00:00
issuer_id.comment = """The user that recorded this proof in the system."""
device_id = Column(BigInteger, ForeignKey(Device.id), nullable=False)
2023-01-19 11:26:25 +00:00
device = relationship(
Device,
backref=backref('proofs', lazy=True, cascade=CASCADE_OWN),
primaryjoin=Device.id == device_id,
)
2023-01-18 17:56:04 +00:00
2023-08-22 08:41:48 +00:00
action_id = Column(UUID(as_uuid=True), ForeignKey(Action.id), nullable=True)
action = relationship(
Action,
2023-01-19 11:26:25 +00:00
backref=backref('proofs', lazy=True),
collection_class=OrderedSet,
2023-08-22 08:41:48 +00:00
primaryjoin=Action.id == action_id,
2023-01-19 11:26:25 +00:00
)
2023-01-18 17:56:04 +00:00
class Dpp(Thing):
"""
Digital PassPort:
It is a type of proof with some field more.
Is the official Digital Passport
"""
2023-01-19 11:26:25 +00:00
2023-06-21 10:48:36 +00:00
id = Column(BigInteger, Sequence('dpp_seq'), primary_key=True)
2023-03-01 17:06:41 +00:00
key = Column(CIText(), nullable=False)
2023-01-18 17:56:04 +00:00
key.comment = "chid:phid, (chid it's in device and phid it's in the snapshot)"
2023-03-01 17:06:41 +00:00
documentId = Column(CIText(), nullable=True)
2023-06-21 10:48:36 +00:00
documentId.comment = "is the uuid of snapshot"
2023-03-01 17:06:41 +00:00
documentSignature = Column(CIText(), nullable=True)
2023-06-21 10:48:36 +00:00
documentSignature.comment = "is the snapshot.json_hw with the signature of the user"
2023-01-18 17:56:04 +00:00
timestamp = Column(BigInteger, nullable=False)
2023-01-19 11:26:25 +00:00
issuer_id = Column(
UUID(as_uuid=True),
ForeignKey(User.id),
nullable=False,
default=lambda: g.user.id,
)
issuer = relationship(
User,
backref=backref('issuered_dpp', lazy=True, collection_class=set),
primaryjoin=User.id == issuer_id,
)
2023-01-18 17:56:04 +00:00
issuer_id.comment = """The user that recorded this proof in the system."""
device_id = Column(BigInteger, ForeignKey(Device.id), nullable=False)
2023-01-19 11:26:25 +00:00
device = relationship(
Device,
backref=backref('dpps', lazy=True, cascade=CASCADE_OWN),
primaryjoin=Device.id == device_id,
)
2023-01-18 17:56:04 +00:00
snapshot_id = Column(UUID(as_uuid=True), ForeignKey(Snapshot.id), nullable=False)
2023-01-19 11:26:25 +00:00
snapshot = relationship(
Snapshot,
backref=backref('dpp', lazy=True),
collection_class=OrderedSet,
primaryjoin=Snapshot.id == snapshot_id,
)