Skip to content

Commit fe22a8d

Browse files
Merge pull request #4011 from hSaria/3313-config-context-gui
Fixes #3313: YAML-format the config context in the GUI
2 parents b0f7fee + ed99158 commit fe22a8d

File tree

6 files changed

+50
-4
lines changed

6 files changed

+50
-4
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
$('.rendered-context-format').on('click', function() {
2+
if (!$(this).hasClass('active')) {
3+
// Update selection in the button group
4+
$('span.rendered-context-format').removeClass('active');
5+
$('span.rendered-context-format[data-format=' + $(this).data('format') + ']').addClass('active');
6+
7+
// Hide all rendered contexts and only show the selected one
8+
$('div.rendered-context-data').hide();
9+
$('div.rendered-context-data[data-format=' + $(this).data('format') + ']').show();
10+
}
11+
});

netbox/templates/extras/configcontext.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{% extends '_base.html' %}
22
{% load helpers %}
3+
{% load static %}
34

45
{% block header %}
56
<div class="row noprint">
@@ -211,11 +212,16 @@ <h1>{% block title %}{{ configcontext }}{% endblock %}</h1>
211212
<div class="panel panel-default">
212213
<div class="panel-heading">
213214
<strong>Data</strong>
215+
{% include 'extras/inc/configcontext_format.html' %}
214216
</div>
215217
<div class="panel-body">
216-
<pre>{{ configcontext.data|render_json }}</pre>
218+
{% include 'extras/inc/configcontext_data.html' with data=configcontext.data %}
217219
</div>
218220
</div>
219221
</div>
220222
</div>
221223
{% endblock %}
224+
225+
{% block javascript %}
226+
<script src="{% static 'js/configcontext.js' %}?v{{ settings.VERSION }}"></script>
227+
{% endblock %}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{% load helpers %}
2+
3+
<div class="rendered-context-data" data-format="json">
4+
<pre>{{ data|render_json }}</pre>
5+
</div>
6+
<div class="rendered-context-data" data-format="yaml" style="display: none;">
7+
<pre>{{ data|render_yaml }}</pre>
8+
</div>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<div class="pull-right">
2+
<div class="btn-group btn-group-xs" role="group">
3+
<span class="btn btn-default rendered-context-format active" data-format="json">JSON</span>
4+
<span class="btn btn-default rendered-context-format" data-format="yaml">YAML</span>
5+
</div>
6+
</div>

netbox/templates/extras/object_configcontext.html

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{% extends base_template %}
22
{% load helpers %}
3+
{% load static %}
34

45
{% block title %}{{ block.super }} - Config Context{% endblock %}
56

@@ -9,9 +10,10 @@
910
<div class="panel panel-default">
1011
<div class="panel-heading">
1112
<strong>Rendered Context</strong>
13+
{% include 'extras/inc/configcontext_format.html' %}
1214
</div>
1315
<div class="panel-body">
14-
<pre>{{ rendered_context|render_json }}</pre>
16+
{% include 'extras/inc/configcontext_data.html' with data=rendered_context %}
1517
</div>
1618
</div>
1719
</div>
@@ -22,7 +24,7 @@
2224
</div>
2325
<div class="panel-body">
2426
{% if obj.local_context_data %}
25-
<pre>{{ obj.local_context_data|render_json }}</pre>
27+
{% include 'extras/inc/configcontext_data.html' with data=obj.local_context_data %}
2628
{% else %}
2729
<span class="text-muted">None</span>
2830
{% endif %}
@@ -47,7 +49,7 @@
4749
{% if context.description %}
4850
<br /><small>{{ context.description }}</small>
4951
{% endif %}
50-
<pre>{{ context.data|render_json }}</pre>
52+
{% include 'extras/inc/configcontext_data.html' with data=context.data %}
5153
</div>
5254
{% empty %}
5355
<div class="panel-body">
@@ -58,3 +60,7 @@
5860
</div>
5961
</div>
6062
{% endblock %}
63+
64+
{% block javascript %}
65+
<script src="{% static 'js/configcontext.js' %}?v{{ settings.VERSION }}"></script>
66+
{% endblock %}

netbox/utilities/templatetags/helpers.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import datetime
22
import json
33
import re
4+
import yaml
45

56
from django import template
67
from django.utils.html import strip_tags
@@ -76,6 +77,14 @@ def render_json(value):
7677
return json.dumps(value, indent=4, sort_keys=True)
7778

7879

80+
@register.filter()
81+
def render_yaml(value):
82+
"""
83+
Render a dictionary as formatted YAML.
84+
"""
85+
return yaml.dump(dict(value))
86+
87+
7988
@register.filter()
8089
def model_name(obj):
8190
"""

0 commit comments

Comments
 (0)