From 833b55aefcfe0b942fa062c60453b508a873b287 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 26 Jul 2021 11:33:11 +0200 Subject: [PATCH] external document erase --- .../versions/7ecb8ff7abad_documents.py | 2 +- ereuse_devicehub/resources/action/models.py | 23 +++------ ereuse_devicehub/resources/action/schemas.py | 4 +- .../resources/action/views/documents.py | 49 ++++++++----------- .../resources/documents/models.py | 14 +----- .../resources/documents/schemas.py | 18 +++---- 6 files changed, 41 insertions(+), 69 deletions(-) diff --git a/ereuse_devicehub/migrations/versions/7ecb8ff7abad_documents.py b/ereuse_devicehub/migrations/versions/7ecb8ff7abad_documents.py index 9b018f04..deaed24e 100644 --- a/ereuse_devicehub/migrations/versions/7ecb8ff7abad_documents.py +++ b/ereuse_devicehub/migrations/versions/7ecb8ff7abad_documents.py @@ -59,7 +59,7 @@ def upgrade(): # ToErased table op.create_table('to_erased', - sa.Column('document_id', sa.BigInteger(), nullable=False), + sa.Column('document_id', sa.BigInteger(), nullable=True), sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), sa.ForeignKeyConstraint(['document_id'], [f'{get_inv()}.document.id'], ), sa.ForeignKeyConstraint(['id'], [f'{get_inv()}.action.id'], ), diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index 81e08074..30a91890 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -1327,26 +1327,15 @@ class ToPrepare(ActionWithMultipleDevices): pass -class ToErased(ActionWithMultipleDevices): +class ToErased(JoinedTableMixin, ActionWithMultipleDevices): """The device has been selected for insert one proof of erease disk. """ document_comment = """The user that gets the device due this deal.""" - # document_id = db.Column(BigInteger, - # db.ForeignKey('document.id'), - # nullable=False) - # document = db.relationship('EraseDocument', - # backref=backref('actions', - # # lazy=True, - # # uselist=False, - # # cascade=CASCADE_OWN), - # uselist=True, - # lazy=True, - # order_by=lambda: Action.end_time, - # collection_class=list), - # primaryjoin='ToErased.document_id == EraseDocument.id') - document_id = Column(BigInteger, ForeignKey('document.id'), nullable=False) - document = relationship('Document', - backref=backref('document_one', + document_id = db.Column(BigInteger, + db.ForeignKey('document.id'), + nullable=False) + document = db.relationship('Document', + backref=backref('actions', lazy=True, cascade=CASCADE_OWN), primaryjoin='ToErased.document_id == Document.id') diff --git a/ereuse_devicehub/resources/action/schemas.py b/ereuse_devicehub/resources/action/schemas.py index 1c2eff1b..1a498a36 100644 --- a/ereuse_devicehub/resources/action/schemas.py +++ b/ereuse_devicehub/resources/action/schemas.py @@ -17,6 +17,7 @@ from ereuse_devicehub.resources.action import models as m from ereuse_devicehub.resources.agent import schemas as s_agent from ereuse_devicehub.resources.device import schemas as s_device from ereuse_devicehub.resources.tradedocument import schemas as s_document +from ereuse_devicehub.resources.documents import schemas as s_generic_document from ereuse_devicehub.resources.enums import AppearanceRange, BiosAccessRange, FunctionalityRange, \ PhysicalErasureMethod, R_POSITIVE, RatingRange, \ Severity, SnapshotSoftware, TestDataStorageLength @@ -432,7 +433,8 @@ class Prepare(ActionWithMultipleDevices): class ToErased(ActionWithMultipleDevices): __doc__ = m.ToErased.__doc__ - document = NestedOn('Document', only_query='id') + document = NestedOn(s_generic_document.Document, only_query='id') + # document = NestedOn('EraseDocument') # device = NestedOn(s_device.Device, only_query='id') diff --git a/ereuse_devicehub/resources/action/views/documents.py b/ereuse_devicehub/resources/action/views/documents.py index bb5c03ec..65c20857 100644 --- a/ereuse_devicehub/resources/action/views/documents.py +++ b/ereuse_devicehub/resources/action/views/documents.py @@ -10,25 +10,13 @@ from ereuse_devicehub.resources.action.models import (Trade, Confirm, ConfirmRev ConfirmRevokeDocument) from ereuse_devicehub.resources.user.models import User from ereuse_devicehub.resources.action.models import ToErased -from ereuse_devicehub.resources.documents.models import EraseDocument -from ereuse_devicehub.resources.documents.schemas import EraseDocument as sh_document +from ereuse_devicehub.resources.documents.models import Document +from ereuse_devicehub.resources.device.models import DataStorage +from ereuse_devicehub.resources.documents.schemas import Document as sh_document class ErasedView(): - """Handler for manager the trade action register from post - - request_post = { - 'type': 'Trade', - 'devices': [device_id], - 'documents': [document_id], - 'userFrom': user2.email, - 'userTo': user.email, - 'price': 10, - 'date': "2020-12-01T02:00:00+00:00", - 'lot': lot['id'], - 'confirm': True, - } - + """Handler for manager the action register for add to a device one proof of erase """ def __init__(self, data, schema): @@ -37,30 +25,33 @@ class ErasedView(): self.insert_action(copy.copy(data)) def post(self): - # import pdb; pdb.set_trace() db.session().final_flush() - ret = self.schema.jsonify(self.erased) + from flask import jsonify + ret = jsonify(self.erased) ret.status_code = 201 db.session.commit() return ret def insert_document(self, data): - # import pdb; pdb.set_trace() schema = sh_document() - [data.pop(x) for x in ['severity', 'devices', 'name', 'description']] + [data.pop(x, None) for x in ['severity', 'devices', 'name', 'description']] doc_data = schema.load(data) doc_data['type'] = 'ToErased' - self.document = EraseDocument(**doc_data) + self.document = Document(**doc_data) db.session.add(self.document) - # db.session.commit() def insert_action(self, data): - import pdb; pdb.set_trace() [data.pop(x, None) for x in ['url', 'documentId', 'filename', 'hash']] - # self.data = self.schema.load(data) - # self.data['document_id'] = self.document.id - # self.data['document'] = self.document - # data['document_id'] = self.document.id - data['document'] = self.document - self.erased = ToErased(**data) + self.data = self.schema.load(data) + + for dev in self.data['devices']: + if not hasattr(dev, 'components'): + continue + + for component in dev.components: + if isinstance(component, DataStorage): + self.data['devices'].add(component) + + self.data['document'] = self.document + self.erased = ToErased(**self.data) db.session.add(self.erased) diff --git a/ereuse_devicehub/resources/documents/models.py b/ereuse_devicehub/resources/documents/models.py index 1dbee994..7ee5eb99 100644 --- a/ereuse_devicehub/resources/documents/models.py +++ b/ereuse_devicehub/resources/documents/models.py @@ -1,14 +1,12 @@ from citext import CIText from flask import g - +from sqlalchemy import BigInteger, Column, Sequence, Unicode from sqlalchemy.dialects.postgresql import UUID +from teal.db import URL from ereuse_devicehub.db import db from ereuse_devicehub.resources.user.models import User from ereuse_devicehub.resources.models import Thing, STR_SM_SIZE -from sqlalchemy import BigInteger, Column, Sequence, Unicode -from teal.db import URL - class Document(Thing): """This represent a generic document.""" @@ -37,11 +35,3 @@ class Document(Thing): def __str__(self) -> str: return '{0.file_name}'.format(self) - - -class EraseDocument(Document): - """This represent a document involved in a erase manual action. - This represent the proof. - - """ - pass diff --git a/ereuse_devicehub/resources/documents/schemas.py b/ereuse_devicehub/resources/documents/schemas.py index 636caf56..21b7017e 100644 --- a/ereuse_devicehub/resources/documents/schemas.py +++ b/ereuse_devicehub/resources/documents/schemas.py @@ -5,22 +5,22 @@ from ereuse_devicehub.resources.documents import models as m # from marshmallow import ValidationError, validates_schema -class EraseDocument(Thing): - __doc__ = m.EraseDocument.__doc__ - id = Integer(description=m.EraseDocument.id.comment, dump_only=True) - type = SanitizedStr(default='EraseDocument') +class Document(Thing): + __doc__ = m.Document.__doc__ + id = Integer(description=m.Document.id.comment, dump_only=True) + type = SanitizedStr(default='Document') date = DateTime(data_key='endTime', required=False, - description=m.EraseDocument.date.comment) + description=m.Document.date.comment) id_document = SanitizedStr(data_key='documentId', default='', - description=m.EraseDocument.id_document.comment) + description=m.Document.id_document.comment) file_name = SanitizedStr(data_key='filename', default='', - description=m.EraseDocument.file_name.comment, + description=m.Document.file_name.comment, validate=validate.Length(max=100)) file_hash = SanitizedStr(data_key='hash', default='', - description=m.EraseDocument.file_hash.comment, + description=m.Document.file_hash.comment, validate=validate.Length(max=64)) - url = URL(description=m.EraseDocument.url.comment) + url = URL(description=m.Document.url.comment)