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-06-16 16:05:52 +00:00
|
|
|
from ereuse_devicehub.resources.action.models import 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
|
|
|
|
|
|
|
PROOF_ENUM = {
|
|
|
|
'Register': 'Register',
|
|
|
|
'IssueDPP': 'IssueDPP',
|
|
|
|
'proof_of_recycling': 'proof_of_recycling',
|
2023-02-20 18:16:43 +00:00
|
|
|
'Erase': 'Erase',
|
2023-01-18 17:56:04 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 11:26:25 +00:00
|
|
|
_sorted_proofs = {'order_by': lambda: Proof.created, 'collection_class': SortedSet}
|
2023-01-18 17:56:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Proof(Thing):
|
|
|
|
id = Column(BigInteger, Sequence('device_seq'), primary_key=True)
|
|
|
|
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-01-18 17:56:04 +00:00
|
|
|
documentId.comment = "is the hash of snapshot.json_wb"
|
2023-03-01 17:06:41 +00:00
|
|
|
documentSignature = Column(CIText(), nullable=True)
|
2023-01-18 17:56:04 +00:00
|
|
|
documentSignature.comment = "is the snapshot.json_wb with the signature of the user"
|
|
|
|
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
|
|
|
|
|
|
|
snapshot_id = Column(UUID(as_uuid=True), ForeignKey(Snapshot.id), nullable=True)
|
2023-01-19 11:26:25 +00:00
|
|
|
snapshot = relationship(
|
|
|
|
Snapshot,
|
|
|
|
backref=backref('proofs', lazy=True),
|
|
|
|
collection_class=OrderedSet,
|
|
|
|
primaryjoin=Snapshot.id == snapshot_id,
|
|
|
|
)
|
|
|
|
|
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-01-18 17:56:04 +00:00
|
|
|
id = Column(BigInteger, Sequence('device_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-01-18 17:56:04 +00:00
|
|
|
documentId.comment = "is the hash of snapshot.json_wb"
|
2023-03-01 17:06:41 +00:00
|
|
|
documentSignature = Column(CIText(), nullable=True)
|
2023-01-18 17:56:04 +00:00
|
|
|
documentSignature.comment = "is the snapshot.json_wb with the signature of the user"
|
|
|
|
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,
|
|
|
|
)
|