2024-07-30 17:38:04 +00:00
|
|
|
import json
|
2024-08-05 11:32:52 +00:00
|
|
|
import pandas as pd
|
2024-07-13 13:27:36 +00:00
|
|
|
|
2024-07-30 17:38:04 +00:00
|
|
|
from django import forms
|
2024-07-31 11:28:02 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2024-07-30 17:38:04 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2024-08-05 13:07:29 +00:00
|
|
|
from utils.device import create_annotation, create_doc, create_index
|
2024-07-31 11:28:02 +00:00
|
|
|
from utils.forms import MultipleFileField
|
2024-08-05 11:32:52 +00:00
|
|
|
from device.models import Device
|
2024-07-30 17:38:04 +00:00
|
|
|
from evidence.parse import Build
|
2024-07-31 11:28:02 +00:00
|
|
|
from evidence.models import Annotation
|
2024-10-15 14:58:51 +00:00
|
|
|
from utils.save_snapshots import move_json, save_in_disk
|
2024-07-13 13:27:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UploadForm(forms.Form):
|
2024-07-31 11:28:02 +00:00
|
|
|
evidence_file = MultipleFileField(label=_("File"))
|
2024-07-13 13:27:36 +00:00
|
|
|
|
|
|
|
def clean(self):
|
2024-07-31 11:28:02 +00:00
|
|
|
self.evidences = []
|
2024-07-30 17:38:04 +00:00
|
|
|
data = self.cleaned_data.get('evidence_file')
|
|
|
|
if not data:
|
|
|
|
return False
|
|
|
|
|
2024-07-31 11:28:02 +00:00
|
|
|
for f in data:
|
|
|
|
file_name = f.name
|
|
|
|
file_data = f.read()
|
|
|
|
if not file_name or not file_data:
|
|
|
|
return False
|
|
|
|
|
|
|
|
try:
|
|
|
|
file_json = json.loads(file_data)
|
|
|
|
Build(file_json, None, check=True)
|
|
|
|
exist_annotation = Annotation.objects.filter(
|
|
|
|
uuid=file_json['uuid']
|
|
|
|
).first()
|
|
|
|
|
|
|
|
if exist_annotation:
|
|
|
|
raise ValidationError("error: {} exist".format(file_name))
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
raise ValidationError("error in: {}".format(file_name))
|
|
|
|
|
|
|
|
self.evidences.append((file_name, file_json))
|
2024-07-30 17:38:04 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def save(self, user, commit=True):
|
|
|
|
if not commit or not user:
|
|
|
|
return
|
|
|
|
|
2024-07-31 11:28:02 +00:00
|
|
|
for ev in self.evidences:
|
2024-10-15 14:58:51 +00:00
|
|
|
path_name = save_in_disk(ev[1], user.institution.name)
|
2024-07-31 11:28:02 +00:00
|
|
|
Build(ev[1], user)
|
2024-10-15 14:58:51 +00:00
|
|
|
move_json(path_name, user.institution.name)
|
2024-08-01 12:33:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserTagForm(forms.Form):
|
|
|
|
tag = forms.CharField(label=_("Tag"))
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2024-10-03 10:49:07 +00:00
|
|
|
self.pk = None
|
2024-08-01 12:33:09 +00:00
|
|
|
self.uuid = kwargs.pop('uuid', None)
|
2024-10-04 15:32:53 +00:00
|
|
|
self.user = kwargs.pop('user')
|
2024-10-03 10:49:07 +00:00
|
|
|
instance = Annotation.objects.filter(
|
|
|
|
uuid=self.uuid,
|
|
|
|
type=Annotation.Type.SYSTEM,
|
2024-10-04 15:32:53 +00:00
|
|
|
key='CUSTOM_ID',
|
|
|
|
owner=self.user.institution
|
2024-10-03 10:49:07 +00:00
|
|
|
).first()
|
|
|
|
|
|
|
|
if instance:
|
|
|
|
kwargs["initial"]["tag"] = instance.value
|
|
|
|
self.pk = instance.pk
|
|
|
|
|
2024-08-01 12:33:09 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
data = self.cleaned_data.get('tag')
|
|
|
|
if not data:
|
|
|
|
return False
|
|
|
|
self.tag = data
|
2024-10-03 10:49:07 +00:00
|
|
|
self.instance = Annotation.objects.filter(
|
|
|
|
uuid=self.uuid,
|
|
|
|
type=Annotation.Type.SYSTEM,
|
2024-10-04 15:32:53 +00:00
|
|
|
key='CUSTOM_ID',
|
|
|
|
owner=self.user.institution
|
2024-10-03 10:49:07 +00:00
|
|
|
).first()
|
|
|
|
|
2024-08-01 12:33:09 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def save(self, user, commit=True):
|
|
|
|
if not commit:
|
|
|
|
return
|
|
|
|
|
2024-10-03 10:49:07 +00:00
|
|
|
if self.instance:
|
|
|
|
if not self.tag:
|
|
|
|
self.instance.delete()
|
|
|
|
self.instance.value = self.tag
|
|
|
|
self.instance.save()
|
|
|
|
return
|
|
|
|
|
2024-08-01 12:33:09 +00:00
|
|
|
Annotation.objects.create(
|
|
|
|
uuid=self.uuid,
|
|
|
|
type=Annotation.Type.SYSTEM,
|
|
|
|
key='CUSTOM_ID',
|
2024-10-04 15:32:53 +00:00
|
|
|
value=self.tag,
|
|
|
|
owner=self.user.institution,
|
|
|
|
user=self.user
|
2024-08-01 12:33:09 +00:00
|
|
|
)
|
2024-08-05 11:32:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ImportForm(forms.Form):
|
|
|
|
file_import = forms.FileField(label=_("File to import"))
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
|
|
|
self.rows = []
|
|
|
|
self.properties = {}
|
|
|
|
self.user = kwargs.pop('user')
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def clean_file_import(self):
|
|
|
|
data = self.cleaned_data["file_import"]
|
|
|
|
|
|
|
|
self.file_name = data.name
|
|
|
|
df = pd.read_excel(data)
|
|
|
|
df.fillna('', inplace=True)
|
|
|
|
|
|
|
|
data_pd = df.to_dict(orient='index')
|
|
|
|
|
|
|
|
if not data_pd or df.last_valid_index() is None:
|
|
|
|
self.exception(_("The file you try to import is empty!"))
|
|
|
|
|
|
|
|
for n in data_pd.keys():
|
|
|
|
if 'type' not in [x.lower() for x in data_pd[n]]:
|
|
|
|
raise ValidationError("You need a column with name 'type'")
|
2024-10-03 10:49:07 +00:00
|
|
|
|
2024-08-05 11:32:52 +00:00
|
|
|
for k, v in data_pd[n].items():
|
|
|
|
if k.lower() == "type":
|
|
|
|
if v not in Device.Types.values:
|
|
|
|
raise ValidationError("{} is not a valid device".format(v))
|
2024-10-03 10:49:07 +00:00
|
|
|
|
2024-08-05 11:32:52 +00:00
|
|
|
self.rows.append(data_pd[n])
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
2024-08-05 13:07:29 +00:00
|
|
|
|
2024-08-05 11:32:52 +00:00
|
|
|
def save(self, commit=True):
|
|
|
|
table = []
|
|
|
|
for row in self.rows:
|
2024-08-05 13:07:29 +00:00
|
|
|
doc = create_doc(row)
|
|
|
|
annotation = create_annotation(doc, self.user)
|
|
|
|
table.append((doc, annotation))
|
2024-08-05 11:32:52 +00:00
|
|
|
|
|
|
|
if commit:
|
|
|
|
for doc, cred in table:
|
2024-10-15 15:56:34 +00:00
|
|
|
path_name = save_in_disk(doc, self.user.institution.name, place="placeholder")
|
|
|
|
|
2024-10-15 14:58:51 +00:00
|
|
|
cred.save()
|
|
|
|
create_index(doc, self.user)
|
2024-10-15 15:56:34 +00:00
|
|
|
move_json(path_name, self.user.institution.name, place="placeholder")
|
2024-10-15 14:58:51 +00:00
|
|
|
return table
|
2024-08-05 11:32:52 +00:00
|
|
|
|
|
|
|
return
|