Implement batch view
This commit is contained in:
parent
ac8375c98f
commit
c9f6217e42
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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
|
||||
|
|
Reference in New Issue