From f7f141c69e956f06c42bb803fca73600eca08e40 Mon Sep 17 00:00:00 2001 From: Saria Hajjar Date: Sat, 25 Jan 2020 15:56:24 +0000 Subject: [PATCH 1/4] Fixes #3313: YAML-format the config context in the GUI --- netbox/project-static/js/configcontext.js | 11 +++++++++++ netbox/templates/extras/configcontext.html | 8 +++++++- netbox/templates/extras/inc/configcontext_data.html | 8 ++++++++ netbox/templates/extras/inc/configcontext_format.html | 6 ++++++ netbox/templates/extras/object_configcontext.html | 8 +++++++- netbox/utilities/templatetags/helpers.py | 9 +++++++++ 6 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 netbox/project-static/js/configcontext.js create mode 100644 netbox/templates/extras/inc/configcontext_data.html create mode 100644 netbox/templates/extras/inc/configcontext_format.html diff --git a/netbox/project-static/js/configcontext.js b/netbox/project-static/js/configcontext.js new file mode 100644 index 000000000..1d731e696 --- /dev/null +++ b/netbox/project-static/js/configcontext.js @@ -0,0 +1,11 @@ +$('.rendered-context-format').on('click', function() { + if (!$(this).hasClass('active')) { + // Update selection in the button group + $('span.rendered-context-format').removeClass('active'); + $('span.rendered-context-format[data-format=' + $(this).data('format') + ']').addClass('active'); + + // Hide all rendered contexts and only show the selected one + $('div.rendered-context-data').hide(); + $('div.rendered-context-data[data-format=' + $(this).data('format') + ']').show(); + } +}); diff --git a/netbox/templates/extras/configcontext.html b/netbox/templates/extras/configcontext.html index 7cec3f403..067bf1e96 100644 --- a/netbox/templates/extras/configcontext.html +++ b/netbox/templates/extras/configcontext.html @@ -1,5 +1,6 @@ {% extends '_base.html' %} {% load helpers %} +{% load static %} {% block header %}
@@ -183,11 +184,16 @@
Data + {% include 'extras/inc/configcontext_format.html' %}
-
{{ configcontext.data|render_json }}
+ {% include 'extras/inc/configcontext_data.html' with data=configcontext.data %}
{% endblock %} + +{% block javascript %} + +{% endblock %} diff --git a/netbox/templates/extras/inc/configcontext_data.html b/netbox/templates/extras/inc/configcontext_data.html new file mode 100644 index 000000000..d987b2acb --- /dev/null +++ b/netbox/templates/extras/inc/configcontext_data.html @@ -0,0 +1,8 @@ +{% load helpers %} + +
+
{{ data|render_yaml }}
+
+ diff --git a/netbox/templates/extras/inc/configcontext_format.html b/netbox/templates/extras/inc/configcontext_format.html new file mode 100644 index 000000000..eb34adfdf --- /dev/null +++ b/netbox/templates/extras/inc/configcontext_format.html @@ -0,0 +1,6 @@ +
+
+ YAML + JSON +
+
diff --git a/netbox/templates/extras/object_configcontext.html b/netbox/templates/extras/object_configcontext.html index d23455c19..f8191936e 100644 --- a/netbox/templates/extras/object_configcontext.html +++ b/netbox/templates/extras/object_configcontext.html @@ -1,5 +1,6 @@ {% extends base_template %} {% load helpers %} +{% load static %} {% block title %}{{ block.super }} - Config Context{% endblock %} @@ -9,9 +10,10 @@
Rendered Context + {% include 'extras/inc/configcontext_format.html' %}
-
{{ rendered_context|render_json }}
+ {% include 'extras/inc/configcontext_data.html' with data=rendered_context %}
@@ -58,3 +60,7 @@ {% endblock %} + +{% block javascript %} + +{% endblock %} diff --git a/netbox/utilities/templatetags/helpers.py b/netbox/utilities/templatetags/helpers.py index c4b3bb6ea..4278b3b95 100644 --- a/netbox/utilities/templatetags/helpers.py +++ b/netbox/utilities/templatetags/helpers.py @@ -1,6 +1,7 @@ import datetime import json import re +import yaml from django import template from django.utils.html import strip_tags @@ -76,6 +77,14 @@ def render_json(value): return json.dumps(value, indent=4, sort_keys=True) +@register.filter() +def render_yaml(value): + """ + Render a dictionary as formatted YAML. + """ + return yaml.dump(dict(value)) + + @register.filter() def model_name(obj): """ From 0970dcb40cdb0ebdfe976f36934e8fca86ca73a0 Mon Sep 17 00:00:00 2001 From: Saria Hajjar Date: Sat, 25 Jan 2020 16:12:37 +0000 Subject: [PATCH 2/4] Format for local and source contexts --- netbox/templates/extras/object_configcontext.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/netbox/templates/extras/object_configcontext.html b/netbox/templates/extras/object_configcontext.html index f8191936e..784b5805f 100644 --- a/netbox/templates/extras/object_configcontext.html +++ b/netbox/templates/extras/object_configcontext.html @@ -24,7 +24,7 @@
{% if obj.local_context_data %} -
{{ obj.local_context_data|render_json }}
+ {% include 'extras/inc/configcontext_data.html' with data=obj.local_context_data %} {% else %} None {% endif %} @@ -49,7 +49,7 @@ {% if context.description %}
{{ context.description }} {% endif %} -
{{ context.data|render_json }}
+ {% include 'extras/inc/configcontext_data.html' with data=context.data %}
{% empty %}
From e62a8de84d1d3ffb6bc7d0c2629c1900b57a31f8 Mon Sep 17 00:00:00 2001 From: Saria Hajjar Date: Sat, 25 Jan 2020 17:55:01 +0000 Subject: [PATCH 3/4] Corrected ConfigContext data --- netbox/extras/tests/test_views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netbox/extras/tests/test_views.py b/netbox/extras/tests/test_views.py index 792390121..bb5d484a0 100644 --- a/netbox/extras/tests/test_views.py +++ b/netbox/extras/tests/test_views.py @@ -49,7 +49,7 @@ class ConfigContextTestCase(TestCase): for i in range(1, 4): configcontext = ConfigContext( name='Config Context {}'.format(i), - data='{{"foo": {}}}'.format(i) + data={"foo": i} ) configcontext.save() configcontext.sites.add(site) From c10e960421c2327f012e4486e0432ba8dabdab86 Mon Sep 17 00:00:00 2001 From: Saria Hajjar Date: Fri, 31 Jan 2020 11:11:42 +0000 Subject: [PATCH 4/4] Set default config context format to JSON to maintain existing behavior --- netbox/templates/extras/inc/configcontext_data.html | 8 ++++---- netbox/templates/extras/inc/configcontext_format.html | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/netbox/templates/extras/inc/configcontext_data.html b/netbox/templates/extras/inc/configcontext_data.html index d987b2acb..d91960e2c 100644 --- a/netbox/templates/extras/inc/configcontext_data.html +++ b/netbox/templates/extras/inc/configcontext_data.html @@ -1,8 +1,8 @@ {% load helpers %} -
-
{{ data|render_yaml }}
-
-