diff --git a/ereuse_devicehub/migrations/versions/7ecb8ff7abad_documents.py b/ereuse_devicehub/migrations/versions/7ecb8ff7abad_documents.py index 88d9ebe8..7f99a5b3 100644 --- a/ereuse_devicehub/migrations/versions/7ecb8ff7abad_documents.py +++ b/ereuse_devicehub/migrations/versions/7ecb8ff7abad_documents.py @@ -42,8 +42,6 @@ def upgrade(): sa.Column('type', sa.Unicode(), nullable=False), sa.Column('date', sa.TIMESTAMP(timezone=True), nullable=True), sa.Column('id_document', sa.Unicode(), nullable=True), - sa.Column('software', sa.Unicode(), nullable=True), - sa.Column('success', sa.Boolean(), nullable=False), sa.Column('owner_id', postgresql.UUID(as_uuid=True), nullable=False), sa.Column('file_name', sa.Unicode(), nullable=False), sa.Column('file_hash', sa.Unicode(), nullable=False), @@ -59,6 +57,17 @@ def upgrade(): op.create_index('document_type_index', 'document', ['type'], unique=False, postgresql_using='hash', schema=f'{get_inv()}') + # DataWipeDocument table + op.create_table('data_wipe_document', + sa.Column('id', sa.BigInteger(), nullable=False), + sa.Column('software', sa.Unicode(), nullable=True), + sa.Column('success', sa.Boolean(), nullable=False), + sa.ForeignKeyConstraint(['id'], [f'{get_inv()}.document.id'], ), + sa.PrimaryKeyConstraint('id'), + schema=f'{get_inv()}' + ) + + # DataWipe table op.create_table('data_wipe', sa.Column('document_id', sa.BigInteger(), nullable=True), diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index e29c37a2..20bd5819 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -1332,13 +1332,13 @@ class DataWipe(JoinedTableMixin, ActionWithMultipleDevices): """ document_comment = """The user that gets the device due this deal.""" document_id = db.Column(BigInteger, - db.ForeignKey('document.id'), + db.ForeignKey('data_wipe_document.id'), nullable=False) - document = db.relationship('Document', + document = db.relationship('DataWipeDocument', backref=backref('actions', lazy=True, cascade=CASCADE_OWN), - primaryjoin='DataWipe.document_id == Document.id') + primaryjoin='DataWipe.document_id == DataWipeDocument.id') class Prepare(ActionWithMultipleDevices): diff --git a/ereuse_devicehub/resources/action/schemas.py b/ereuse_devicehub/resources/action/schemas.py index f0a2e895..3232f11b 100644 --- a/ereuse_devicehub/resources/action/schemas.py +++ b/ereuse_devicehub/resources/action/schemas.py @@ -433,7 +433,7 @@ class Prepare(ActionWithMultipleDevices): class DataWipe(ActionWithMultipleDevices): __doc__ = m.DataWipe.__doc__ - document = NestedOn(s_generic_document.Document, only_query='id') + document = NestedOn(s_generic_document.DataWipeDocument, only_query='id') class Live(ActionWithOneDevice): diff --git a/ereuse_devicehub/resources/action/views/documents.py b/ereuse_devicehub/resources/action/views/documents.py index 19d40bb8..3f3aaa84 100644 --- a/ereuse_devicehub/resources/action/views/documents.py +++ b/ereuse_devicehub/resources/action/views/documents.py @@ -1,18 +1,10 @@ import copy -from flask import g -from sqlalchemy.util import OrderedSet -from teal.marshmallow import ValidationError - from ereuse_devicehub.db import db -from ereuse_devicehub.resources.action.models import (Trade, Confirm, ConfirmRevoke, - Revoke, RevokeDocument, ConfirmDocument, - ConfirmRevokeDocument) -from ereuse_devicehub.resources.user.models import User from ereuse_devicehub.resources.action.models import DataWipe -from ereuse_devicehub.resources.documents.models import Document +from ereuse_devicehub.resources.documents.models import DataWipeDocument from ereuse_devicehub.resources.device.models import DataStorage -from ereuse_devicehub.resources.documents.schemas import Document as sh_document +from ereuse_devicehub.resources.documents.schemas import DataWipeDocument as sh_document from ereuse_devicehub.resources.hash_reports import ReportHash @@ -38,7 +30,7 @@ class ErasedView(): [data.pop(x, None) for x in ['severity', 'devices', 'name', 'description']] doc_data = schema.load(data) doc_data['type'] = 'DataWipe' - self.document = Document(**doc_data) + self.document = DataWipeDocument(**doc_data) db.session.add(self.document) db_hash = ReportHash(hash3=self.document.file_hash) diff --git a/ereuse_devicehub/resources/documents/models.py b/ereuse_devicehub/resources/documents/models.py index 46be23e8..85aeb03f 100644 --- a/ereuse_devicehub/resources/documents/models.py +++ b/ereuse_devicehub/resources/documents/models.py @@ -1,6 +1,7 @@ from citext import CIText from flask import g -from sqlalchemy import BigInteger, Column, Sequence, Unicode, Boolean +from sqlalchemy import BigInteger, Column, Sequence, Unicode, Boolean, ForeignKey +from sqlalchemy.ext.declarative import declared_attr from sqlalchemy.dialects.postgresql import UUID from teal.db import URL from ereuse_devicehub.db import db @@ -19,10 +20,6 @@ class Document(Thing): date = Column(db.DateTime, nullable=True) date.comment = """The date of document, some documents need to have one date """ - software = Column(CIText(), nullable=False) - software.comment = """Which software is used""" - success = Column(Boolean) - success.comment = """If the erase was success""" id_document = Column(CIText(), nullable=False) id_document.comment = """The id of one document like invoice so they can be linked.""" owner_id = db.Column(UUID(as_uuid=True), @@ -39,3 +36,22 @@ class Document(Thing): def __str__(self) -> str: return '{0.file_name}'.format(self) + + +class JoinedTableMixin: + # noinspection PyMethodParameters + @declared_attr + def id(cls): + return Column(BigInteger, ForeignKey(Document.id), primary_key=True) + + +class DataWipeDocument(JoinedTableMixin, Document): + """This represent a generic document.""" + + software = Column(CIText(), nullable=False) + software.comment = """Which software is used""" + success = Column(Boolean) + success.comment = """If the erase was success""" + + def __str__(self) -> str: + return '{0.file_name}'.format(self) diff --git a/ereuse_devicehub/resources/documents/schemas.py b/ereuse_devicehub/resources/documents/schemas.py index 7b15103a..23db2fa2 100644 --- a/ereuse_devicehub/resources/documents/schemas.py +++ b/ereuse_devicehub/resources/documents/schemas.py @@ -2,27 +2,26 @@ from marshmallow.fields import DateTime, Integer, validate, Boolean from teal.marshmallow import SanitizedStr, URL from ereuse_devicehub.resources.schemas import Thing from ereuse_devicehub.resources.documents import models as m -# from marshmallow import ValidationError, validates_schema -class Document(Thing): - __doc__ = m.Document.__doc__ - id = Integer(description=m.Document.id.comment, dump_only=True) - type = SanitizedStr(default='Document') - url = URL(description=m.Document.url.comment) - success = Boolean(description=m.Document.success.comment) - software = SanitizedStr(description=m.Document.software.comment) +class DataWipeDocument(Thing): + __doc__ = m.DataWipeDocument.__doc__ + id = Integer(description=m.DataWipeDocument.id.comment, dump_only=True) + type = SanitizedStr(default='DataWipeDocument') + url = URL(description=m.DataWipeDocument.url.comment) + success = Boolean(description=m.DataWipeDocument.success.comment) + software = SanitizedStr(description=m.DataWipeDocument.software.comment) date = DateTime(data_key='endTime', required=False, - description=m.Document.date.comment) + description=m.DataWipeDocument.date.comment) id_document = SanitizedStr(data_key='documentId', default='', - description=m.Document.id_document.comment) + description=m.DataWipeDocument.id_document.comment) file_name = SanitizedStr(data_key='filename', default='', - description=m.Document.file_name.comment, + description=m.DataWipeDocument.file_name.comment, validate=validate.Length(max=100)) file_hash = SanitizedStr(data_key='hash', default='', - description=m.Document.file_hash.comment, + description=m.DataWipeDocument.file_hash.comment, validate=validate.Length(max=64))