mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-24 08:25:17 -06:00
11617 validate bulk import fields
This commit is contained in:
parent
ef33aa9fce
commit
18b98e641c
@ -21,6 +21,7 @@ from utilities.error_handlers import handle_protectederror
|
||||
from utilities.exceptions import AbortRequest, AbortTransaction, PermissionsViolation
|
||||
from utilities.forms import BulkRenameForm, ConfirmationForm, restrict_form_fields
|
||||
from utilities.forms.bulk_import import BulkImportForm
|
||||
from utilities.forms.utils import validate_import_headers
|
||||
from utilities.htmx import is_embedded, is_htmx
|
||||
from utilities.permissions import get_permission_for_model
|
||||
from utilities.utils import get_viewname
|
||||
@ -395,6 +396,14 @@ class BulkImportView(GetReturnURLMixin, BaseMultiObjectView):
|
||||
model_form_kwargs['headers'] = form._csv_headers # Add CSV headers
|
||||
model_form = self.model_form(**model_form_kwargs)
|
||||
|
||||
# validate the fields (required fields are present and no unknown fields)
|
||||
form_fields = model_form.fields
|
||||
required_fields = [
|
||||
name for name, field in form_fields.items() if field.required
|
||||
]
|
||||
headers = list(record.keys())
|
||||
validate_import_headers(headers, form_fields, required_fields)
|
||||
|
||||
# When updating, omit all form fields other than those specified in the record. (No
|
||||
# fields are required when modifying an existing object.)
|
||||
if object_id:
|
||||
|
@ -18,7 +18,7 @@ __all__ = (
|
||||
'parse_numeric_range',
|
||||
'restrict_form_fields',
|
||||
'parse_csv',
|
||||
'validate_csv',
|
||||
'validate_import_headers',
|
||||
)
|
||||
|
||||
|
||||
@ -242,7 +242,7 @@ def parse_csv(reader):
|
||||
return headers, records
|
||||
|
||||
|
||||
def validate_csv(headers, fields, required_fields):
|
||||
def validate_import_headers(headers, fields, required_fields):
|
||||
"""
|
||||
Validate that parsed csv data conforms to the object's available fields. Raise validation errors
|
||||
if parsed csv data contains invalid headers or does not contain required headers.
|
||||
|
Loading…
Reference in New Issue
Block a user