mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-25 01:48:38 -06:00
Move JSON/YAML data valdiation to ImportForm
This commit is contained in:
parent
2621f17cde
commit
30ee232654
@ -2,11 +2,11 @@ import csv
|
|||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
|
import yaml
|
||||||
|
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.postgres.forms.jsonb import JSONField as _JSONField, InvalidJSONInput
|
from django.contrib.postgres.forms.jsonb import JSONField as _JSONField, InvalidJSONInput
|
||||||
from django.utils.html import mark_safe
|
|
||||||
from mptt.forms import TreeNodeMultipleChoiceField
|
from mptt.forms import TreeNodeMultipleChoiceField
|
||||||
|
|
||||||
from .constants import *
|
from .constants import *
|
||||||
@ -747,7 +747,8 @@ class ImportForm(BootstrapMixin, forms.Form):
|
|||||||
Generic form for creating an object from JSON/YAML data
|
Generic form for creating an object from JSON/YAML data
|
||||||
"""
|
"""
|
||||||
data = forms.CharField(
|
data = forms.CharField(
|
||||||
widget=forms.Textarea
|
widget=forms.Textarea,
|
||||||
|
help_text="Enter object data in JSON or YAML format."
|
||||||
)
|
)
|
||||||
format = forms.ChoiceField(
|
format = forms.ChoiceField(
|
||||||
choices=(
|
choices=(
|
||||||
@ -756,3 +757,24 @@ class ImportForm(BootstrapMixin, forms.Form):
|
|||||||
),
|
),
|
||||||
initial='yaml'
|
initial='yaml'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def clean(self):
|
||||||
|
|
||||||
|
data = self.cleaned_data['data']
|
||||||
|
format = self.cleaned_data['format']
|
||||||
|
|
||||||
|
# Process JSON/YAML data
|
||||||
|
if format == 'json':
|
||||||
|
try:
|
||||||
|
self.cleaned_data['data'] = json.loads(data)
|
||||||
|
except json.decoder.JSONDecodeError as err:
|
||||||
|
raise forms.ValidationError({
|
||||||
|
'data': "Invalid JSON data: {}".format(err)
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
self.cleaned_data['data'] = yaml.load(data)
|
||||||
|
except yaml.scanner.ScannerError as err:
|
||||||
|
raise forms.ValidationError({
|
||||||
|
'data': "Invalid YAML data: {}".format(err)
|
||||||
|
})
|
||||||
|
@ -420,18 +420,10 @@ class ObjectImportView(GetReturnURLMixin, View):
|
|||||||
def post(self, request):
|
def post(self, request):
|
||||||
|
|
||||||
form = ImportForm(request.POST)
|
form = ImportForm(request.POST)
|
||||||
|
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
|
|
||||||
# Process object data
|
|
||||||
if form.cleaned_data['format'] == 'json':
|
|
||||||
data = json.loads(form.cleaned_data['data'])
|
|
||||||
else:
|
|
||||||
data = yaml.load(form.cleaned_data['data'])
|
|
||||||
|
|
||||||
# Initialize model form
|
# Initialize model form
|
||||||
model_form = self.model_form(data)
|
model_form = self.model_form(form.cleaned_data['data'])
|
||||||
|
|
||||||
if model_form.is_valid():
|
if model_form.is_valid():
|
||||||
|
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
|
Loading…
Reference in New Issue
Block a user