Add schemas for all proofs, redefince and 1-1 relation to device

This commit is contained in:
yiorgos marinellis 2020-03-20 23:57:30 +01:00
parent 4f6eb00d3a
commit 8a818bdc00
2 changed files with 43 additions and 23 deletions

View File

@ -28,6 +28,7 @@ from ereuse_devicehub.resources.action.models import Action, DisposeProduct, \
EraseBasic, Rate, Trade EraseBasic, Rate, Trade
from ereuse_devicehub.resources.device.models import Device from ereuse_devicehub.resources.device.models import Device
from ereuse_devicehub.resources.models import Thing from ereuse_devicehub.resources.models import Thing
from ereuse_devicehub.resources.user import User
class JoinedTableMixin: class JoinedTableMixin:
@ -44,11 +45,13 @@ class Proof(Thing):
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid4) id = Column(UUID(as_uuid=True), primary_key=True, default=uuid4)
type = Column(Unicode, nullable=False) type = Column(Unicode, nullable=False)
ethereum_hash = Column(CIText(), default='', nullable=False) ethereum_hash = Column(CIText(), default='', nullable=False)
devices = relationship(Device, device_id = db.Column(BigInteger,
backref=backref('proofs_multiple', lazy=True), db.ForeignKey(Device.id),
secondary=lambda: ProofDevice.__table__, nullable=False)
order_by=lambda: Device.id, device = db.relationship(Device,
collection_class=OrderedSet) backref=db.backref('devices', uselist=True, lazy=True),
lazy=True,
primaryjoin=Device.id == device_id)
@property @property
def url(self) -> urlutils.URL: 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): class ProofTransfer(JoinedTableMixin, Proof):
transfer_id = Column(UUID(as_uuid=True), ForeignKey(Trade.id), nullable=False) transfer_id = Column(UUID(as_uuid=True), ForeignKey(Trade.id), nullable=False)
transfer = relationship(DisposeProduct, transfer = relationship(DisposeProduct,
backref=backref("proof_transfer", backref=backref("proof_transfer",
lazy=True, lazy=True,
uselist=False,
cascade=CASCADE_OWN), cascade=CASCADE_OWN),
uselist=False,
primaryjoin=DisposeProduct.id == transfer_id) primaryjoin=DisposeProduct.id == transfer_id)
@ -102,33 +99,55 @@ class ProofDataWipe(JoinedTableMixin, Proof):
date = Column(db.DateTime, nullable=False, default=datetime.utcnow) date = Column(db.DateTime, nullable=False, default=datetime.utcnow)
result = Column(db.Boolean, default=False, nullable=False) result = Column(db.Boolean, default=False, nullable=False)
result.comment = """Identifies proof datawipe as a result.""" 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_id = Column(UUID(as_uuid=True), ForeignKey(EraseBasic.id), nullable=False)
erasure = relationship(EraseBasic, erasure = relationship(EraseBasic,
backref=backref('proof_datawipe', backref=backref('proof_datawipe',
lazy=True, lazy=True,
uselist=False,
cascade=CASCADE_OWN), cascade=CASCADE_OWN),
primaryjoin=EraseBasic.id == erasure_id) primaryjoin=EraseBasic.id == erasure_id)
class ProofFunction(JoinedTableMixin, Proof): class ProofFunction(JoinedTableMixin, Proof):
disk_usage = Column(db.Integer, default=0) 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_id = Column(UUID(as_uuid=True), ForeignKey(Rate.id), nullable=False)
rate = relationship(Rate, rate = relationship(Rate,
backref=backref('proof_function', backref=backref('proof_function',
lazy=True, lazy=True,
uselist=False,
cascade=CASCADE_OWN), cascade=CASCADE_OWN),
primaryjoin=Rate.id == rate_id) primaryjoin=Rate.id == rate_id)
class ProofReuse(JoinedTableMixin, Proof): 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) price = Column(db.Integer)
class ProofRecycling(JoinedTableMixin, Proof): class ProofRecycling(JoinedTableMixin, Proof):
collection_point = Column(CIText(), default='', nullable=False) 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) contact = Column(CIText(), default='', nullable=False)
ticket = Column(CIText(), default='', nullable=False) ticket = Column(CIText(), default='', nullable=False)
gps_location = Column(CIText(), default='', nullable=False) gps_location = Column(CIText(), default='', nullable=False)
recycler_code = Column(CIText(), default='', nullable=False) recycler_code = Column(CIText(), default='', nullable=False)

View File

@ -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.schemas import Thing
from ereuse_devicehub.resources.action import schemas as s_action from ereuse_devicehub.resources.action import schemas as s_action
from ereuse_devicehub.resources.device import schemas as s_device from ereuse_devicehub.resources.device import schemas as s_device
from ereuse_devicehub.resources.user import schemas as s_user
class Proof(Thing): class Proof(Thing):
@ -20,40 +21,40 @@ class Proof(Thing):
ethereum_hash = SanitizedStr(default='', validate=Length(max=STR_BIG_SIZE), ethereum_hash = SanitizedStr(default='', validate=Length(max=STR_BIG_SIZE),
data_key="ethereumHash", required=True) data_key="ethereumHash", required=True)
url = URL(dump_only=True, description=m.Proof.url.__doc__) url = URL(dump_only=True, description=m.Proof.url.__doc__)
devices = NestedOn(s_device.Device, device = NestedOn(s_device.Device, only_query='id', required=True, data_key='deviceID')
many=True,
required=True, # todo test ensuring len(devices) >= 1
only_query='id',
data_key='deviceIDs',
collection_class=OrderedSet)
class ProofTransfer(Proof): class ProofTransfer(Proof):
__doc__ = m.ProofTransfer.__doc__ __doc__ = m.ProofTransfer.__doc__
transfer = NestedOn(s_action.DisposeProduct, transfer = NestedOn(s_action.DisposeProduct,
required=True, required=True,
data_key='transferID',
only_query='id') only_query='id')
class ProofDataWipe(Proof): class ProofDataWipe(Proof):
__doc__ = m.ProofDataWipe.__doc__ __doc__ = m.ProofDataWipe.__doc__
erasure_type = SanitizedStr(default='', data_key='erasureType') erasure_type = String(default='', data_key='erasureType')
date = DateTime('iso', required=True) date = DateTime('iso', required=True)
result = Boolean(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') erasure = NestedOn(s_action.EraseBasic, only_query='id', data_key='erasureID')
class ProofFunction(Proof): class ProofFunction(Proof):
__doc__ = m.ProofFunction.__doc__ __doc__ = m.ProofFunction.__doc__
disk_usage = Integer(data_key='diskUsage') 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, rate = NestedOn(s_action.Rate, required=True,
only_query='id', data_key='rateID') only_query='id', data_key='rateID')
class ProofReuse(Proof): class ProofReuse(Proof):
__doc__ = m.ProofReuse.__doc__ __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): class ProofRecycling(Proof):