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

Misc cleanup for config contexts

Jeremy Stretch 7 лет назад
Родитель
Сommit
484a74defd

+ 2 - 1
netbox/extras/forms.py

@@ -11,7 +11,7 @@ from taggit.forms import TagField
 from taggit.models import Tag
 
 from dcim.models import Region
-from utilities.forms import add_blank_choice, BootstrapMixin, BulkEditForm, LaxURLField, SlugField
+from utilities.forms import add_blank_choice, BootstrapMixin, BulkEditForm, LaxURLField, JSONField, SlugField
 from .constants import (
     CF_FILTER_DISABLED, CF_TYPE_BOOLEAN, CF_TYPE_DATE, CF_TYPE_INTEGER, CF_TYPE_SELECT, CF_TYPE_URL,
     OBJECTCHANGE_ACTION_CHOICES,
@@ -213,6 +213,7 @@ class ConfigContextForm(BootstrapMixin, forms.ModelForm):
         queryset=Region.objects.all(),
         required=False
     )
+    data = JSONField()
 
     class Meta:
         model = ConfigContext

+ 4 - 0
netbox/templates/extras/object_configcontext.html

@@ -31,6 +31,10 @@
                         {% endif %}
                         <pre>{{ context.data|render_json }}</pre>
                     </div>
+                {% empty %}
+                    <div class="panel-body">
+                        <span class="text-muted">None found</span>
+                    </div>
                 {% endfor %}
             </div>
         </div>

+ 17 - 0
netbox/utilities/forms.py

@@ -6,6 +6,7 @@ import re
 
 from django import forms
 from django.conf import settings
+from django.contrib.postgres.forms import JSONField as _JSONField
 from django.db.models import Count
 from django.urls import reverse_lazy
 from mptt.forms import TreeNodeMultipleChoiceField
@@ -536,6 +537,22 @@ class LaxURLField(forms.URLField):
     default_validators = [EnhancedURLValidator()]
 
 
+class JSONField(_JSONField):
+    """
+    Custom wrapper around Django's built-in JSONField to avoid presenting "null" as the default text.
+    """
+    def __init__(self, *args, **kwargs):
+        super(JSONField, self).__init__(*args, **kwargs)
+        if not self.help_text:
+            self.help_text = 'Enter context data in <a href="https://json.org/">JSON</a> format.'
+            self.widget.attrs['placeholder'] = ''
+
+    def prepare_value(self, value):
+        if value is None:
+            return ''
+        return super(JSONField, self).prepare_value(value)
+
+
 #
 # Forms
 #