devicehub-django/evidence/forms.py

34 lines
792 B
Python
Raw Normal View History

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
from django.utils.translation import gettext_lazy as _
from evidence.parse import Build
2024-07-13 13:27:36 +00:00
class UploadForm(forms.Form):
2024-07-30 17:38:04 +00:00
2024-07-26 15:59:34 +00:00
evidence_file = forms.FileField(label=_("File"))
2024-07-13 13:27:36 +00:00
def clean(self):
2024-07-30 17:38:04 +00:00
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
return True
def save(self, user, commit=True):
if not commit or not user:
return
evidence = Build(self.file_json, user)
return evidence