Closes #4421: Retain user's preference for config context format

This commit is contained in:
Jeremy Stretch 2020-04-24 12:01:41 -04:00
parent f019c8d2ce
commit bdbf21b3e2
7 changed files with 26 additions and 32 deletions

View File

@ -5,7 +5,8 @@
### Enhancements
* [#3294](https://github.com/netbox-community/netbox/issues/3294) - Implement mechanism for storing user preferences
* [#4531](https://github.com/netbox-community/netbox/issues/4531) - Retain user's pagination preference
* [#4421](https://github.com/netbox-community/netbox/issues/4421) - Retain user's preference for config context format
* [#4531](https://github.com/netbox-community/netbox/issues/4531) - Retain user's preference for page length
### Bug Fixes

View File

@ -119,11 +119,18 @@ class ConfigContextView(PermissionRequiredMixin, View):
permission_required = 'extras.view_configcontext'
def get(self, request, pk):
configcontext = get_object_or_404(ConfigContext, pk=pk)
# Determine user's preferred output format
if request.GET.get('format') in ['json', 'yaml']:
format = request.GET.get('format')
request.user.config.set('extras.configcontext.format', format, commit=True)
else:
format = request.user.config.get('extras.configcontext.format', 'json')
return render(request, 'extras/configcontext.html', {
'configcontext': configcontext,
'format': format,
})
@ -171,11 +178,19 @@ class ObjectConfigContextView(View):
source_contexts = ConfigContext.objects.get_for_object(obj)
model_name = self.object_class._meta.model_name
# Determine user's preferred output format
if request.GET.get('format') in ['json', 'yaml']:
format = request.GET.get('format')
request.user.config.set('extras.configcontext.format', format, commit=True)
else:
format = request.user.config.get('extras.configcontext.format', 'json')
return render(request, 'extras/object_configcontext.html', {
model_name: obj,
'obj': obj,
'rendered_context': obj.get_config_context(),
'source_contexts': source_contexts,
'format': format,
'base_template': self.base_template,
'active_tab': 'config-context',
})

View File

@ -1,11 +0,0 @@
$('.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();
}
});

View File

@ -215,13 +215,9 @@
{% include 'extras/inc/configcontext_format.html' %}
</div>
<div class="panel-body">
{% include 'extras/inc/configcontext_data.html' with data=configcontext.data %}
{% include 'extras/inc/configcontext_data.html' with data=configcontext.data format=format %}
</div>
</div>
</div>
</div>
{% endblock %}
{% block javascript %}
<script src="{% static 'js/configcontext.js' %}?v{{ settings.VERSION }}"></script>
{% endblock %}

View File

@ -1,8 +1,5 @@
{% 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 class="rendered-context-data">
<pre>{% if format == 'json' %}{{ data|render_json }}{% elif format == 'yaml' %}{{ data|render_yaml }}{% else %}{{ data }}{% endif %}</pre>
</div>

View File

@ -1,6 +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>
<a href="?format=json" class="btn btn-default{% if format == 'json' %} active{% endif %}">JSON</a>
<a href="?format=yaml" class="btn btn-default{% if format == 'yaml' %} active{% endif %}">YAML</a>
</div>
</div>

View File

@ -13,7 +13,7 @@
{% include 'extras/inc/configcontext_format.html' %}
</div>
<div class="panel-body">
{% include 'extras/inc/configcontext_data.html' with data=rendered_context %}
{% include 'extras/inc/configcontext_data.html' with data=rendered_context format=format %}
</div>
</div>
</div>
@ -24,7 +24,7 @@
</div>
<div class="panel-body">
{% if obj.local_context_data %}
{% include 'extras/inc/configcontext_data.html' with data=obj.local_context_data %}
{% include 'extras/inc/configcontext_data.html' with data=obj.local_context_data format=format %}
{% else %}
<span class="text-muted">None</span>
{% endif %}
@ -49,7 +49,7 @@
{% if context.description %}
<br /><small>{{ context.description }}</small>
{% endif %}
{% include 'extras/inc/configcontext_data.html' with data=context.data %}
{% include 'extras/inc/configcontext_data.html' with data=context.data format=format %}
</div>
{% empty %}
<div class="panel-body">
@ -60,7 +60,3 @@
</div>
</div>
{% endblock %}
{% block javascript %}
<script src="{% static 'js/configcontext.js' %}?v{{ settings.VERSION }}"></script>
{% endblock %}