datawipe form example as 2 forms in one

This commit is contained in:
Cayo Puigdefabregas 2022-02-08 13:59:50 +01:00
parent 2542719378
commit 764d749db0
1 changed files with 29 additions and 16 deletions

View File

@ -1,13 +1,16 @@
import json import json
import copy
from json.decoder import JSONDecodeError from json.decoder import JSONDecodeError
from flask import g, request 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.fields import FormField
from wtforms import (DateField, FloatField, HiddenField, IntegerField, from wtforms import (DateField, FloatField, HiddenField, IntegerField,
MultipleFileField, FileField, SelectField, StringField, MultipleFileField, FileField, SelectField, StringField,
TextAreaField, BooleanField, URLField, validators) TextAreaField, BooleanField, URLField, validators, Form)
from boltons.urlutils import URL
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.hash_reports import insert_hash
from ereuse_devicehub.resources.documents.models import DataWipeDocument from ereuse_devicehub.resources.documents.models import DataWipeDocument
@ -550,7 +553,9 @@ class AllocateForm(NewActionForm):
return is_valid return is_valid
class DataWipeForm(NewActionForm): class DataWipeDocumentForm(Form):
date = DateField(u'Date', [validators.Optional()],
description="Date when was data wipe")
url = URLField(u'Url', [validators.Optional()], url = URLField(u'Url', [validators.Optional()],
description="Url where the document resides") description="Url where the document resides")
success = BooleanField(u'Success', [validators.Optional()], success = BooleanField(u'Success', [validators.Optional()],
@ -568,24 +573,32 @@ class DataWipeForm(NewActionForm):
return is_valid return is_valid
def save(self): def save(self, commit=True):
file_name = '' file_name = ''
file_hash = '' file_hash = ''
if self.file_name.data: if self.file_name.data:
file_name = self.file_name.data.filename file_name = self.file_name.data.filename
file_hash = insert_hash(self.file_name.data.read(), commit=False) file_hash = insert_hash(self.file_name.data.read(), commit=False)
document = DataWipeDocument( self.url.data = URL(self.url.data)
date=self.date.data, self._obj = DataWipeDocument(
id_document=self.id_document.data, document_type='DataWipeDocument',
file_name=file_name,
file_hash=file_hash,
url=self.url.data,
software=self.software.data,
success=self.success.data
) )
self.populate_obj(self._obj)
self._obj.file_name = file_name
self._obj.file_hash = file_hash
db.session.add(self._obj)
if commit:
db.session.commit()
db.session.add(document) return self._obj
class DataWipeForm(NewActionForm):
document = FormField(DataWipeDocumentForm)
def save(self):
self.document.form.save(commit=False)
Model = db.Model._decl_class_registry.data[self.type.data]() Model = db.Model._decl_class_registry.data[self.type.data]()
self.instance = Model() self.instance = Model()
@ -593,16 +606,16 @@ class DataWipeForm(NewActionForm):
severity = self.severity.data severity = self.severity.data
self.devices.data = self._devices self.devices.data = self._devices
self.severity.data = Severity[self.severity.data] self.severity.data = Severity[self.severity.data]
self.instance.document = document
self.instance.file_name = file_name
# import pdb; pdb.set_trace() document = copy.copy(self.document)
#TODO AttributeError: can't set attribute del self.document
self.populate_obj(self.instance) self.populate_obj(self.instance)
self.instance.document = document.form._obj
db.session.add(self.instance) db.session.add(self.instance)
db.session.commit() db.session.commit()
self.devices.data = devices self.devices.data = devices
self.severity.data = severity self.severity.data = severity
self.document = document
return self.instance return self.instance