Skip to content
Snippets Groups Projects
Commit d6b2a1b0 authored by Felix Blanke's avatar Felix Blanke
Browse files

Add further input validation

parent 10054ca7
No related branches found
No related tags found
No related merge requests found
Pipeline #275346 passed
...@@ -4,6 +4,7 @@ Central and admin forms ...@@ -4,6 +4,7 @@ Central and admin forms
import csv import csv
import io import io
import json
from django import forms from django import forms
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
...@@ -299,17 +300,44 @@ class JSONScheduleImportForm(AdminIntermediateForm): ...@@ -299,17 +300,44 @@ class JSONScheduleImportForm(AdminIntermediateForm):
help_text=_("File with JSON data from the scheduling solver"), help_text=_("File with JSON data from the scheduling solver"),
) )
def _check_json_data(self, data: str):
try:
schedule = json.loads(data)
except json.JSONDecodeError as ex:
return ValidationError(_("Cannot decode as JSON"), "invalid")
for field in ["input", "scheduled_aks"]:
if not field in schedule:
return ValidationError(
_("Invalid JSON format: field '%(field)s' is missing"),
"invalid",
params={"field": field}
)
# TODO: Add further checks on json input
def clean(self): def clean(self):
cleaned_data = super().clean() cleaned_data = super().clean()
if cleaned_data.get("json_file") and cleaned_data.get("json_data"): if cleaned_data.get("json_file") and cleaned_data.get("json_data"):
err = ValidationError(_("Please enter data as a file OR via text, not both."), "invalid") err = ValidationError(
_("Please enter data as a file OR via text, not both."), "invalid"
)
self.add_error("json_data", err) self.add_error("json_data", err)
self.add_error("json_file", err) self.add_error("json_file", err)
elif not (cleaned_data.get("json_file") or cleaned_data.get("json_data")): elif not (cleaned_data.get("json_file") or cleaned_data.get("json_data")):
err = ValidationError(_("No data entered. Please enter data as a file or via text."), "invalid") err = ValidationError(
_("No data entered. Please enter data as a file or via text."), "invalid"
)
self.add_error("json_data", err) self.add_error("json_data", err)
self.add_error("json_file", err) self.add_error("json_file", err)
elif cleaned_data.get("json_file"):
# TODO Check input data if it is a valid JSON data = self.cleaned_data.get("json_file")
with data.open() as ff:
read_data = ff.read()
err = self._check_json_data(read_data)
if err is not None:
self.add_error("json_file", err)
elif cleaned_data.get("json_data"):
err = self._check_json_data(cleaned_data.get("json_data"))
if err is not None:
self.add_error("json_data", err)
return cleaned_data return cleaned_data
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment