From 621e172e5581b4964c2712213b4fed7195de90ab Mon Sep 17 00:00:00 2001 From: Big Lebowski Date: Tue, 17 Mar 2020 00:57:14 +0100 Subject: [PATCH 01/16] Add required field result and ethereumHash on POST --- ereuse_devicehub/resources/proof/models.py | 2 +- ereuse_devicehub/resources/proof/schemas.py | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/ereuse_devicehub/resources/proof/models.py b/ereuse_devicehub/resources/proof/models.py index 60e18ae3..3fe4ab7b 100644 --- a/ereuse_devicehub/resources/proof/models.py +++ b/ereuse_devicehub/resources/proof/models.py @@ -42,7 +42,7 @@ class Proof(Thing): """ id = Column(UUID(as_uuid=True), primary_key=True, default=uuid4) type = Column(Unicode, nullable=False) - ethereum_hashes = Column(CIText(), default='', nullable=False) + ethereum_hash = Column(CIText(), default='', nullable=False) @property def url(self) -> urlutils.URL: diff --git a/ereuse_devicehub/resources/proof/schemas.py b/ereuse_devicehub/resources/proof/schemas.py index 4859150a..aa7cba1c 100644 --- a/ereuse_devicehub/resources/proof/schemas.py +++ b/ereuse_devicehub/resources/proof/schemas.py @@ -16,8 +16,8 @@ from ereuse_devicehub.resources.action import schemas as s_action class Proof(Thing): __doc__ = m.Proof.__doc__ id = UUID(dump_only=True) - ethereum_hashes = SanitizedStr(default='', validate=Length(max=STR_BIG_SIZE), - data_key="ethereumHashes") + 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__) @@ -30,10 +30,10 @@ class ProofTransfer(Proof): class ProofDataWipe(Proof): __doc__ = m.ProofDataWipe.__doc__ - erasure_type = SanitizedStr(default='') + erasure_type = SanitizedStr(default='', data_key='erasureType') date = DateTime('iso', required=True) - result = Boolean(missing=False) - erasure = NestedOn(s_action.EraseBasic, only_query='id') + result = Boolean(required=True) + erasure = NestedOn(s_action.EraseBasic, only_query='id', data_key='erasureID') class ProofFunction(Proof): From 3ce32f2dd43b94a93f1555aa6bb23ccab318980f Mon Sep 17 00:00:00 2001 From: Big Lebowski Date: Tue, 17 Mar 2020 02:03:48 +0100 Subject: [PATCH 02/16] Add Proof N:M Device relation and support deviceIDs on POST --- ereuse_devicehub/resources/proof/models.py | 13 +++++++++++++ ereuse_devicehub/resources/proof/schemas.py | 7 +++++++ ereuse_devicehub/resources/proof/views.py | 2 +- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/ereuse_devicehub/resources/proof/models.py b/ereuse_devicehub/resources/proof/models.py index 3fe4ab7b..13b81cc9 100644 --- a/ereuse_devicehub/resources/proof/models.py +++ b/ereuse_devicehub/resources/proof/models.py @@ -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, diff --git a/ereuse_devicehub/resources/proof/schemas.py b/ereuse_devicehub/resources/proof/schemas.py index aa7cba1c..16a833b7 100644 --- a/ereuse_devicehub/resources/proof/schemas.py +++ b/ereuse_devicehub/resources/proof/schemas.py @@ -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): diff --git a/ereuse_devicehub/resources/proof/views.py b/ereuse_devicehub/resources/proof/views.py index c645479d..cf63bdfe 100644 --- a/ereuse_devicehub/resources/proof/views.py +++ b/ereuse_devicehub/resources/proof/views.py @@ -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 From e3f960aa8b4436a857dea12d194caacb3750adb1 Mon Sep 17 00:00:00 2001 From: yiorgos marinellis Date: Tue, 17 Mar 2020 23:36:27 +0100 Subject: [PATCH 03/16] Add ProofFunction view except author field --- ereuse_devicehub/resources/proof/models.py | 2 +- ereuse_devicehub/resources/proof/schemas.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/ereuse_devicehub/resources/proof/models.py b/ereuse_devicehub/resources/proof/models.py index 13b81cc9..4aa4f2fa 100644 --- a/ereuse_devicehub/resources/proof/models.py +++ b/ereuse_devicehub/resources/proof/models.py @@ -112,7 +112,7 @@ class ProofDataWipe(JoinedTableMixin, Proof): class ProofFunction(JoinedTableMixin, Proof): disk_usage = Column(db.Integer, default=0) - rate_id = Column(UUID, ForeignKey(Rate.id), nullable=False) + rate_id = Column(UUID(as_uuid=True), ForeignKey(Rate.id), nullable=False) rate = relationship(Rate, backref=backref('proof_function', lazy=True, diff --git a/ereuse_devicehub/resources/proof/schemas.py b/ereuse_devicehub/resources/proof/schemas.py index 16a833b7..26479e50 100644 --- a/ereuse_devicehub/resources/proof/schemas.py +++ b/ereuse_devicehub/resources/proof/schemas.py @@ -45,8 +45,9 @@ class ProofDataWipe(Proof): class ProofFunction(Proof): __doc__ = m.ProofFunction.__doc__ - disk_usage = Integer() - rate = NestedOn(s_action.Rate, required=True, only_query='id') + disk_usage = Integer(data_key='diskUsage') + rate = NestedOn(s_action.Rate, required=True, + only_query='id', data_key='rateID') class ProofReuse(Proof): From 390fb30d4f8bfb10fa1b3195565c2822edd8ca64 Mon Sep 17 00:00:00 2001 From: yiorgos marinellis Date: Tue, 17 Mar 2020 23:45:23 +0100 Subject: [PATCH 04/16] Update ProofRecycle --- ereuse_devicehub/resources/proof/models.py | 2 ++ ereuse_devicehub/resources/proof/schemas.py | 11 ++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/ereuse_devicehub/resources/proof/models.py b/ereuse_devicehub/resources/proof/models.py index 4aa4f2fa..fb1f676c 100644 --- a/ereuse_devicehub/resources/proof/models.py +++ b/ereuse_devicehub/resources/proof/models.py @@ -130,3 +130,5 @@ class ProofRecycling(JoinedTableMixin, Proof): contact = Column(CIText(), default='', nullable=False) ticket = Column(CIText(), default='', nullable=False) gps_location = Column(CIText(), default='', nullable=False) + recycler_code = Column(CIText(), default='', nullable=False) + diff --git a/ereuse_devicehub/resources/proof/schemas.py b/ereuse_devicehub/resources/proof/schemas.py index 26479e50..bc9143bc 100644 --- a/ereuse_devicehub/resources/proof/schemas.py +++ b/ereuse_devicehub/resources/proof/schemas.py @@ -57,8 +57,9 @@ class ProofReuse(Proof): class ProofRecycling(Proof): __doc__ = m.ProofRecycling.__doc__ - collection_point = SanitizedStr(default='') - date = DateTime() - contact = SanitizedStr(default='') - ticket = SanitizedStr(default='') - gps_location = SanitizedStr(default='') + collection_point = SanitizedStr(default='', data_key='collectionPoint', required=True) + date = DateTime('iso', required=True) + contact = SanitizedStr(default='', required=True) + ticket = SanitizedStr(default='', required=True) + gps_location = SanitizedStr(default='', data_key='gpsLocation', required=True) + recycler_code = SanitizedStr(default='', data_key='recyclerCode', required=True) From f6af58ef51e81b39db03a1d7f57a17745b32574b Mon Sep 17 00:00:00 2001 From: yiorgos marinellis Date: Tue, 17 Mar 2020 23:47:02 +0100 Subject: [PATCH 05/16] Update ProofTransfer --- ereuse_devicehub/resources/proof/models.py | 2 +- ereuse_devicehub/resources/proof/schemas.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/ereuse_devicehub/resources/proof/models.py b/ereuse_devicehub/resources/proof/models.py index fb1f676c..07739ecb 100644 --- a/ereuse_devicehub/resources/proof/models.py +++ b/ereuse_devicehub/resources/proof/models.py @@ -88,7 +88,7 @@ class ProofDevice(db.Model): class ProofTransfer(JoinedTableMixin, Proof): - transfer_id = Column(UUID, ForeignKey(Trade.id), nullable=False) + transfer_id = Column(UUID(as_uuid=True), ForeignKey(Trade.id), nullable=False) transfer = relationship(DisposeProduct, backref=backref("proof_transfer", lazy=True, diff --git a/ereuse_devicehub/resources/proof/schemas.py b/ereuse_devicehub/resources/proof/schemas.py index bc9143bc..1751dc20 100644 --- a/ereuse_devicehub/resources/proof/schemas.py +++ b/ereuse_devicehub/resources/proof/schemas.py @@ -32,6 +32,7 @@ class ProofTransfer(Proof): __doc__ = m.ProofTransfer.__doc__ transfer = NestedOn(s_action.DisposeProduct, required=True, + data_key='transferID', only_query='id') From dfb518068ddf6223c5d3245925fdeaa81ad49045 Mon Sep 17 00:00:00 2001 From: emmdim Date: Thu, 19 Mar 2020 01:55:26 +0100 Subject: [PATCH 06/16] Adds ethereum address to device, and adds PATCH --- ereuse_devicehub/resources/device/models.py | 1 + ereuse_devicehub/resources/device/schemas.py | 1 + ereuse_devicehub/resources/device/views.py | 25 ++++++++++++++++---- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index 1ecbe8f3..68134b8e 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -382,6 +382,7 @@ class Computer(Device): It is a subset of the Linux definition of DMI / DMI decode. """ + ethereum_address = Column(CIText(), unique=True, default=None) deposit = Column(Integer, check_range('deposit',min=0,max=100), default=0) owner_address = db.Column(CIText(), db.ForeignKey(User.ethereum_address), diff --git a/ereuse_devicehub/resources/device/schemas.py b/ereuse_devicehub/resources/device/schemas.py index 093cec75..61c399ad 100644 --- a/ereuse_devicehub/resources/device/schemas.py +++ b/ereuse_devicehub/resources/device/schemas.py @@ -122,6 +122,7 @@ class Computer(Device): dump_only=True, collection_class=set, description=m.Computer.privacy.__doc__) + ethereum_address = SanitizedStr(validate=f.validate.Length(max=42)) deposit = Integer(validate=f.validate.Range(min=0, max=100), description=m.Computer.deposit.__doc__) # author_id = NestedOn(s_user.User,only_query='author_id') diff --git a/ereuse_devicehub/resources/device/views.py b/ereuse_devicehub/resources/device/views.py index 665acd68..71e77cfc 100644 --- a/ereuse_devicehub/resources/device/views.py +++ b/ereuse_devicehub/resources/device/views.py @@ -1,10 +1,10 @@ import datetime import marshmallow -from flask import current_app as app, render_template, request +from flask import current_app as app, render_template, request, Response from flask.json import jsonify from flask_sqlalchemy import Pagination -from marshmallow import fields, fields as f, validate as v +from marshmallow import fields, fields as f, validate as v, ValidationError from teal import query from teal.cache import cache from teal.resource import View @@ -15,7 +15,7 @@ from ereuse_devicehub.query import SearchQueryParser, things_response from ereuse_devicehub.resources import search from ereuse_devicehub.resources.action import models as actions from ereuse_devicehub.resources.device import states -from ereuse_devicehub.resources.device.models import Device, Manufacturer +from ereuse_devicehub.resources.device.models import Device, Manufacturer, Computer from ereuse_devicehub.resources.device.search import DeviceSearch from ereuse_devicehub.resources.lot.models import LotDeviceDescendants from ereuse_devicehub.resources.tag.model import Tag @@ -92,7 +92,24 @@ class DeviceView(View): description: The device or devices. """ return super().get(id) - + + def patch(self, id): + dev = Device.query.filter_by(id=id).one() + if isinstance(dev, Computer): + resource_def = app.resources['Computer'] + # TODO check how to handle the 'actions_one' + patch_schema = resource_def.SCHEMA(only=['ethereum_address', 'actions_one'], partial=True) + json = request.get_json(schema=patch_schema) + # TODO check how to handle the 'actions_one' + json.pop('actions_one') + if not dev: + raise ValueError('Device non existent') + for key, value in json.items(): + setattr(dev,key,value) + db.session.commit() + return Response(status=204) + raise ValueError('Cannot patch a non computer') + def one(self, id: int): """Gets one device.""" if not request.authorization: From b70c2294854a24c4c0811bdd42193b1efc95ea63 Mon Sep 17 00:00:00 2001 From: emmdim Date: Fri, 20 Mar 2020 15:20:22 +0100 Subject: [PATCH 07/16] Implements #13, but currently can be applied only to transferred devices --- ereuse_devicehub/resources/deliverynote/views.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/ereuse_devicehub/resources/deliverynote/views.py b/ereuse_devicehub/resources/deliverynote/views.py index 0b0cb35a..75834c09 100644 --- a/ereuse_devicehub/resources/deliverynote/views.py +++ b/ereuse_devicehub/resources/deliverynote/views.py @@ -16,6 +16,7 @@ from ereuse_devicehub.db import db from ereuse_devicehub.query import things_response from ereuse_devicehub.resources.deliverynote.models import Deliverynote from ereuse_devicehub.resources.lot.models import Lot +from ereuse_devicehub.resources.device.models import Computer class DeliverynoteView(View): @@ -41,8 +42,18 @@ class DeliverynoteView(View): 'ethereum_address'), partial=True) d = request.get_json(schema=patch_schema) dlvnote = Deliverynote.query.filter_by(id=id).one() + device_fields = ['transfer_state', 'deliverynote_address'] + computers = [x for x in dlvnote.transferred_devices if isinstance(x, Computer)] for key, value in d.items(): + # Transalate ethereum_address attribute + devKey = key + if key == 'ethereum_address': + devKey = 'deliverynote_address' setattr(dlvnote, key, value) + if devKey in device_fields: + for dev in computers: + setattr(dev, devKey, value) + db.session.commit() return Response(status=204) From bd396f0642e607593102e92c3bf095f9ed388cf2 Mon Sep 17 00:00:00 2001 From: emmdim Date: Fri, 20 Mar 2020 15:47:42 +0100 Subject: [PATCH 08/16] Adds new PATCH properties for device --- ereuse_devicehub/resources/device/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ereuse_devicehub/resources/device/views.py b/ereuse_devicehub/resources/device/views.py index 71e77cfc..1aaa1f93 100644 --- a/ereuse_devicehub/resources/device/views.py +++ b/ereuse_devicehub/resources/device/views.py @@ -98,7 +98,7 @@ class DeviceView(View): if isinstance(dev, Computer): resource_def = app.resources['Computer'] # TODO check how to handle the 'actions_one' - patch_schema = resource_def.SCHEMA(only=['ethereum_address', 'actions_one'], partial=True) + patch_schema = resource_def.SCHEMA(only=['ethereum_address', 'transfer_state', 'deliverynote_address', 'actions_one'], partial=True) json = request.get_json(schema=patch_schema) # TODO check how to handle the 'actions_one' json.pop('actions_one') From 4f6eb00d3a5ded52a2319e20cf5805eac84b95ae Mon Sep 17 00:00:00 2001 From: emmdim Date: Fri, 20 Mar 2020 15:48:40 +0100 Subject: [PATCH 09/16] Reverts temporarilty the propagation of values for devices until a clear Device registration strategy takes place --- .../resources/deliverynote/views.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/ereuse_devicehub/resources/deliverynote/views.py b/ereuse_devicehub/resources/deliverynote/views.py index 75834c09..02a1ff8b 100644 --- a/ereuse_devicehub/resources/deliverynote/views.py +++ b/ereuse_devicehub/resources/deliverynote/views.py @@ -42,17 +42,17 @@ class DeliverynoteView(View): 'ethereum_address'), partial=True) d = request.get_json(schema=patch_schema) dlvnote = Deliverynote.query.filter_by(id=id).one() - device_fields = ['transfer_state', 'deliverynote_address'] - computers = [x for x in dlvnote.transferred_devices if isinstance(x, Computer)] + # device_fields = ['transfer_state', 'deliverynote_address'] + # computers = [x for x in dlvnote.transferred_devices if isinstance(x, Computer)] for key, value in d.items(): - # Transalate ethereum_address attribute - devKey = key - if key == 'ethereum_address': - devKey = 'deliverynote_address' setattr(dlvnote, key, value) - if devKey in device_fields: - for dev in computers: - setattr(dev, devKey, value) + # Transalate ethereum_address attribute + # devKey = key + # if key == 'ethereum_address': + # devKey = 'deliverynote_address' + # if devKey in device_fields: + # for dev in computers: + # setattr(dev, devKey, value) db.session.commit() return Response(status=204) From 8a818bdc007fc3c4a8cedbb211ca4f87a48b78f0 Mon Sep 17 00:00:00 2001 From: yiorgos marinellis Date: Fri, 20 Mar 2020 23:57:30 +0100 Subject: [PATCH 10/16] Add schemas for all proofs, redefince and 1-1 relation to device --- ereuse_devicehub/resources/proof/models.py | 47 +++++++++++++++------ ereuse_devicehub/resources/proof/schemas.py | 19 +++++---- 2 files changed, 43 insertions(+), 23 deletions(-) diff --git a/ereuse_devicehub/resources/proof/models.py b/ereuse_devicehub/resources/proof/models.py index 07739ecb..ff7a252e 100644 --- a/ereuse_devicehub/resources/proof/models.py +++ b/ereuse_devicehub/resources/proof/models.py @@ -28,6 +28,7 @@ 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 +from ereuse_devicehub.resources.user import User class JoinedTableMixin: @@ -44,11 +45,13 @@ 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) + device_id = db.Column(BigInteger, + db.ForeignKey(Device.id), + nullable=False) + device = db.relationship(Device, + backref=db.backref('devices', uselist=True, lazy=True), + lazy=True, + primaryjoin=Device.id == device_id) @property def url(self) -> urlutils.URL: @@ -81,19 +84,13 @@ class Proof(Thing): -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(as_uuid=True), ForeignKey(Trade.id), nullable=False) transfer = relationship(DisposeProduct, backref=backref("proof_transfer", lazy=True, + uselist=False, cascade=CASCADE_OWN), - uselist=False, primaryjoin=DisposeProduct.id == transfer_id) @@ -102,33 +99,55 @@ class ProofDataWipe(JoinedTableMixin, Proof): 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), + nullable=False, + default=lambda: g.user.ethereum_address) + proof_author = relationship(User, primaryjoin=lambda: ProofDataWipe.proof_author_id == User.ethereum_address) erasure_id = Column(UUID(as_uuid=True), ForeignKey(EraseBasic.id), nullable=False) erasure = relationship(EraseBasic, backref=backref('proof_datawipe', lazy=True, + uselist=False, cascade=CASCADE_OWN), primaryjoin=EraseBasic.id == erasure_id) class ProofFunction(JoinedTableMixin, Proof): disk_usage = Column(db.Integer, default=0) + proof_author_id = Column(CIText(), + db.ForeignKey(User.ethereum_address), + nullable=False, + default=lambda: g.user.ethereum_address) + proof_author = db.relationship(User, primaryjoin=lambda: ProofFunction.proof_author_id == User.ethereum_address) rate_id = Column(UUID(as_uuid=True), ForeignKey(Rate.id), nullable=False) rate = relationship(Rate, backref=backref('proof_function', lazy=True, + uselist=False, cascade=CASCADE_OWN), primaryjoin=Rate.id == rate_id) 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), + 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), + nullable=False) + receiver = db.relationship(User, primaryjoin=lambda: ProofReuse.receiver_id == User.ethereum_address) price = Column(db.Integer) class ProofRecycling(JoinedTableMixin, Proof): collection_point = Column(CIText(), default='', nullable=False) - date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) + date = Column(db.DateTime, nullable=False, default=datetime.utcnow) contact = Column(CIText(), default='', nullable=False) ticket = Column(CIText(), default='', nullable=False) gps_location = Column(CIText(), default='', nullable=False) recycler_code = Column(CIText(), default='', nullable=False) - diff --git a/ereuse_devicehub/resources/proof/schemas.py b/ereuse_devicehub/resources/proof/schemas.py index 1751dc20..3070448e 100644 --- a/ereuse_devicehub/resources/proof/schemas.py +++ b/ereuse_devicehub/resources/proof/schemas.py @@ -12,6 +12,7 @@ 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 +from ereuse_devicehub.resources.user import schemas as s_user class Proof(Thing): @@ -20,40 +21,40 @@ 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) + device = NestedOn(s_device.Device, only_query='id', required=True, data_key='deviceID') class ProofTransfer(Proof): __doc__ = m.ProofTransfer.__doc__ transfer = NestedOn(s_action.DisposeProduct, required=True, - data_key='transferID', only_query='id') class ProofDataWipe(Proof): __doc__ = m.ProofDataWipe.__doc__ - erasure_type = SanitizedStr(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') 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') rate = NestedOn(s_action.Rate, required=True, only_query='id', data_key='rateID') class ProofReuse(Proof): __doc__ = m.ProofReuse.__doc__ - price = Integer() + 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') + price = Integer(required=True) class ProofRecycling(Proof): From e78d62602b104729757e1d37ccedba2f0767ce2f Mon Sep 17 00:00:00 2001 From: yiorgos marinellis Date: Mon, 23 Mar 2020 18:49:22 +0100 Subject: [PATCH 11/16] Align ProofTransfer to Chapter 6 on specs, fixes #12 --- ereuse_devicehub/resources/proof/models.py | 17 ++++++++++------- ereuse_devicehub/resources/proof/schemas.py | 10 ++++++---- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/ereuse_devicehub/resources/proof/models.py b/ereuse_devicehub/resources/proof/models.py index ff7a252e..6f63404b 100644 --- a/ereuse_devicehub/resources/proof/models.py +++ b/ereuse_devicehub/resources/proof/models.py @@ -85,13 +85,16 @@ class Proof(Thing): class ProofTransfer(JoinedTableMixin, Proof): - transfer_id = Column(UUID(as_uuid=True), ForeignKey(Trade.id), nullable=False) - transfer = relationship(DisposeProduct, - backref=backref("proof_transfer", - lazy=True, - uselist=False, - cascade=CASCADE_OWN), - primaryjoin=DisposeProduct.id == transfer_id) + supplier_id = db.Column(CIText(), + db.ForeignKey(User.ethereum_address), + 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), + nullable=False) + receiver = db.relationship(User, primaryjoin=lambda: ProofTransfer.receiver_id == User.ethereum_address) + deposit = Column(db.Integer, default=0) class ProofDataWipe(JoinedTableMixin, Proof): diff --git a/ereuse_devicehub/resources/proof/schemas.py b/ereuse_devicehub/resources/proof/schemas.py index 3070448e..9726381a 100644 --- a/ereuse_devicehub/resources/proof/schemas.py +++ b/ereuse_devicehub/resources/proof/schemas.py @@ -1,5 +1,5 @@ from flask import current_app as app -from marshmallow import Schema as MarshmallowSchema, ValidationError, validates_schema +from marshmallow import Schema as MarshmallowSchema, ValidationError, fields as f, validates_schema from marshmallow.fields import Boolean, DateTime, Integer, Nested, String, UUID from marshmallow.validate import Length from sqlalchemy.util import OrderedSet @@ -26,9 +26,11 @@ class Proof(Thing): class ProofTransfer(Proof): __doc__ = m.ProofTransfer.__doc__ - transfer = NestedOn(s_action.DisposeProduct, - required=True, - only_query='id') + 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') class ProofDataWipe(Proof): From bd2d72ad69956b7c96595402ba40520d22971b97 Mon Sep 17 00:00:00 2001 From: yiorgos marinellis Date: Tue, 24 Mar 2020 22:34:53 +0100 Subject: [PATCH 12/16] 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 From edf2e39fc7c7ba1b05f60bd094955ea8bca7e38a Mon Sep 17 00:00:00 2001 From: yiorgos marinellis Date: Wed, 25 Mar 2020 15:38:29 +0100 Subject: [PATCH 13/16] Change proofAuthor to proofAuthorID and resolves #18 --- ereuse_devicehub/resources/proof/schemas.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ereuse_devicehub/resources/proof/schemas.py b/ereuse_devicehub/resources/proof/schemas.py index 581ad681..a07fc524 100644 --- a/ereuse_devicehub/resources/proof/schemas.py +++ b/ereuse_devicehub/resources/proof/schemas.py @@ -38,7 +38,7 @@ class ProofDataWipe(Proof): date = DateTime('iso', required=True) result = Boolean(required=True) proof_author_id = SanitizedStr(validate=f.validate.Length(max=STR_SIZE), - load_only=True, required=True, data_key='proofAuthor') + load_only=True, required=True, data_key='proofAuthorID') proof_author = NestedOn(s_user.User, dump_only=True) erasure = NestedOn(s_action.EraseBasic, only_query='id', data_key='erasureID') @@ -47,7 +47,7 @@ class ProofFunction(Proof): __doc__ = m.ProofFunction.__doc__ 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') + load_only=True, required=True, data_key='proofAuthorID') proof_author = NestedOn(s_user.User, dump_only=True) rate = NestedOn(s_action.Rate, required=True, only_query='id', data_key='rateID') From bc69d194f4040e0630095de88f7ca5d8b6b8ed48 Mon Sep 17 00:00:00 2001 From: yiorgos marinellis Date: Fri, 27 Mar 2020 11:48:16 +0100 Subject: [PATCH 14/16] Make supplier_id, receiver_id optional for ProofReuse, fixes #21 --- ereuse_devicehub/resources/proof/models.py | 8 +++++--- ereuse_devicehub/resources/proof/schemas.py | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/ereuse_devicehub/resources/proof/models.py b/ereuse_devicehub/resources/proof/models.py index ed8bd798..2562b446 100644 --- a/ereuse_devicehub/resources/proof/models.py +++ b/ereuse_devicehub/resources/proof/models.py @@ -137,12 +137,14 @@ class ProofReuse(JoinedTableMixin, Proof): id_receipt = Column(CIText(), default='', nullable=False) supplier_id = db.Column(UUID(as_uuid=True), db.ForeignKey(User.id), - nullable=False, - default=lambda: g.user.id) + # nullable=False, + # default=lambda: g.user.id) + nullable=True) 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) + # nullable=False) + nullable=True) 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 a07fc524..de18a41f 100644 --- a/ereuse_devicehub/resources/proof/schemas.py +++ b/ereuse_devicehub/resources/proof/schemas.py @@ -57,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_id = UUID(load_only=True, required=True, data_key='supplierID') - receiver_id = UUID(load_only=True, required=True, data_key='receiverID') + supplier_id = UUID(load_only=True, required=False, data_key='supplierID') + receiver_id = UUID(load_only=True, required=False, data_key='receiverID') price = Integer(required=True) From e78b1c5db344e60a18ed3d907b6c1de5f008fcee Mon Sep 17 00:00:00 2001 From: yiorgos marinellis Date: Wed, 1 Apr 2020 19:11:14 +0200 Subject: [PATCH 15/16] Change FOREIGN keys to User.ethereum_address to User.id, resolves #14 --- ereuse_devicehub/resources/device/models.py | 44 ++++++++++---------- ereuse_devicehub/resources/device/schemas.py | 6 +-- ereuse_devicehub/resources/lot/models.py | 12 +++--- ereuse_devicehub/resources/lot/schemas.py | 2 +- 4 files changed, 32 insertions(+), 32 deletions(-) diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index 68134b8e..912fc95d 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -52,22 +52,22 @@ class Device(Thing): """ type = Column(Unicode(STR_SM_SIZE), nullable=False) hid = Column(Unicode(), check_lower('hid'), unique=True) - hid.comment = """The Hardware ID (HID) is the unique ID traceability + hid.comment = """The Hardware ID (HID) is the unique ID traceability systems use to ID a device globally. This field is auto-generated from Devicehub using literal identifiers from the device, - so it can re-generated *offline*. + so it can re-generated *offline*. """ + HID_CONVERSION_DOC model = Column(Unicode, check_lower('model')) model.comment = """The model of the device in lower case. - + The model is the unambiguous, as technical as possible, denomination - for the product. This field, among others, is used to identify + for the product. This field, among others, is used to identify the product. """ manufacturer = Column(Unicode(), check_lower('manufacturer')) manufacturer.comment = """The normalized name of the manufacturer, in lower case. - + Although as of now Devicehub does not enforce normalization, users can choose a list of normalized manufacturer names from the own ``/manufacturers`` REST endpoint. @@ -76,7 +76,7 @@ class Device(Thing): serial_number.comment = """The serial number of the device in lower case.""" brand = db.Column(CIText()) brand.comment = """A naming for consumers. This field can represent - several models, so it can be ambiguous, and it is not used to + several models, so it can be ambiguous, and it is not used to identify the product. """ generation = db.Column(db.SmallInteger, check_range('generation', 0)) @@ -94,14 +94,14 @@ class Device(Thing): color = Column(ColorType) color.comment = """The predominant color of the device.""" production_date = Column(db.DateTime) - production_date.comment = """The date of production of the device. + production_date.comment = """The date of production of the device. This is timezone naive, as Workbench cannot report this data with timezone information. """ variant = Column(db.CIText()) variant.comment = """A variant or sub-model of the device.""" sku = db.Column(db.CIText()) - sku.comment = """The Stock Keeping Unit (SKU), i.e. a + sku.comment = """The Stock Keeping Unit (SKU), i.e. a merchant-specific identifier for a product or service. """ image = db.Column(db.URL) @@ -312,17 +312,17 @@ class DisplayMixin: size = Column(Float(decimal_return_scale=1), check_range('size', 2, 150), nullable=False) size.comment = """The size of the monitor in inches.""" technology = Column(DBEnum(DisplayTech)) - technology.comment = """The technology the monitor uses to display + technology.comment = """The technology the monitor uses to display the image. """ resolution_width = Column(SmallInteger, check_range('resolution_width', 10, 20000), nullable=False) - resolution_width.comment = """The maximum horizontal resolution the + resolution_width.comment = """The maximum horizontal resolution the monitor can natively support in pixels. """ resolution_height = Column(SmallInteger, check_range('resolution_height', 10, 20000), nullable=False) - resolution_height.comment = """The maximum vertical resolution the + resolution_height.comment = """The maximum vertical resolution the monitor can natively support in pixels. """ refresh_rate = Column(SmallInteger, check_range('refresh_rate', 10, 1000)) @@ -384,17 +384,17 @@ class Computer(Device): """ ethereum_address = Column(CIText(), unique=True, default=None) deposit = Column(Integer, check_range('deposit',min=0,max=100), default=0) - owner_address = db.Column(CIText(), - db.ForeignKey(User.ethereum_address), + owner_id = db.Column(UUID(as_uuid=True), + db.ForeignKey(User.id), nullable=False, - default=lambda: g.user.ethereum_address) - author = db.relationship(User, primaryjoin=owner_address == User.ethereum_address) + default=lambda: g.user.id) + author = db.relationship(User, primaryjoin=owner_id == User.id) transfer_state = db.Column(IntEnum(TransferState), default=TransferState.Initial, nullable=False) transfer_state.comment = TransferState.__doc__ - receiver_address = db.Column(CIText(), - db.ForeignKey(User.ethereum_address), + receiver_id = db.Column(UUID(as_uuid=True), + db.ForeignKey(User.id), nullable=True) - receiver = db.relationship(User, primaryjoin=receiver_address == User.ethereum_address) + receiver = db.relationship(User, primaryjoin=receiver_id == User.id) deliverynote_address = db.Column(CIText(), nullable=True) def __init__(self, chassis, **kwargs) -> None: @@ -502,11 +502,11 @@ class Mobile(Device): id = Column(BigInteger, ForeignKey(Device.id), primary_key=True) imei = Column(BigInteger) - imei.comment = """The International Mobile Equipment Identity of + imei.comment = """The International Mobile Equipment Identity of the smartphone as an integer. """ meid = Column(Unicode) - meid.comment = """The Mobile Equipment Identifier as a hexadecimal + meid.comment = """The Mobile Equipment Identifier as a hexadecimal string. """ ram_size = db.Column(db.Integer, check_range(1, )) @@ -639,7 +639,7 @@ class Motherboard(JoinedComponentTableMixin, Component): class NetworkMixin: speed = Column(SmallInteger, check_range('speed', min=10, max=10000)) - speed.comment = """The maximum speed this network adapter can handle, + speed.comment = """The maximum speed this network adapter can handle, in mbps. """ wireless = Column(Boolean, nullable=False, default=False) @@ -698,7 +698,7 @@ class Battery(JoinedComponentTableMixin, Component): technology = db.Column(db.Enum(BatteryTechnology)) size = db.Column(db.Integer, nullable=False) size.comment = """Maximum battery capacity by design, in mAh. - + Use BatteryTest's "size" to get the actual size of the battery. """ diff --git a/ereuse_devicehub/resources/device/schemas.py b/ereuse_devicehub/resources/device/schemas.py index 61c399ad..8a5ee08e 100644 --- a/ereuse_devicehub/resources/device/schemas.py +++ b/ereuse_devicehub/resources/device/schemas.py @@ -126,11 +126,11 @@ class Computer(Device): deposit = Integer(validate=f.validate.Range(min=0, max=100), description=m.Computer.deposit.__doc__) # author_id = NestedOn(s_user.User,only_query='author_id') - owner_address = SanitizedStr(validate=f.validate.Length(max=42)) + owner_id = UUID(data_key='ownerID') transfer_state = EnumField(enums.TransferState, description=m.Computer.transfer_state.comment) - receiver_address = SanitizedStr(validate=f.validate.Length(max=42)) + receiver_id = UUID(data_key='receiverID') deliverynote_address = SanitizedStr(validate=f.validate.Length(max=42)) - + class Desktop(Computer): __doc__ = m.Desktop.__doc__ diff --git a/ereuse_devicehub/resources/lot/models.py b/ereuse_devicehub/resources/lot/models.py index a26bbd53..5f8cbb8a 100644 --- a/ereuse_devicehub/resources/lot/models.py +++ b/ereuse_devicehub/resources/lot/models.py @@ -32,8 +32,8 @@ class Lot(Thing): lazy=True, collection_class=set) """The **children** devices that the lot has. - - Note that the lot can have more devices, if they are inside + + Note that the lot can have more devices, if they are inside descendant lots. """ parents = db.relationship(lambda: Lot, @@ -63,11 +63,11 @@ class Lot(Thing): descendants. """ deposit = db.Column(db.Integer, check_range('deposit', min=0, max=100), default=0) - owner_address = db.Column(CIText(), - db.ForeignKey(User.ethereum_address), + owner_id = db.Column(UUID(as_uuid=True), + db.ForeignKey(User.id), nullable=False, - default=lambda: g.user.ethereum_address) - owner = db.relationship(User, primaryjoin=owner_address == User.ethereum_address) + default=lambda: g.user.id) + owner = db.relationship(User, primaryjoin=owner_id == User.id) transfer_state = db.Column(IntEnum(TransferState), default=TransferState.Initial, nullable=False) transfer_state.comment = TransferState.__doc__ receiver_address = db.Column(CIText(), diff --git a/ereuse_devicehub/resources/lot/schemas.py b/ereuse_devicehub/resources/lot/schemas.py index f027622b..d62eb6c6 100644 --- a/ereuse_devicehub/resources/lot/schemas.py +++ b/ereuse_devicehub/resources/lot/schemas.py @@ -22,7 +22,7 @@ class Lot(Thing): deposit = f.Integer(validate=f.validate.Range(min=0, max=100), description=m.Lot.deposit.__doc__) # author_id = NestedOn(s_user.User,only_query='author_id') - owner_address = SanitizedStr(validate=f.validate.Length(max=42)) + owner_id = f.UUID(data_key='ownerID') transfer_state = EnumField(TransferState, description=m.Lot.transfer_state.comment) receiver_address = SanitizedStr(validate=f.validate.Length(max=42)) deliverynote = NestedOn(s_deliverynote.Deliverynote, dump_only=True) From 61fd9de4774e564c0cd466519bbccdc8506ae8b8 Mon Sep 17 00:00:00 2001 From: yiorgos marinellis Date: Wed, 1 Apr 2020 19:56:09 +0200 Subject: [PATCH 16/16] Remove caching when returning lots, devices, fixes UI issue 18 --- ereuse_devicehub/resources/device/views.py | 6 +++--- ereuse_devicehub/resources/lot/views.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ereuse_devicehub/resources/device/views.py b/ereuse_devicehub/resources/device/views.py index 1aaa1f93..2a7a5efd 100644 --- a/ereuse_devicehub/resources/device/views.py +++ b/ereuse_devicehub/resources/device/views.py @@ -92,7 +92,7 @@ class DeviceView(View): description: The device or devices. """ return super().get(id) - + def patch(self, id): dev = Device.query.filter_by(id=id).one() if isinstance(dev, Computer): @@ -109,7 +109,7 @@ class DeviceView(View): db.session.commit() return Response(status=204) raise ValueError('Cannot patch a non computer') - + def one(self, id: int): """Gets one device.""" if not request.authorization: @@ -127,7 +127,7 @@ class DeviceView(View): return self.schema.jsonify(device) @auth.Auth.requires_auth - @cache(datetime.timedelta(minutes=1)) + # @cache(datetime.timedelta(minutes=1)) def find(self, args: dict): """Gets many devices.""" # Compute query diff --git a/ereuse_devicehub/resources/lot/views.py b/ereuse_devicehub/resources/lot/views.py index 3e50070d..0a117c78 100644 --- a/ereuse_devicehub/resources/lot/views.py +++ b/ereuse_devicehub/resources/lot/views.py @@ -59,7 +59,7 @@ class LotView(View): lot = Lot.query.filter_by(id=id).one() # type: Lot return self.schema.jsonify(lot, nested=2) - @teal.cache.cache(datetime.timedelta(minutes=5)) + # @teal.cache.cache(datetime.timedelta(minutes=5)) def find(self, args: dict): """Gets lots.