2021-07-21 06:35:18 +00:00
|
|
|
import copy
|
|
|
|
|
|
|
|
from ereuse_devicehub.db import db
|
2021-07-29 10:45:43 +00:00
|
|
|
from ereuse_devicehub.resources.action.models import DataWipe
|
2021-07-29 13:46:49 +00:00
|
|
|
from ereuse_devicehub.resources.documents.models import DataWipeDocument
|
2021-08-10 10:45:03 +00:00
|
|
|
from ereuse_devicehub.resources.device.models import DataStorage
|
2021-07-29 13:46:49 +00:00
|
|
|
from ereuse_devicehub.resources.documents.schemas import DataWipeDocument as sh_document
|
2021-07-28 13:59:11 +00:00
|
|
|
from ereuse_devicehub.resources.hash_reports import ReportHash
|
2021-07-21 06:35:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ErasedView():
|
2021-07-26 09:33:11 +00:00
|
|
|
"""Handler for manager the action register for add to a device one proof of erase
|
2021-07-21 06:35:18 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, data, schema):
|
|
|
|
self.schema = schema
|
|
|
|
self.insert_document(copy.copy(data))
|
|
|
|
self.insert_action(copy.copy(data))
|
|
|
|
|
|
|
|
def post(self):
|
|
|
|
db.session().final_flush()
|
2021-07-26 09:33:11 +00:00
|
|
|
from flask import jsonify
|
|
|
|
ret = jsonify(self.erased)
|
2021-07-21 06:35:18 +00:00
|
|
|
ret.status_code = 201
|
|
|
|
db.session.commit()
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def insert_document(self, data):
|
|
|
|
schema = sh_document()
|
2021-07-26 09:33:11 +00:00
|
|
|
[data.pop(x, None) for x in ['severity', 'devices', 'name', 'description']]
|
2021-07-21 06:35:18 +00:00
|
|
|
doc_data = schema.load(data)
|
2021-08-10 10:45:03 +00:00
|
|
|
# doc_data['type'] = 'DataWipe'
|
2021-07-29 13:46:49 +00:00
|
|
|
self.document = DataWipeDocument(**doc_data)
|
2021-07-21 06:35:18 +00:00
|
|
|
db.session.add(self.document)
|
2021-08-10 10:45:03 +00:00
|
|
|
|
2021-07-28 13:59:11 +00:00
|
|
|
db_hash = ReportHash(hash3=self.document.file_hash)
|
|
|
|
db.session.add(db_hash)
|
2021-07-21 06:35:18 +00:00
|
|
|
|
|
|
|
def insert_action(self, data):
|
2021-07-29 12:54:58 +00:00
|
|
|
[data.pop(x, None) for x in ['url', 'documentId', 'filename', 'hash', 'software', 'success']]
|
2021-07-26 09:33:11 +00:00
|
|
|
self.data = self.schema.load(data)
|
2021-08-10 10:45:03 +00:00
|
|
|
|
2021-07-26 09:33:11 +00:00
|
|
|
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
|
2021-07-29 10:45:43 +00:00
|
|
|
self.erased = DataWipe(**self.data)
|
2021-07-21 06:35:18 +00:00
|
|
|
db.session.add(self.erased)
|