From bd2d72ad69956b7c96595402ba40520d22971b97 Mon Sep 17 00:00:00 2001 From: yiorgos marinellis Date: Tue, 24 Mar 2020 22:34:53 +0100 Subject: [PATCH] Replace ethereum_address for User with id --- ereuse_devicehub/resources/proof/models.py | 48 ++++++++++----------- ereuse_devicehub/resources/proof/schemas.py | 27 ++++++------ ereuse_devicehub/resources/proof/views.py | 5 ++- 3 files changed, 42 insertions(+), 38 deletions(-) diff --git a/ereuse_devicehub/resources/proof/models.py b/ereuse_devicehub/resources/proof/models.py index 6f63404b..ed8bd798 100644 --- a/ereuse_devicehub/resources/proof/models.py +++ b/ereuse_devicehub/resources/proof/models.py @@ -49,7 +49,7 @@ class Proof(Thing): db.ForeignKey(Device.id), nullable=False) device = db.relationship(Device, - backref=db.backref('devices', uselist=True, lazy=True), + backref=db.backref('proofs_device', uselist=True, lazy=True), lazy=True, primaryjoin=Device.id == device_id) @@ -85,28 +85,28 @@ class Proof(Thing): class ProofTransfer(JoinedTableMixin, Proof): - supplier_id = db.Column(CIText(), - db.ForeignKey(User.ethereum_address), + supplier_id = db.Column(UUID(as_uuid=True), + db.ForeignKey(User.id), nullable=False, - default=lambda: g.user.ethereum_address) - supplier = db.relationship(User, primaryjoin=lambda: ProofTransfer.supplier_id == User.ethereum_address) - receiver_id = db.Column(CIText(), - db.ForeignKey(User.ethereum_address), + default=lambda: g.user.id) + supplier = db.relationship(User, primaryjoin=lambda: ProofTransfer.supplier_id == User.id) + receiver_id = db.Column(UUID(as_uuid=True), + db.ForeignKey(User.id), nullable=False) - receiver = db.relationship(User, primaryjoin=lambda: ProofTransfer.receiver_id == User.ethereum_address) + receiver = db.relationship(User, primaryjoin=lambda: ProofTransfer.receiver_id == User.id) deposit = Column(db.Integer, default=0) class ProofDataWipe(JoinedTableMixin, Proof): - erasure_type = Column(CIText(), default='', nullable=False) + # erasure_type = Column(CIText(), default='', nullable=False) 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), + proof_author_id = Column(UUID(as_uuid=True), + db.ForeignKey(User.id), nullable=False, - default=lambda: g.user.ethereum_address) - proof_author = relationship(User, primaryjoin=lambda: ProofDataWipe.proof_author_id == User.ethereum_address) + default=lambda: g.user.id) + proof_author = relationship(User, primaryjoin=lambda: ProofDataWipe.proof_author_id == User.id) erasure_id = Column(UUID(as_uuid=True), ForeignKey(EraseBasic.id), nullable=False) erasure = relationship(EraseBasic, backref=backref('proof_datawipe', @@ -118,11 +118,11 @@ class ProofDataWipe(JoinedTableMixin, Proof): class ProofFunction(JoinedTableMixin, Proof): disk_usage = Column(db.Integer, default=0) - proof_author_id = Column(CIText(), - db.ForeignKey(User.ethereum_address), + proof_author_id = Column(UUID(as_uuid=True), + db.ForeignKey(User.id), nullable=False, - default=lambda: g.user.ethereum_address) - proof_author = db.relationship(User, primaryjoin=lambda: ProofFunction.proof_author_id == User.ethereum_address) + default=lambda: g.user.id) + proof_author = db.relationship(User, primaryjoin=lambda: ProofFunction.proof_author_id == User.id) rate_id = Column(UUID(as_uuid=True), ForeignKey(Rate.id), nullable=False) rate = relationship(Rate, backref=backref('proof_function', @@ -135,15 +135,15 @@ class ProofFunction(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), + supplier_id = db.Column(UUID(as_uuid=True), + db.ForeignKey(User.id), 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), + default=lambda: g.user.id) + supplier = db.relationship(User, primaryjoin=lambda: ProofReuse.supplier_id == User.id) + receiver_id = db.Column(UUID(as_uuid=True), + db.ForeignKey(User.id), nullable=False) - receiver = db.relationship(User, primaryjoin=lambda: ProofReuse.receiver_id == User.ethereum_address) + receiver = db.relationship(User, primaryjoin=lambda: ProofReuse.receiver_id == User.id) price = Column(db.Integer) diff --git a/ereuse_devicehub/resources/proof/schemas.py b/ereuse_devicehub/resources/proof/schemas.py index 9726381a..581ad681 100644 --- a/ereuse_devicehub/resources/proof/schemas.py +++ b/ereuse_devicehub/resources/proof/schemas.py @@ -21,31 +21,34 @@ 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__) - device = NestedOn(s_device.Device, only_query='id', required=True, data_key='deviceID') + device_id = Integer(load_only=True, data_key='deviceID') + device = NestedOn(s_device.Device, dump_only=True) class ProofTransfer(Proof): __doc__ = m.ProofTransfer.__doc__ - deposit = Integer() - supplier_id = SanitizedStr(validate=f.validate.Length(max=STR_SIZE), - load_only=True, required=True, data_key='supplierID') - receiver_id = SanitizedStr(validate=f.validate.Length(max=STR_SIZE), - load_only=True, required=True, data_key='receiverID') + deposit = Integer(validate=f.validate.Range(min=0, max=100)) + supplier_id = UUID(load_only=True, required=True, data_key='supplierID') + receiver_id = UUID(load_only=True, required=True, data_key='receiverID') class ProofDataWipe(Proof): __doc__ = m.ProofDataWipe.__doc__ - erasure_type = String(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') + proof_author_id = SanitizedStr(validate=f.validate.Length(max=STR_SIZE), + load_only=True, required=True, data_key='proofAuthor') + proof_author = NestedOn(s_user.User, dump_only=True) 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') + disk_usage = Integer(validate=f.validate.Range(min=0, max=100), data_key='diskUsage') + proof_author_id = SanitizedStr(validate=f.validate.Length(max=STR_SIZE), + load_only=True, required=True, data_key='proofAuthor') + proof_author = NestedOn(s_user.User, dump_only=True) rate = NestedOn(s_action.Rate, required=True, only_query='id', data_key='rateID') @@ -54,8 +57,8 @@ class ProofReuse(Proof): __doc__ = m.ProofReuse.__doc__ 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') + supplier_id = UUID(load_only=True, required=True, data_key='supplierID') + receiver_id = UUID(load_only=True, required=True, data_key='receiverID') price = Integer(required=True) diff --git a/ereuse_devicehub/resources/proof/views.py b/ereuse_devicehub/resources/proof/views.py index cf63bdfe..9b016df9 100644 --- a/ereuse_devicehub/resources/proof/views.py +++ b/ereuse_devicehub/resources/proof/views.py @@ -33,8 +33,9 @@ class ProofView(View): Model = db.Model._decl_class_registry.data[prf['type']]() proof = Model(**p) db.session.add(proof) - db.session.commit() - proofs.append(self.schema.dump(proof)) + proofs.append(resource_def.schema.dump(proof)) + db.session().final_flush() + db.session.commit() response = jsonify({ 'items': proofs, 'url': request.path