From 8a818bdc007fc3c4a8cedbb211ca4f87a48b78f0 Mon Sep 17 00:00:00 2001 From: yiorgos marinellis Date: Fri, 20 Mar 2020 23:57:30 +0100 Subject: [PATCH] Add schemas for all proofs, redefince and 1-1 relation to device --- ereuse_devicehub/resources/proof/models.py | 47 +++++++++++++++------ ereuse_devicehub/resources/proof/schemas.py | 19 +++++---- 2 files changed, 43 insertions(+), 23 deletions(-) diff --git a/ereuse_devicehub/resources/proof/models.py b/ereuse_devicehub/resources/proof/models.py index 07739ecb..ff7a252e 100644 --- a/ereuse_devicehub/resources/proof/models.py +++ b/ereuse_devicehub/resources/proof/models.py @@ -28,6 +28,7 @@ from ereuse_devicehub.resources.action.models import Action, DisposeProduct, \ EraseBasic, Rate, Trade from ereuse_devicehub.resources.device.models import Device from ereuse_devicehub.resources.models import Thing +from ereuse_devicehub.resources.user import User class JoinedTableMixin: @@ -44,11 +45,13 @@ class Proof(Thing): id = Column(UUID(as_uuid=True), primary_key=True, default=uuid4) type = Column(Unicode, nullable=False) ethereum_hash = Column(CIText(), default='', nullable=False) - devices = relationship(Device, - backref=backref('proofs_multiple', lazy=True), - secondary=lambda: ProofDevice.__table__, - order_by=lambda: Device.id, - collection_class=OrderedSet) + device_id = db.Column(BigInteger, + db.ForeignKey(Device.id), + nullable=False) + device = db.relationship(Device, + backref=db.backref('devices', uselist=True, lazy=True), + lazy=True, + primaryjoin=Device.id == device_id) @property def url(self) -> urlutils.URL: @@ -81,19 +84,13 @@ class Proof(Thing): -class ProofDevice(db.Model): - device_id = Column(BigInteger, ForeignKey(Device.id), primary_key=True) - proof_id = Column(UUID(as_uuid=True), ForeignKey(Proof.id), - primary_key=True) - - class ProofTransfer(JoinedTableMixin, Proof): transfer_id = Column(UUID(as_uuid=True), ForeignKey(Trade.id), nullable=False) transfer = relationship(DisposeProduct, backref=backref("proof_transfer", lazy=True, + uselist=False, cascade=CASCADE_OWN), - uselist=False, primaryjoin=DisposeProduct.id == transfer_id) @@ -102,33 +99,55 @@ class ProofDataWipe(JoinedTableMixin, Proof): date = Column(db.DateTime, nullable=False, default=datetime.utcnow) result = Column(db.Boolean, default=False, nullable=False) result.comment = """Identifies proof datawipe as a result.""" + proof_author_id = Column(CIText(), + db.ForeignKey(User.ethereum_address), + nullable=False, + default=lambda: g.user.ethereum_address) + proof_author = relationship(User, primaryjoin=lambda: ProofDataWipe.proof_author_id == User.ethereum_address) erasure_id = Column(UUID(as_uuid=True), ForeignKey(EraseBasic.id), nullable=False) erasure = relationship(EraseBasic, backref=backref('proof_datawipe', lazy=True, + uselist=False, cascade=CASCADE_OWN), primaryjoin=EraseBasic.id == erasure_id) class ProofFunction(JoinedTableMixin, Proof): disk_usage = Column(db.Integer, default=0) + proof_author_id = Column(CIText(), + db.ForeignKey(User.ethereum_address), + nullable=False, + default=lambda: g.user.ethereum_address) + proof_author = db.relationship(User, primaryjoin=lambda: ProofFunction.proof_author_id == User.ethereum_address) rate_id = Column(UUID(as_uuid=True), ForeignKey(Rate.id), nullable=False) rate = relationship(Rate, backref=backref('proof_function', lazy=True, + uselist=False, cascade=CASCADE_OWN), primaryjoin=Rate.id == rate_id) class ProofReuse(JoinedTableMixin, Proof): + receiver_segment = Column(CIText(), default='', nullable=False) + id_receipt = Column(CIText(), default='', nullable=False) + supplier_id = db.Column(CIText(), + db.ForeignKey(User.ethereum_address), + nullable=False, + default=lambda: g.user.ethereum_address) + supplier = db.relationship(User, primaryjoin=lambda: ProofReuse.supplier_id == User.ethereum_address) + receiver_id = db.Column(CIText(), + db.ForeignKey(User.ethereum_address), + nullable=False) + receiver = db.relationship(User, primaryjoin=lambda: ProofReuse.receiver_id == User.ethereum_address) price = Column(db.Integer) class ProofRecycling(JoinedTableMixin, Proof): collection_point = Column(CIText(), default='', nullable=False) - date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) + date = Column(db.DateTime, nullable=False, default=datetime.utcnow) contact = Column(CIText(), default='', nullable=False) ticket = Column(CIText(), default='', nullable=False) gps_location = Column(CIText(), default='', nullable=False) recycler_code = Column(CIText(), default='', nullable=False) - diff --git a/ereuse_devicehub/resources/proof/schemas.py b/ereuse_devicehub/resources/proof/schemas.py index 1751dc20..3070448e 100644 --- a/ereuse_devicehub/resources/proof/schemas.py +++ b/ereuse_devicehub/resources/proof/schemas.py @@ -12,6 +12,7 @@ from ereuse_devicehub.resources.models import STR_BIG_SIZE, STR_SIZE from ereuse_devicehub.resources.schemas import Thing from ereuse_devicehub.resources.action import schemas as s_action from ereuse_devicehub.resources.device import schemas as s_device +from ereuse_devicehub.resources.user import schemas as s_user class Proof(Thing): @@ -20,40 +21,40 @@ class Proof(Thing): ethereum_hash = SanitizedStr(default='', validate=Length(max=STR_BIG_SIZE), data_key="ethereumHash", required=True) url = URL(dump_only=True, description=m.Proof.url.__doc__) - devices = NestedOn(s_device.Device, - many=True, - required=True, # todo test ensuring len(devices) >= 1 - only_query='id', - data_key='deviceIDs', - collection_class=OrderedSet) + device = NestedOn(s_device.Device, only_query='id', required=True, data_key='deviceID') class ProofTransfer(Proof): __doc__ = m.ProofTransfer.__doc__ transfer = NestedOn(s_action.DisposeProduct, required=True, - data_key='transferID', only_query='id') class ProofDataWipe(Proof): __doc__ = m.ProofDataWipe.__doc__ - erasure_type = SanitizedStr(default='', data_key='erasureType') + erasure_type = String(default='', data_key='erasureType') date = DateTime('iso', required=True) result = Boolean(required=True) + proof_author = NestedOn(s_user.User, only_query='id', data_key='proofAuthor') erasure = NestedOn(s_action.EraseBasic, only_query='id', data_key='erasureID') class ProofFunction(Proof): __doc__ = m.ProofFunction.__doc__ disk_usage = Integer(data_key='diskUsage') + proof_author = NestedOn(s_user.User, only_query='id', data_key='proofAuthor') rate = NestedOn(s_action.Rate, required=True, only_query='id', data_key='rateID') class ProofReuse(Proof): __doc__ = m.ProofReuse.__doc__ - price = Integer() + receiver_segment = String(default='', data_key='receiverSegment', required=True) + id_receipt = String(default='', data_key='idReceipt', required=True) + supplier = NestedOn(s_user.User, only_query='ethereum_address', required=True, data_key='supplierAddress') + receiver = NestedOn(s_user.User, only_query='ethereum_address', required=True, data_key='receiverAddress') + price = Integer(required=True) class ProofRecycling(Proof):