From 46a0bb392da9e4801f569efbede110e28c4cb4e9 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 31 Jul 2024 13:28:02 +0200 Subject: [PATCH] Upload multiple File of evidences --- dhub/settings.py | 1 + evidence/forms.py | 38 +++++++++++++++++++++++++++----------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/dhub/settings.py b/dhub/settings.py index 2baf522..055828a 100644 --- a/dhub/settings.py +++ b/dhub/settings.py @@ -171,3 +171,4 @@ LOGGING = { } SNAPSHOT_PATH="/tmp/" +DATA_UPLOAD_MAX_NUMBER_FILES = 1000 diff --git a/evidence/forms.py b/evidence/forms.py index b2e0a61..2e49a6d 100644 --- a/evidence/forms.py +++ b/evidence/forms.py @@ -1,27 +1,43 @@ import json from django import forms +from django.core.exceptions import ValidationError from django.utils.translation import gettext_lazy as _ +from utils.forms import MultipleFileField from evidence.parse import Build +from evidence.models import Annotation class UploadForm(forms.Form): - evidence_file = forms.FileField(label=_("File")) + evidence_file = MultipleFileField(label=_("File")) def clean(self): + self.evidences = [] data = self.cleaned_data.get('evidence_file') if not data: return False - self.file_name = data.name - self.file_data = data.read() - if not self.file_name or not self.file_data: - return False - try: - self.file_json = json.loads(self.file_data) - except Exception: - return False + 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)) return True @@ -29,5 +45,5 @@ class UploadForm(forms.Form): if not commit or not user: return - evidence = Build(self.file_json, user) - return evidence + for ev in self.evidences: + Build(ev[1], user)