add form for data wipe

This commit is contained in:
Cayo Puigdefabregas 2022-02-07 14:02:28 +01:00
parent 94604dd029
commit 2a92b1f747
1 changed files with 67 additions and 17 deletions

View File

@ -5,12 +5,15 @@ from flask import g, request
from flask_wtf import FlaskForm from flask_wtf import FlaskForm
from sqlalchemy.util import OrderedSet from sqlalchemy.util import OrderedSet
from wtforms import (DateField, FloatField, HiddenField, IntegerField, from wtforms import (DateField, FloatField, HiddenField, IntegerField,
MultipleFileField, SelectField, StringField, MultipleFileField, FileField, SelectField, StringField,
TextAreaField, validators) TextAreaField, BooleanField, URLField, validators)
from ereuse_devicehub.db import db from ereuse_devicehub.db import db
from ereuse_devicehub.resources.hash_reports import insert_hash
from ereuse_devicehub.resources.documents.models import DataWipeDocument
from ereuse_devicehub.resources.action.models import (Action, RateComputer, from ereuse_devicehub.resources.action.models import (Action, RateComputer,
Snapshot, VisualTest) Snapshot, VisualTest,
DataWipe)
from ereuse_devicehub.resources.action.rate.v1_0 import CannotRate from ereuse_devicehub.resources.action.rate.v1_0 import CannotRate
from ereuse_devicehub.resources.action.schemas import \ from ereuse_devicehub.resources.action.schemas import \
Snapshot as SnapshotSchema Snapshot as SnapshotSchema
@ -472,10 +475,18 @@ class TagDeviceForm(FlaskForm):
class NewActionForm(FlaskForm): class NewActionForm(FlaskForm):
name = StringField(u'Name', [validators.length(max=50)]) name = StringField(u'Name', [validators.length(max=50)],
description="A name or title of the event. Something to look for.")
devices = HiddenField() devices = HiddenField()
date = DateField(u'Date', validators=(validators.Optional(),)) date = DateField(u'Date', [validators.Optional()],
severity = SelectField(u'Severity', choices=[(v.name, v.name) for v in Severity]) description="""When the action ends. For some actions like booking
the time when it expires, for others like renting the
time that the end rents. For specific actions, it is the
time in which they are carried out; differs from created
in that created is where the system receives the action.""")
severity = SelectField(u'Severity', choices=[(v.name, v.name) for v in Severity],
description="""An indicator that evaluates the execution of the event.
For example, failed events are set to Error""")
description = TextAreaField(u'Description') description = TextAreaField(u'Description')
lot = HiddenField() lot = HiddenField()
type = HiddenField() type = HiddenField()
@ -540,19 +551,58 @@ class AllocateForm(NewActionForm):
class DataWipeForm(NewActionForm): class DataWipeForm(NewActionForm):
url = URL(required= False, ) url = URLField(u'Url', [validators.Optional()],
success = Boolean(required=False, default=False) description="Url where the document resides")
software = StringField() success = BooleanField(u'Success', [validators.Optional()],
date = DateField(u'endTime', description="The erase was success or not?")
required=False, software = StringField(u'Software', [validators.Optional()],
id_document = StringField(u'documentId', description="Which software has you use for erase the disks")
file_name = StringField(u'filename', id_document = StringField(u'Document Id', [validators.Optional()],
validate=validate.Length(max=100)) description="Identification number of document")
file_hash = StringField(u'hash', file_name = FileField(u'File', [validators.DataRequired()],
default='', description="""This file is not stored on our servers, it is only used to
validate=validate.Length(max=64)) generate a digital signature and obtain the name of the file.""")
def validate(self, extra_validators=None): def validate(self, extra_validators=None):
is_valid = super().validate(extra_validators) is_valid = super().validate(extra_validators)
return is_valid return is_valid
def save(self):
file_name = ''
file_hash = ''
if self.file_name.data:
file_name = self.file_name.data.filename
file_hash = insert_hash(self.file_name.data.read(), commit=False)
document = DataWipeDocument(
date=self.date.data,
id_document=self.id_document.data,
file_name=file_name,
file_hash=file_hash,
url=self.url.data,
software=self.software.data,
success=self.success.data
)
db.session.add(document)
Model = db.Model._decl_class_registry.data[self.type.data]()
self.instance = Model()
devices = self.devices.data
severity = self.severity.data
self.devices.data = self._devices
self.severity.data = Severity[self.severity.data]
self.instance.document = document
self.instance.file_name = file_name
# import pdb; pdb.set_trace()
#TODO AttributeError: can't set attribute
self.populate_obj(self.instance)
db.session.add(self.instance)
db.session.commit()
self.devices.data = devices
self.severity.data = severity
return self.instance