Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
AKPlanning
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Felix Blanke
AKPlanning
Commits
26d3aa70
Commit
26d3aa70
authored
1 week ago
by
Felix Blanke
Browse files
Options
Downloads
Patches
Plain Diff
Use schema to validate user JSON schedule
parent
5f53d1ff
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
AKModel/forms.py
+20
-8
20 additions, 8 deletions
AKModel/forms.py
with
20 additions
and
8 deletions
AKModel/forms.py
+
20
−
8
View file @
26d3aa70
...
...
@@ -10,9 +10,11 @@ from django import forms
from
django.core.exceptions
import
ValidationError
from
django.forms.utils
import
ErrorList
from
django.utils.translation
import
gettext_lazy
as
_
from
jsonschema.exceptions
import
best_match
from
AKModel.availability.forms
import
AvailabilitiesFormMixin
from
AKModel.models
import
Event
,
AKCategory
,
AKRequirement
,
Room
,
AKType
from
AKModel.utils
import
construct_schema_validator
class
DateTimeInput
(
forms
.
DateInput
):
...
...
@@ -300,19 +302,29 @@ class JSONScheduleImportForm(AdminIntermediateForm):
help_text
=
_
(
"
File with JSON data from the scheduling solver
"
),
)
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
().
__init__
(
*
args
,
**
kwargs
)
self
.
json_schema_validator
=
construct_schema_validator
(
schema
=
"
solver-output.schema.json
"
)
def
_check_json_data
(
self
,
data
:
str
):
try
:
schedule
=
json
.
loads
(
data
)
except
json
.
JSONDecodeError
as
ex
:
raise
ValidationError
(
_
(
"
Cannot decode as JSON
"
),
"
invalid
"
)
from
ex
for
field
in
[
"
input
"
,
"
scheduled_aks
"
]:
if
not
field
in
schedule
:
raise
ValidationError
(
_
(
"
Invalid JSON format: field
'
%(field)s
'
is missing
"
),
"
invalid
"
,
params
=
{
"
field
"
:
field
}
)
# TODO: Add further checks on json input
error
=
best_match
(
self
.
json_schema_validator
.
iter_errors
(
schedule
))
if
error
:
raise
ValidationError
(
_
(
"
Invalid JSON format: %(msg)s at %(error_path)s
"
),
"
invalid
"
,
params
=
{
"
msg
"
:
error
.
message
,
"
error_path
"
:
error
.
json_path
}
)
from
error
return
schedule
def
clean
(
self
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment