2024-07-30 17:38:04 +00:00
|
|
|
import json
|
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-07-31 11:28:02 +00:00
|
|
|
from utils.forms import MultipleFileField
|
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-07-13 13:27:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UploadForm(forms.Form):
|
2024-07-30 17:38:04 +00:00
|
|
|
|
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:
|
|
|
|
Build(ev[1], user)
|