Add Proof N:M Device relation and support deviceIDs on POST

This commit is contained in:
Big Lebowski 2020-03-17 02:03:48 +01:00
parent 621e172e55
commit 3ce32f2dd4
3 changed files with 21 additions and 1 deletions

View File

@ -26,6 +26,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, Trade
from ereuse_devicehub.resources.device.models import Device
from ereuse_devicehub.resources.models import Thing
@ -43,6 +44,11 @@ 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)
@property
def url(self) -> urlutils.URL:
@ -74,6 +80,13 @@ class Proof(Thing):
return '<{0.t} {0.id} >'.format(self)
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, ForeignKey(Trade.id), nullable=False)
transfer = relationship(DisposeProduct,

View File

@ -11,6 +11,7 @@ from ereuse_devicehub.resources.proof import models as m
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
class Proof(Thing):
@ -19,6 +20,12 @@ 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)
class ProofTransfer(Proof):

View File

@ -33,8 +33,8 @@ 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))
db.session.commit()
response = jsonify({
'items': proofs,
'url': request.path