mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-24 08:25:17 -06:00
adds csv delimiter on the form
This commit is contained in:
parent
4da93f4628
commit
81024af024
@ -44,6 +44,7 @@ Context:
|
|||||||
<input type="hidden" name="import_method" value="direct" />
|
<input type="hidden" name="import_method" value="direct" />
|
||||||
{% render_field form.data %}
|
{% render_field form.data %}
|
||||||
{% render_field form.format %}
|
{% render_field form.format %}
|
||||||
|
{% render_field form.csv_delimiter %}
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<div class="col col-md-12 text-end">
|
<div class="col col-md-12 text-end">
|
||||||
<button type="submit" name="data_submit" class="btn btn-primary">Submit</button>
|
<button type="submit" name="data_submit" class="btn btn-primary">Submit</button>
|
||||||
|
@ -227,3 +227,17 @@ class ImportFormatChoices(ChoiceSet):
|
|||||||
(JSON, 'JSON'),
|
(JSON, 'JSON'),
|
||||||
(YAML, 'YAML'),
|
(YAML, 'YAML'),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
class CSVDelimiterChoices(ChoiceSet):
|
||||||
|
AUTO = 'auto'
|
||||||
|
COMMA = ','
|
||||||
|
SEMICOLON = ';'
|
||||||
|
TAB = '\t'
|
||||||
|
|
||||||
|
CHOICES = [
|
||||||
|
(AUTO, 'Auto-detect'),
|
||||||
|
(COMMA, 'Comma'),
|
||||||
|
(SEMICOLON, 'Semicolon'),
|
||||||
|
(TAB, 'Tab'),
|
||||||
|
]
|
||||||
|
@ -7,7 +7,7 @@ from django import forms
|
|||||||
from django.utils.translation import gettext as _
|
from django.utils.translation import gettext as _
|
||||||
|
|
||||||
from core.forms.mixins import SyncedDataMixin
|
from core.forms.mixins import SyncedDataMixin
|
||||||
from utilities.choices import ImportFormatChoices
|
from utilities.choices import CSVDelimiterChoices, ImportFormatChoices
|
||||||
from utilities.forms.utils import parse_csv
|
from utilities.forms.utils import parse_csv
|
||||||
from .mixins import BootstrapMixin
|
from .mixins import BootstrapMixin
|
||||||
from ..choices import ImportMethodChoices
|
from ..choices import ImportMethodChoices
|
||||||
@ -31,6 +31,13 @@ class BulkImportForm(BootstrapMixin, SyncedDataMixin, forms.Form):
|
|||||||
choices=ImportFormatChoices,
|
choices=ImportFormatChoices,
|
||||||
initial=ImportFormatChoices.AUTO
|
initial=ImportFormatChoices.AUTO
|
||||||
)
|
)
|
||||||
|
csv_delimiter = forms.ChoiceField(
|
||||||
|
choices=CSVDelimiterChoices,
|
||||||
|
initial=CSVDelimiterChoices.AUTO,
|
||||||
|
label="CSV Delimiter",
|
||||||
|
help_text=_("The character which delimits CSV fields. Applies only to CSV format."),
|
||||||
|
required=False
|
||||||
|
)
|
||||||
|
|
||||||
data_field = 'data'
|
data_field = 'data'
|
||||||
|
|
||||||
@ -91,13 +98,21 @@ class BulkImportForm(BootstrapMixin, SyncedDataMixin, forms.Form):
|
|||||||
Clean CSV-formatted data. The first row will be treated as column headers.
|
Clean CSV-formatted data. The first row will be treated as column headers.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Determine the CSV dialect
|
if self.cleaned_data['csv_delimiter'] == CSVDelimiterChoices.AUTO:
|
||||||
try:
|
# Determine the CSV dialect
|
||||||
# This uses a rough heuristic to detect the CSV dialect. If the data is malformed, we'll fall back to
|
try:
|
||||||
# the default Excel dialect.
|
# This uses a rough heuristic to detect the CSV dialect. If the data is malformed, we'll fall back to
|
||||||
dialect = csv.Sniffer().sniff(data.strip(), delimiters=',; ')
|
# the default Excel dialect.
|
||||||
except csv.Error:
|
dialect = csv.Sniffer().sniff(
|
||||||
|
data.strip(), delimiters=''.join(
|
||||||
|
[CSVDelimiterChoices.COMMA, CSVDelimiterChoices.SEMICOLON, CSVDelimiterChoices.TAB]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
except csv.Error:
|
||||||
|
dialect = csv.excel
|
||||||
|
else:
|
||||||
dialect = csv.excel
|
dialect = csv.excel
|
||||||
|
dialect.delimiter = self.cleaned_data['csv_delimiter']
|
||||||
|
|
||||||
stream = StringIO(data.strip())
|
stream = StringIO(data.strip())
|
||||||
reader = csv.reader(stream, dialect=dialect)
|
reader = csv.reader(stream, dialect=dialect)
|
||||||
|
Loading…
Reference in New Issue
Block a user