mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
11955 remove csvdatafield csvfilefield
This commit is contained in:
@@ -14,8 +14,6 @@ from utilities.utils import content_type_identifier
|
||||
__all__ = (
|
||||
'CSVChoiceField',
|
||||
'CSVContentTypeField',
|
||||
'CSVDataField',
|
||||
'CSVFileField',
|
||||
'CSVModelChoiceField',
|
||||
'CSVModelMultipleChoiceField',
|
||||
'CSVMultipleChoiceField',
|
||||
@@ -24,90 +22,6 @@ __all__ = (
|
||||
)
|
||||
|
||||
|
||||
class CSVDataField(forms.CharField):
|
||||
"""
|
||||
A CharField (rendered as a Textarea) which accepts CSV-formatted data. It returns data as a two-tuple: The first
|
||||
item is a dictionary of column headers, mapping field names to the attribute by which they match a related object
|
||||
(where applicable). The second item is a list of dictionaries, each representing a discrete row of CSV data.
|
||||
|
||||
:param from_form: The form from which the field derives its validation rules.
|
||||
"""
|
||||
widget = forms.Textarea
|
||||
|
||||
def __init__(self, from_form, *args, **kwargs):
|
||||
|
||||
form = from_form()
|
||||
self.model = form.Meta.model
|
||||
self.fields = form.fields
|
||||
self.required_fields = [
|
||||
name for name, field in form.fields.items() if field.required
|
||||
]
|
||||
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
self.strip = False
|
||||
if not self.label:
|
||||
self.label = ''
|
||||
if not self.initial:
|
||||
self.initial = ','.join(self.required_fields) + '\n'
|
||||
if not self.help_text:
|
||||
self.help_text = _('Enter the list of column headers followed by one line per record to be imported, using '
|
||||
'commas to separate values. Multi-line data and values containing commas may be wrapped '
|
||||
'in double quotes.')
|
||||
|
||||
def to_python(self, value):
|
||||
reader = csv.reader(StringIO(value.strip()))
|
||||
|
||||
return parse_csv(reader)
|
||||
|
||||
def validate(self, value):
|
||||
headers, records = value
|
||||
validate_csv(headers, self.fields, self.required_fields)
|
||||
|
||||
return value
|
||||
|
||||
|
||||
class CSVFileField(forms.FileField):
|
||||
"""
|
||||
A FileField (rendered as a file input button) which accepts a file containing CSV-formatted data. It returns
|
||||
data as a two-tuple: The first item is a dictionary of column headers, mapping field names to the attribute
|
||||
by which they match a related object (where applicable). The second item is a list of dictionaries, each
|
||||
representing a discrete row of CSV data.
|
||||
|
||||
:param from_form: The form from which the field derives its validation rules.
|
||||
"""
|
||||
|
||||
def __init__(self, from_form, *args, **kwargs):
|
||||
|
||||
form = from_form()
|
||||
self.model = form.Meta.model
|
||||
self.fields = form.fields
|
||||
self.required_fields = [
|
||||
name for name, field in form.fields.items() if field.required
|
||||
]
|
||||
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def to_python(self, file):
|
||||
if file is None:
|
||||
return None
|
||||
|
||||
csv_str = file.read().decode('utf-8').strip()
|
||||
reader = csv.reader(StringIO(csv_str))
|
||||
headers, records = parse_csv(reader)
|
||||
|
||||
return headers, records
|
||||
|
||||
def validate(self, value):
|
||||
if value is None:
|
||||
return None
|
||||
|
||||
headers, records = value
|
||||
validate_csv(headers, self.fields, self.required_fields)
|
||||
|
||||
return value
|
||||
|
||||
|
||||
class CSVChoicesMixin:
|
||||
STATIC_CHOICES = True
|
||||
|
||||
|
Reference in New Issue
Block a user