Skip to content

Fixes #3313: YAML-format the config context in the GUI #4011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions netbox/project-static/js/configcontext.js
Original file line number Diff line number Diff line change
@@ -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();
}
});
8 changes: 7 additions & 1 deletion netbox/templates/extras/configcontext.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% extends '_base.html' %}
{% load helpers %}
{% load static %}

{% block header %}
<div class="row noprint">
Expand Down Expand Up @@ -211,11 +212,16 @@ <h1>{% block title %}{{ configcontext }}{% endblock %}</h1>
<div class="panel panel-default">
<div class="panel-heading">
<strong>Data</strong>
{% include 'extras/inc/configcontext_format.html' %}
</div>
<div class="panel-body">
<pre>{{ configcontext.data|render_json }}</pre>
{% include 'extras/inc/configcontext_data.html' with data=configcontext.data %}
</div>
</div>
</div>
</div>
{% endblock %}

{% block javascript %}
<script src="{% static 'js/configcontext.js' %}?v{{ settings.VERSION }}"></script>
{% endblock %}
8 changes: 8 additions & 0 deletions netbox/templates/extras/inc/configcontext_data.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% load helpers %}

<div class="rendered-context-data" data-format="json">
<pre>{{ data|render_json }}</pre>
</div>
<div class="rendered-context-data" data-format="yaml" style="display: none;">
<pre>{{ data|render_yaml }}</pre>
</div>
6 changes: 6 additions & 0 deletions netbox/templates/extras/inc/configcontext_format.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class="pull-right">
<div class="btn-group btn-group-xs" role="group">
<span class="btn btn-default rendered-context-format active" data-format="json">JSON</span>
<span class="btn btn-default rendered-context-format" data-format="yaml">YAML</span>
</div>
</div>
12 changes: 9 additions & 3 deletions netbox/templates/extras/object_configcontext.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% extends base_template %}
{% load helpers %}
{% load static %}

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

Expand All @@ -9,9 +10,10 @@
<div class="panel panel-default">
<div class="panel-heading">
<strong>Rendered Context</strong>
{% include 'extras/inc/configcontext_format.html' %}
</div>
<div class="panel-body">
<pre>{{ rendered_context|render_json }}</pre>
{% include 'extras/inc/configcontext_data.html' with data=rendered_context %}
</div>
</div>
</div>
Expand All @@ -22,7 +24,7 @@
</div>
<div class="panel-body">
{% if obj.local_context_data %}
<pre>{{ obj.local_context_data|render_json }}</pre>
{% include 'extras/inc/configcontext_data.html' with data=obj.local_context_data %}
{% else %}
<span class="text-muted">None</span>
{% endif %}
Expand All @@ -47,7 +49,7 @@
{% if context.description %}
<br /><small>{{ context.description }}</small>
{% endif %}
<pre>{{ context.data|render_json }}</pre>
{% include 'extras/inc/configcontext_data.html' with data=context.data %}
</div>
{% empty %}
<div class="panel-body">
Expand All @@ -58,3 +60,7 @@
</div>
</div>
{% endblock %}

{% block javascript %}
<script src="{% static 'js/configcontext.js' %}?v{{ settings.VERSION }}"></script>
{% endblock %}
9 changes: 9 additions & 0 deletions netbox/utilities/templatetags/helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
import json
import re
import yaml

from django import template
from django.utils.html import strip_tags
Expand Down Expand Up @@ -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):
"""
Expand Down