Przeglądaj źródła

#9073: Fix form behavior when disassociating a ConfigContext from a DataFile

jeremystretch 2 lat temu
rodzic
commit
2b3b9517d2

+ 8 - 0
netbox/extras/forms/model_forms.py

@@ -267,6 +267,14 @@ class ConfigContextForm(BootstrapMixin, SyncedDataMixin, forms.ModelForm):
             'tenants', 'tags', 'data_source', 'data_file',
         )
 
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+
+        # Disable data field when a DataFile has been set
+        if self.instance.data_file:
+            self.fields['data'].widget.attrs['readonly'] = True
+            self.fields['data'].help_text = _('Data is populated from the remote source selected below.')
+
     def clean(self):
         super().clean()
 

+ 7 - 8
netbox/netbox/models/features.py

@@ -372,16 +372,15 @@ class SyncedDataMixin(models.Model):
         return self.data_file and self.data_synced >= self.data_file.last_updated
 
     def clean(self):
-        if self.data_file:
-            self.sync_data()
-            self.data_path = self.data_file.path
 
-        if self.data_source and not self.data_file:
-            raise ValidationError({
-                'data_file': _(f"Must specify a data file when designating a data source.")
-            })
-        if self.data_file and not self.data_source:
+        if self.data_file:
             self.data_source = self.data_file.source
+            self.data_path = self.data_file.path
+            self.sync_data()
+        else:
+            self.data_source = None
+            self.data_path = ''
+            self.data_synced = None
 
         super().clean()