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.utils.translation import gettext_lazy as _
|
||||
|
||||
from .constants import CSV_DELIMITERS
|
||||
|
||||
|
||||
class ChoiceSetMeta(type):
|
||||
"""
|
||||
@ -234,9 +236,9 @@ class ImportFormatChoices(ChoiceSet):
|
||||
|
||||
class CSVDelimiterChoices(ChoiceSet):
|
||||
AUTO = 'auto'
|
||||
COMMA = ','
|
||||
SEMICOLON = ';'
|
||||
TAB = '\t'
|
||||
COMMA = CSV_DELIMITERS['comma']
|
||||
SEMICOLON = CSV_DELIMITERS['semicolon']
|
||||
TAB = CSV_DELIMITERS['tab']
|
||||
|
||||
CHOICES = [
|
||||
(AUTO, _('Auto-detect')),
|
||||
|
@ -58,3 +58,14 @@ HTTP_REQUEST_META_SAFE_COPY = [
|
||||
'SERVER_NAME',
|
||||
'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 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 .mixins import BootstrapMixin
|
||||
from ..choices import ImportMethodChoices
|
||||
|
||||
|
||||
class BulkImportForm(BootstrapMixin, SyncedDataMixin, forms.Form):
|
||||
@ -92,7 +92,7 @@ class BulkImportForm(BootstrapMixin, SyncedDataMixin, forms.Form):
|
||||
return ImportFormatChoices.YAML
|
||||
# Look for any of the CSV delimiters in the first line (ignoring the default 'auto' choice)
|
||||
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):
|
||||
return ImportFormatChoices.CSV
|
||||
except IndexError:
|
||||
@ -109,7 +109,7 @@ class BulkImportForm(BootstrapMixin, SyncedDataMixin, forms.Form):
|
||||
if delimiter == CSVDelimiterChoices.AUTO:
|
||||
# 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.
|
||||
delimiters = ''.join(CSVDelimiterChoices.values()[1:]) # Skip "auto"
|
||||
delimiters = ''.join(CSV_DELIMITERS.values())
|
||||
try:
|
||||
dialect = csv.Sniffer().sniff(data.strip(), delimiters=delimiters)
|
||||
except csv.Error:
|
||||
|
Loading…
Reference in New Issue
Block a user