Просмотр исходного кода

Fixes #20731 add data file data source to config template bulk import (#20778)

Idris Foughali 2 месяцев назад
Родитель
Сommit
d7877b7627
1 измененных файлов с 30 добавлено и 3 удалено
  1. 30 3
      netbox/extras/forms/bulk_import.py

+ 30 - 3
netbox/extras/forms/bulk_import.py

@@ -5,7 +5,7 @@ from django.contrib.postgres.forms import SimpleArrayField
 from django.core.exceptions import ObjectDoesNotExist
 from django.utils.translation import gettext_lazy as _
 
-from core.models import ObjectType
+from core.models import DataFile, DataSource, ObjectType
 from extras.choices import *
 from extras.models import *
 from netbox.events import get_event_type_choices
@@ -160,14 +160,41 @@ class ConfigContextProfileImportForm(NetBoxModelImportForm):
 
 
 class ConfigTemplateImportForm(CSVModelForm):
+    data_source = CSVModelChoiceField(
+        label=_('Data source'),
+        queryset=DataSource.objects.all(),
+        required=False,
+        to_field_name='name',
+        help_text=_('Data source which provides the data file')
+    )
+    data_file = CSVModelChoiceField(
+        label=_('Data file'),
+        queryset=DataFile.objects.all(),
+        required=False,
+        to_field_name='path',
+        help_text=_('Data file containing the template code')
+    )
+    auto_sync_enabled = forms.BooleanField(
+        required=False,
+        label=_('Auto sync enabled'),
+        help_text=_("Enable automatic synchronization of template content when the data file is updated")
+    )
 
     class Meta:
         model = ConfigTemplate
         fields = (
-            'name', 'description', 'template_code', 'environment_params', 'mime_type', 'file_name', 'file_extension',
-            'as_attachment', 'tags',
+            'name', 'description', 'template_code', 'data_source', 'data_file', 'auto_sync_enabled',
+            'environment_params', 'mime_type', 'file_name', 'file_extension', 'as_attachment', 'tags',
         )
 
+    def clean(self):
+        super().clean()
+
+        # Make sure template_code is None when it's not included in the uploaded data
+        if not self.data.get('template_code') and not self.data.get('data_file'):
+            raise forms.ValidationError(_("Must specify either local content or a data file"))
+        return self.cleaned_data['template_code']
+
 
 class SavedFilterImportForm(CSVModelForm):
     object_types = CSVMultipleContentTypeField(