Add Proof N:M Device relation and support deviceIDs on POST
This commit is contained in:
parent
621e172e55
commit
3ce32f2dd4
|
@ -26,6 +26,7 @@ from teal.resource import url_for_resource
|
||||||
from ereuse_devicehub.db import db
|
from ereuse_devicehub.db import db
|
||||||
from ereuse_devicehub.resources.action.models import Action, DisposeProduct, \
|
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.models import Thing
|
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)
|
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,
|
||||||
|
backref=backref('proofs_multiple', lazy=True),
|
||||||
|
secondary=lambda: ProofDevice.__table__,
|
||||||
|
order_by=lambda: Device.id,
|
||||||
|
collection_class=OrderedSet)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def url(self) -> urlutils.URL:
|
def url(self) -> urlutils.URL:
|
||||||
|
@ -74,6 +80,13 @@ class Proof(Thing):
|
||||||
return '<{0.t} {0.id} >'.format(self)
|
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):
|
class ProofTransfer(JoinedTableMixin, Proof):
|
||||||
transfer_id = Column(UUID, ForeignKey(Trade.id), nullable=False)
|
transfer_id = Column(UUID, ForeignKey(Trade.id), nullable=False)
|
||||||
transfer = relationship(DisposeProduct,
|
transfer = relationship(DisposeProduct,
|
||||||
|
|
|
@ -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.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
|
||||||
|
|
||||||
|
|
||||||
class Proof(Thing):
|
class Proof(Thing):
|
||||||
|
@ -19,6 +20,12 @@ 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,
|
||||||
|
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):
|
||||||
|
|
|
@ -33,8 +33,8 @@ class ProofView(View):
|
||||||
Model = db.Model._decl_class_registry.data[prf['type']]()
|
Model = db.Model._decl_class_registry.data[prf['type']]()
|
||||||
proof = Model(**p)
|
proof = Model(**p)
|
||||||
db.session.add(proof)
|
db.session.add(proof)
|
||||||
|
db.session.commit()
|
||||||
proofs.append(self.schema.dump(proof))
|
proofs.append(self.schema.dump(proof))
|
||||||
db.session.commit()
|
|
||||||
response = jsonify({
|
response = jsonify({
|
||||||
'items': proofs,
|
'items': proofs,
|
||||||
'url': request.path
|
'url': request.path
|
||||||
|
|
Reference in New Issue