mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-24 00:15:17 -06:00
Move delimiting chars to a separate constant for easy reference
This commit is contained in:
parent
f14f2fa5f0
commit
910f6b380c
@ -1,6 +1,8 @@
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
|
from .constants import CSV_DELIMITERS
|
||||||
|
|
||||||
|
|
||||||
class ChoiceSetMeta(type):
|
class ChoiceSetMeta(type):
|
||||||
"""
|
"""
|
||||||
@ -234,9 +236,9 @@ class ImportFormatChoices(ChoiceSet):
|
|||||||
|
|
||||||
class CSVDelimiterChoices(ChoiceSet):
|
class CSVDelimiterChoices(ChoiceSet):
|
||||||
AUTO = 'auto'
|
AUTO = 'auto'
|
||||||
COMMA = ','
|
COMMA = CSV_DELIMITERS['comma']
|
||||||
SEMICOLON = ';'
|
SEMICOLON = CSV_DELIMITERS['semicolon']
|
||||||
TAB = '\t'
|
TAB = CSV_DELIMITERS['tab']
|
||||||
|
|
||||||
CHOICES = [
|
CHOICES = [
|
||||||
(AUTO, _('Auto-detect')),
|
(AUTO, _('Auto-detect')),
|
||||||
|
@ -58,3 +58,14 @@ HTTP_REQUEST_META_SAFE_COPY = [
|
|||||||
'SERVER_NAME',
|
'SERVER_NAME',
|
||||||
'SERVER_PORT',
|
'SERVER_PORT',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
#
|
||||||
|
# CSV-style format delimiters
|
||||||
|
#
|
||||||
|
|
||||||
|
CSV_DELIMITERS = {
|
||||||
|
'comma': ',',
|
||||||
|
'semicolon': ';',
|
||||||
|
'tab': '\t',
|
||||||
|
}
|
||||||
|
@ -7,10 +7,10 @@ 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 CSVDelimiterChoices, ImportFormatChoices
|
from utilities.choices import CSVDelimiterChoices, ImportFormatChoices, ImportMethodChoices
|
||||||
|
from utilities.constants import CSV_DELIMITERS
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
class BulkImportForm(BootstrapMixin, SyncedDataMixin, forms.Form):
|
class BulkImportForm(BootstrapMixin, SyncedDataMixin, forms.Form):
|
||||||
@ -92,7 +92,7 @@ class BulkImportForm(BootstrapMixin, SyncedDataMixin, forms.Form):
|
|||||||
return ImportFormatChoices.YAML
|
return ImportFormatChoices.YAML
|
||||||
# Look for any of the CSV delimiters in the first line (ignoring the default 'auto' choice)
|
# Look for any of the CSV delimiters in the first line (ignoring the default 'auto' choice)
|
||||||
first_line = data.split('\n', 1)[0]
|
first_line = data.split('\n', 1)[0]
|
||||||
csv_delimiters = CSVDelimiterChoices.values()[1:]
|
csv_delimiters = CSV_DELIMITERS.values()
|
||||||
if any(x in first_line for x in csv_delimiters):
|
if any(x in first_line for x in csv_delimiters):
|
||||||
return ImportFormatChoices.CSV
|
return ImportFormatChoices.CSV
|
||||||
except IndexError:
|
except IndexError:
|
||||||
@ -109,7 +109,7 @@ class BulkImportForm(BootstrapMixin, SyncedDataMixin, forms.Form):
|
|||||||
if delimiter == CSVDelimiterChoices.AUTO:
|
if delimiter == CSVDelimiterChoices.AUTO:
|
||||||
# This uses a rough heuristic to detect the CSV dialect based on the presence of supported delimiting
|
# This uses a rough heuristic to detect the CSV dialect based on the presence of supported delimiting
|
||||||
# characters. If the data is malformed, we'll fall back to the default Excel dialect.
|
# characters. If the data is malformed, we'll fall back to the default Excel dialect.
|
||||||
delimiters = ''.join(CSVDelimiterChoices.values()[1:]) # Skip "auto"
|
delimiters = ''.join(CSV_DELIMITERS.values())
|
||||||
try:
|
try:
|
||||||
dialect = csv.Sniffer().sniff(data.strip(), delimiters=delimiters)
|
dialect = csv.Sniffer().sniff(data.strip(), delimiters=delimiters)
|
||||||
except csv.Error:
|
except csv.Error:
|
||||||
|
Loading…
Reference in New Issue
Block a user