mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Replaced old CSVDataField
This commit is contained in:
@ -7,7 +7,7 @@ from django import forms
|
|||||||
from django.db.models import Count
|
from django.db.models import Count
|
||||||
|
|
||||||
from dcim.models import Device
|
from dcim.models import Device
|
||||||
from utilities.forms import BootstrapMixin, BulkEditForm, CSVDataField2, FilterChoiceField, SlugField
|
from utilities.forms import BootstrapMixin, BulkEditForm, CSVDataField, FilterChoiceField, SlugField
|
||||||
from .models import Secret, SecretRole, UserKey
|
from .models import Secret, SecretRole, UserKey
|
||||||
|
|
||||||
|
|
||||||
|
@ -216,47 +216,6 @@ class Livesearch(forms.TextInput):
|
|||||||
#
|
#
|
||||||
|
|
||||||
class CSVDataField(forms.CharField):
|
class CSVDataField(forms.CharField):
|
||||||
"""
|
|
||||||
A field for comma-separated values (CSV). Values containing commas should be encased within double quotes. Example:
|
|
||||||
'"New York, NY",new-york-ny,Other stuff' => ['New York, NY', 'new-york-ny', 'Other stuff']
|
|
||||||
"""
|
|
||||||
csv_form = None
|
|
||||||
widget = forms.Textarea
|
|
||||||
|
|
||||||
def __init__(self, csv_form, *args, **kwargs):
|
|
||||||
self.csv_form = csv_form
|
|
||||||
self.columns = self.csv_form().fields.keys()
|
|
||||||
super(CSVDataField, self).__init__(*args, **kwargs)
|
|
||||||
self.strip = False
|
|
||||||
if not self.label:
|
|
||||||
self.label = 'CSV Data'
|
|
||||||
if not self.help_text:
|
|
||||||
self.help_text = 'Enter one line per record in CSV format.'
|
|
||||||
|
|
||||||
def to_python(self, value):
|
|
||||||
"""
|
|
||||||
Return a list of dictionaries, each representing an individual record
|
|
||||||
"""
|
|
||||||
# Python 2's csv module has problems with Unicode
|
|
||||||
if not isinstance(value, str):
|
|
||||||
value = value.encode('utf-8')
|
|
||||||
records = []
|
|
||||||
reader = csv.reader(value.splitlines())
|
|
||||||
for i, row in enumerate(reader, start=1):
|
|
||||||
if row:
|
|
||||||
if len(row) < len(self.columns):
|
|
||||||
raise forms.ValidationError("Line {}: Field(s) missing (found {}; expected {})"
|
|
||||||
.format(i, len(row), len(self.columns)))
|
|
||||||
elif len(row) > len(self.columns):
|
|
||||||
raise forms.ValidationError("Line {}: Too many fields (found {}; expected {})"
|
|
||||||
.format(i, len(row), len(self.columns)))
|
|
||||||
row = [col.strip() for col in row]
|
|
||||||
record = dict(zip(self.columns, row))
|
|
||||||
records.append(record)
|
|
||||||
return records
|
|
||||||
|
|
||||||
|
|
||||||
class CSVDataField2(forms.CharField):
|
|
||||||
"""
|
"""
|
||||||
A CharField (rendered as a Textarea) which accepts CSV-formatted data. It returns a list of dictionaries mapping
|
A CharField (rendered as a Textarea) which accepts CSV-formatted data. It returns a list of dictionaries mapping
|
||||||
column headers to values. Each dictionary represents an individual record.
|
column headers to values. Each dictionary represents an individual record.
|
||||||
@ -268,7 +227,7 @@ class CSVDataField2(forms.CharField):
|
|||||||
self.fields = fields
|
self.fields = fields
|
||||||
self.required_fields = required_fields
|
self.required_fields = required_fields
|
||||||
|
|
||||||
super(CSVDataField2, self).__init__(*args, **kwargs)
|
super(CSVDataField, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
self.strip = False
|
self.strip = False
|
||||||
if not self.label:
|
if not self.label:
|
||||||
|
@ -20,7 +20,7 @@ from django.utils.safestring import mark_safe
|
|||||||
from django.views.generic import View
|
from django.views.generic import View
|
||||||
|
|
||||||
from extras.models import CustomField, CustomFieldValue, ExportTemplate, UserAction
|
from extras.models import CustomField, CustomFieldValue, ExportTemplate, UserAction
|
||||||
from utilities.forms import BootstrapMixin, CSVDataField2
|
from utilities.forms import BootstrapMixin, CSVDataField
|
||||||
from .error_handlers import handle_protectederror
|
from .error_handlers import handle_protectederror
|
||||||
from .forms import ConfirmationForm
|
from .forms import ConfirmationForm
|
||||||
from .paginator import EnhancedPaginator
|
from .paginator import EnhancedPaginator
|
||||||
@ -389,7 +389,7 @@ class BulkImportView(View):
|
|||||||
required_fields = [name for name, field in self.model_form().fields.items() if field.required]
|
required_fields = [name for name, field in self.model_form().fields.items() if field.required]
|
||||||
|
|
||||||
class ImportForm(BootstrapMixin, Form):
|
class ImportForm(BootstrapMixin, Form):
|
||||||
csv = CSVDataField2(fields=fields, required_fields=required_fields)
|
csv = CSVDataField(fields=fields, required_fields=required_fields)
|
||||||
|
|
||||||
return ImportForm(*args, **kwargs)
|
return ImportForm(*args, **kwargs)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user