heredit datawipe document form general document model

This commit is contained in:
Cayo Puigdefabregas 2021-07-29 15:46:49 +02:00
parent 1811b81e93
commit 3119178c9c
6 changed files with 50 additions and 34 deletions

View File

@ -42,8 +42,6 @@ def upgrade():
sa.Column('type', sa.Unicode(), nullable=False), sa.Column('type', sa.Unicode(), nullable=False),
sa.Column('date', sa.TIMESTAMP(timezone=True), nullable=True), sa.Column('date', sa.TIMESTAMP(timezone=True), nullable=True),
sa.Column('id_document', sa.Unicode(), 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('owner_id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('file_name', sa.Unicode(), nullable=False), sa.Column('file_name', sa.Unicode(), nullable=False),
sa.Column('file_hash', 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()}') 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 # DataWipe table
op.create_table('data_wipe', op.create_table('data_wipe',
sa.Column('document_id', sa.BigInteger(), nullable=True), sa.Column('document_id', sa.BigInteger(), nullable=True),

View File

@ -1332,13 +1332,13 @@ class DataWipe(JoinedTableMixin, ActionWithMultipleDevices):
""" """
document_comment = """The user that gets the device due this deal.""" document_comment = """The user that gets the device due this deal."""
document_id = db.Column(BigInteger, document_id = db.Column(BigInteger,
db.ForeignKey('document.id'), db.ForeignKey('data_wipe_document.id'),
nullable=False) nullable=False)
document = db.relationship('Document', document = db.relationship('DataWipeDocument',
backref=backref('actions', backref=backref('actions',
lazy=True, lazy=True,
cascade=CASCADE_OWN), cascade=CASCADE_OWN),
primaryjoin='DataWipe.document_id == Document.id') primaryjoin='DataWipe.document_id == DataWipeDocument.id')
class Prepare(ActionWithMultipleDevices): class Prepare(ActionWithMultipleDevices):

View File

@ -433,7 +433,7 @@ class Prepare(ActionWithMultipleDevices):
class DataWipe(ActionWithMultipleDevices): class DataWipe(ActionWithMultipleDevices):
__doc__ = m.DataWipe.__doc__ __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): class Live(ActionWithOneDevice):

View File

@ -1,18 +1,10 @@
import copy 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.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.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.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 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']] [data.pop(x, None) for x in ['severity', 'devices', 'name', 'description']]
doc_data = schema.load(data) doc_data = schema.load(data)
doc_data['type'] = 'DataWipe' doc_data['type'] = 'DataWipe'
self.document = Document(**doc_data) self.document = DataWipeDocument(**doc_data)
db.session.add(self.document) db.session.add(self.document)
db_hash = ReportHash(hash3=self.document.file_hash) db_hash = ReportHash(hash3=self.document.file_hash)

View File

@ -1,6 +1,7 @@
from citext import CIText from citext import CIText
from flask import g 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 sqlalchemy.dialects.postgresql import UUID
from teal.db import URL from teal.db import URL
from ereuse_devicehub.db import db from ereuse_devicehub.db import db
@ -19,10 +20,6 @@ class Document(Thing):
date = Column(db.DateTime, nullable=True) date = Column(db.DateTime, nullable=True)
date.comment = """The date of document, some documents need to have one date 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 = Column(CIText(), nullable=False)
id_document.comment = """The id of one document like invoice so they can be linked.""" id_document.comment = """The id of one document like invoice so they can be linked."""
owner_id = db.Column(UUID(as_uuid=True), owner_id = db.Column(UUID(as_uuid=True),
@ -39,3 +36,22 @@ class Document(Thing):
def __str__(self) -> str: def __str__(self) -> str:
return '{0.file_name}'.format(self) 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)

View File

@ -2,27 +2,26 @@ from marshmallow.fields import DateTime, Integer, validate, Boolean
from teal.marshmallow import SanitizedStr, URL from teal.marshmallow import SanitizedStr, URL
from ereuse_devicehub.resources.schemas import Thing from ereuse_devicehub.resources.schemas import Thing
from ereuse_devicehub.resources.documents import models as m from ereuse_devicehub.resources.documents import models as m
# from marshmallow import ValidationError, validates_schema
class Document(Thing): class DataWipeDocument(Thing):
__doc__ = m.Document.__doc__ __doc__ = m.DataWipeDocument.__doc__
id = Integer(description=m.Document.id.comment, dump_only=True) id = Integer(description=m.DataWipeDocument.id.comment, dump_only=True)
type = SanitizedStr(default='Document') type = SanitizedStr(default='DataWipeDocument')
url = URL(description=m.Document.url.comment) url = URL(description=m.DataWipeDocument.url.comment)
success = Boolean(description=m.Document.success.comment) success = Boolean(description=m.DataWipeDocument.success.comment)
software = SanitizedStr(description=m.Document.software.comment) software = SanitizedStr(description=m.DataWipeDocument.software.comment)
date = DateTime(data_key='endTime', date = DateTime(data_key='endTime',
required=False, required=False,
description=m.Document.date.comment) description=m.DataWipeDocument.date.comment)
id_document = SanitizedStr(data_key='documentId', id_document = SanitizedStr(data_key='documentId',
default='', default='',
description=m.Document.id_document.comment) description=m.DataWipeDocument.id_document.comment)
file_name = SanitizedStr(data_key='filename', file_name = SanitizedStr(data_key='filename',
default='', default='',
description=m.Document.file_name.comment, description=m.DataWipeDocument.file_name.comment,
validate=validate.Length(max=100)) validate=validate.Length(max=100))
file_hash = SanitizedStr(data_key='hash', file_hash = SanitizedStr(data_key='hash',
default='', default='',
description=m.Document.file_hash.comment, description=m.DataWipeDocument.file_hash.comment,
validate=validate.Length(max=64)) validate=validate.Length(max=64))