mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Move JSON/YAML data valdiation to ImportForm
This commit is contained in:
@@ -2,11 +2,11 @@ import csv
|
||||
import json
|
||||
import re
|
||||
from io import StringIO
|
||||
import yaml
|
||||
|
||||
from django import forms
|
||||
from django.conf import settings
|
||||
from django.contrib.postgres.forms.jsonb import JSONField as _JSONField, InvalidJSONInput
|
||||
from django.utils.html import mark_safe
|
||||
from mptt.forms import TreeNodeMultipleChoiceField
|
||||
|
||||
from .constants import *
|
||||
@@ -747,7 +747,8 @@ class ImportForm(BootstrapMixin, forms.Form):
|
||||
Generic form for creating an object from JSON/YAML data
|
||||
"""
|
||||
data = forms.CharField(
|
||||
widget=forms.Textarea
|
||||
widget=forms.Textarea,
|
||||
help_text="Enter object data in JSON or YAML format."
|
||||
)
|
||||
format = forms.ChoiceField(
|
||||
choices=(
|
||||
@@ -756,3 +757,24 @@ class ImportForm(BootstrapMixin, forms.Form):
|
||||
),
|
||||
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):
|
||||
|
||||
form = ImportForm(request.POST)
|
||||
|
||||
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
|
||||
model_form = self.model_form(data)
|
||||
|
||||
model_form = self.model_form(form.cleaned_data['data'])
|
||||
if model_form.is_valid():
|
||||
|
||||
with transaction.atomic():
|
||||
|
Reference in New Issue
Block a user