From 0d9dccda47c97ab9186472d3066a1dad3f822fc6 Mon Sep 17 00:00:00 2001 From: yiorgos marinellis Date: Mon, 16 Mar 2020 00:26:20 +0100 Subject: [PATCH] First implementation of batch POST of ProofDataWipe --- ereuse_devicehub/resources/proof/models.py | 34 ++++++++++++--------- ereuse_devicehub/resources/proof/schemas.py | 4 +-- ereuse_devicehub/resources/proof/views.py | 12 +++++--- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/ereuse_devicehub/resources/proof/models.py b/ereuse_devicehub/resources/proof/models.py index 48f016f0..60e18ae3 100644 --- a/ereuse_devicehub/resources/proof/models.py +++ b/ereuse_devicehub/resources/proof/models.py @@ -49,10 +49,6 @@ class Proof(Thing): """The URL where to GET this proof.""" return urlutils.URL(url_for_resource(Proof, item_id=self.id)) - @property - def certificate(self) -> Optional[urlutils.URL]: - return None - # noinspection PyMethodParameters @declared_attr def __mapper_args__(cls): @@ -81,35 +77,43 @@ class Proof(Thing): class ProofTransfer(JoinedTableMixin, Proof): transfer_id = Column(UUID, ForeignKey(Trade.id), nullable=False) transfer = relationship(DisposeProduct, + backref=backref("proof_transfer", + lazy=True, + cascade=CASCADE_OWN), + uselist=False, primaryjoin=DisposeProduct.id == transfer_id) 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(UUID, ForeignKey(EraseBasic.id), 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.""" + erasure_id = Column(UUID(as_uuid=True), ForeignKey(EraseBasic.id), nullable=False) erasure = relationship(EraseBasic, - backref=backref('proofs_datawipe', + backref=backref('proof_datawipe', lazy=True, cascade=CASCADE_OWN), primaryjoin=EraseBasic.id == erasure_id) class ProofFunction(JoinedTableMixin, Proof): - disk_usage = db.Column(db.Integer, default=0) + disk_usage = Column(db.Integer, default=0) rate_id = Column(UUID, ForeignKey(Rate.id), nullable=False) rate = relationship(Rate, + backref=backref('proof_function', + lazy=True, + cascade=CASCADE_OWN), primaryjoin=Rate.id == rate_id) class ProofReuse(JoinedTableMixin, Proof): - price = db.Column(db.Integer) + price = Column(db.Integer) class ProofRecycling(JoinedTableMixin, Proof): - collection_point = Column(CIText()) + collection_point = Column(CIText(), default='', nullable=False) date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) - contact = Column(CIText()) - ticket = Column(CIText()) - gps_location = Column(CIText()) + contact = Column(CIText(), default='', nullable=False) + ticket = Column(CIText(), default='', nullable=False) + gps_location = Column(CIText(), default='', nullable=False) diff --git a/ereuse_devicehub/resources/proof/schemas.py b/ereuse_devicehub/resources/proof/schemas.py index 1aba739e..4859150a 100644 --- a/ereuse_devicehub/resources/proof/schemas.py +++ b/ereuse_devicehub/resources/proof/schemas.py @@ -31,9 +31,9 @@ class ProofTransfer(Proof): class ProofDataWipe(Proof): __doc__ = m.ProofDataWipe.__doc__ erasure_type = SanitizedStr(default='') - date = DateTime() + date = DateTime('iso', required=True) result = Boolean(missing=False) - erasure = NestedOn(s_action.EraseBasic, dump_only=True, only_query='id') + erasure = NestedOn(s_action.EraseBasic, only_query='id') class ProofFunction(Proof): diff --git a/ereuse_devicehub/resources/proof/views.py b/ereuse_devicehub/resources/proof/views.py index 2485a6a2..c645479d 100644 --- a/ereuse_devicehub/resources/proof/views.py +++ b/ereuse_devicehub/resources/proof/views.py @@ -2,12 +2,13 @@ from distutils.version import StrictVersion from typing import List from uuid import UUID -from flask import current_app as app, request +from flask import current_app as app, request, jsonify from sqlalchemy.util import OrderedSet from teal.marshmallow import ValidationError from teal.resource import View from ereuse_devicehub.db import db +from ereuse_devicehub.query import things_response from ereuse_devicehub.resources.action.models import Action, RateComputer, Snapshot, VisualTest from ereuse_devicehub.resources.action.rate.v1_0 import CannotRate from ereuse_devicehub.resources.device.models import Component, Computer @@ -34,6 +35,9 @@ class ProofView(View): db.session.add(proof) proofs.append(self.schema.dump(proof)) db.session.commit() - ret = self.schema.jsonify(proofs) - ret.status_code = 201 - return ret + response = jsonify({ + 'items': proofs, + 'url': request.path + }) + response.status_code = 201 + return response