From c9f6217e4264cb98cb6559c1d81018dd4af092b4 Mon Sep 17 00:00:00 2001 From: yiorgos marinellis Date: Thu, 12 Mar 2020 17:07:35 +0100 Subject: [PATCH] Implement batch view --- ereuse_devicehub/config.py | 3 ++- ereuse_devicehub/resources/proof/__init__.py | 5 +++-- ereuse_devicehub/resources/proof/models.py | 10 +++++----- ereuse_devicehub/resources/proof/schemas.py | 5 +++-- ereuse_devicehub/resources/proof/views.py | 17 +++++++++-------- 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/ereuse_devicehub/config.py b/ereuse_devicehub/config.py index 36c2dbd7..b1db6e9e 100644 --- a/ereuse_devicehub/config.py +++ b/ereuse_devicehub/config.py @@ -7,7 +7,8 @@ from teal.config import Config from teal.enums import Currency from teal.utils import import_resource -from ereuse_devicehub.resources import action, agent, deliverynote, inventory, lot, tag, user +from ereuse_devicehub.resources import action, agent, deliverynote, inventory, \ + lot, proof, tag, user from ereuse_devicehub.resources.device import definitions from ereuse_devicehub.resources.documents import documents from ereuse_devicehub.resources.enums import PriceSoftware diff --git a/ereuse_devicehub/resources/proof/__init__.py b/ereuse_devicehub/resources/proof/__init__.py index 93b8e9f3..ef3a68a6 100644 --- a/ereuse_devicehub/resources/proof/__init__.py +++ b/ereuse_devicehub/resources/proof/__init__.py @@ -1,13 +1,14 @@ from teal.resource import Converters, Resource -from ereuse_devicehub.resources.action import schemas +from ereuse_devicehub.resources.proof import schemas from ereuse_devicehub.resources.proof.views import ProofView class ProofDef(Resource): SCHEMA = schemas.Proof VIEW = ProofView - AUTH = True + # AUTH = True + AUTH = False ID_CONVERTER = Converters.uuid diff --git a/ereuse_devicehub/resources/proof/models.py b/ereuse_devicehub/resources/proof/models.py index 47888943..48f016f0 100644 --- a/ereuse_devicehub/resources/proof/models.py +++ b/ereuse_devicehub/resources/proof/models.py @@ -25,7 +25,7 @@ from teal.resource import url_for_resource from ereuse_devicehub.db import db from ereuse_devicehub.resources.action.models import Action, DisposeProduct, \ - EraseBasic, Rate + EraseBasic, Rate, Trade from ereuse_devicehub.resources.models import Thing @@ -79,7 +79,7 @@ class Proof(Thing): class ProofTransfer(JoinedTableMixin, Proof): - transfer_id = Column(BigInteger, ForeignKey(Action.id), nullable=False) + transfer_id = Column(UUID, ForeignKey(Trade.id), nullable=False) transfer = relationship(DisposeProduct, primaryjoin=DisposeProduct.id == transfer_id) @@ -88,7 +88,7 @@ class ProofDataWipe(JoinedTableMixin, Proof): erasure_type = Column(CIText()) date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) result = db.Column(db.Boolean, default=False, nullable=False) - erasure_id = Column(BigInteger, ForeignKey(EraseBasic.id), nullable=False) + erasure_id = Column(UUID, ForeignKey(EraseBasic.id), nullable=False) erasure = relationship(EraseBasic, backref=backref('proofs_datawipe', lazy=True, @@ -98,13 +98,13 @@ class ProofDataWipe(JoinedTableMixin, Proof): class ProofFunction(JoinedTableMixin, Proof): disk_usage = db.Column(db.Integer, default=0) - rate_id = Column(BigInteger, ForeignKey(Rate.id), nullable=False) + rate_id = Column(UUID, ForeignKey(Rate.id), nullable=False) rate = relationship(Rate, primaryjoin=Rate.id == rate_id) class ProofReuse(JoinedTableMixin, Proof): - price = db.Column(db.Integer, required=True) + price = db.Column(db.Integer) class ProofRecycling(JoinedTableMixin, Proof): diff --git a/ereuse_devicehub/resources/proof/schemas.py b/ereuse_devicehub/resources/proof/schemas.py index 650a2090..1aba739e 100644 --- a/ereuse_devicehub/resources/proof/schemas.py +++ b/ereuse_devicehub/resources/proof/schemas.py @@ -16,7 +16,8 @@ from ereuse_devicehub.resources.action import schemas as s_action class Proof(Thing): __doc__ = m.Proof.__doc__ id = UUID(dump_only=True) - ethereumHashes = SanitizedStr(default='', validate=Length(max=STR_BIG_SIZE)) + ethereum_hashes = SanitizedStr(default='', validate=Length(max=STR_BIG_SIZE), + data_key="ethereumHashes") url = URL(dump_only=True, description=m.Proof.url.__doc__) @@ -32,7 +33,7 @@ class ProofDataWipe(Proof): erasure_type = SanitizedStr(default='') date = DateTime() result = Boolean(missing=False) - erasure = NestedOn(s_action.EraseBasic, required=True, only_query='id') + erasure = NestedOn(s_action.EraseBasic, dump_only=True, only_query='id') class ProofFunction(Proof): diff --git a/ereuse_devicehub/resources/proof/views.py b/ereuse_devicehub/resources/proof/views.py index 61886ae8..2485a6a2 100644 --- a/ereuse_devicehub/resources/proof/views.py +++ b/ereuse_devicehub/resources/proof/views.py @@ -24,15 +24,16 @@ class ProofView(View): raise ValidationError('JSON is not correct.') # todo there should be a way to better get subclassess resource # defs + proofs = list() if json['batch']: - for proof in json['proofs']: - resource_def = app.resources[proof['type']] - a = resource_def.schema.load(json) - Model = db.Model._decl_class_registry.data[json['type']]() - action = Model(**a) - db.session.add(action) - db.session().final_flush() - ret = self.schema.jsonify(action) + for prf in json['proofs']: + resource_def = app.resources[prf['type']] + p = resource_def.schema.load(prf) + Model = db.Model._decl_class_registry.data[prf['type']]() + proof = Model(**p) + db.session.add(proof) + proofs.append(self.schema.dump(proof)) db.session.commit() + ret = self.schema.jsonify(proofs) ret.status_code = 201 return ret