mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-24 17:38:37 -06:00
Standardized graph display; added templatization for graph links
This commit is contained in:
parent
9de8ac91a1
commit
39330850b3
@ -5,10 +5,14 @@ from extras.models import Graph
|
|||||||
|
|
||||||
class GraphSerializer(serializers.ModelSerializer):
|
class GraphSerializer(serializers.ModelSerializer):
|
||||||
embed_url = serializers.SerializerMethodField()
|
embed_url = serializers.SerializerMethodField()
|
||||||
|
embed_link = serializers.SerializerMethodField()
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Graph
|
model = Graph
|
||||||
fields = ['name', 'embed_url', 'link']
|
fields = ['name', 'embed_url', 'embed_link']
|
||||||
|
|
||||||
def get_embed_url(self, obj):
|
def get_embed_url(self, obj):
|
||||||
return obj.embed_url(self.context['graphed_object'])
|
return obj.embed_url(self.context['graphed_object'])
|
||||||
|
|
||||||
|
def get_embed_link(self, obj):
|
||||||
|
return obj.embed_link(self.context['graphed_object'])
|
||||||
|
@ -56,6 +56,12 @@ class Graph(models.Model):
|
|||||||
template = Template(self.source)
|
template = Template(self.source)
|
||||||
return template.render(Context({'obj': obj}))
|
return template.render(Context({'obj': obj}))
|
||||||
|
|
||||||
|
def embed_link(self, obj):
|
||||||
|
if self.link is None:
|
||||||
|
return ''
|
||||||
|
template = Template(self.link)
|
||||||
|
return template.render(Context({'obj': obj}))
|
||||||
|
|
||||||
|
|
||||||
class ExportTemplate(models.Model):
|
class ExportTemplate(models.Model):
|
||||||
content_type = models.ForeignKey(ContentType, limit_choices_to={'model__in': EXPORTTEMPLATE_MODELS})
|
content_type = models.ForeignKey(ContentType, limit_choices_to={'model__in': EXPORTTEMPLATE_MODELS})
|
||||||
|
26
netbox/project-static/js/graphs.js
Normal file
26
netbox/project-static/js/graphs.js
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
$('#graphs_modal').on('show.bs.modal', function (event) {
|
||||||
|
var button = $(event.relatedTarget);
|
||||||
|
var obj = button.data('obj');
|
||||||
|
var url = button.data('url');
|
||||||
|
var modal_title = $(this).find('.modal-title');
|
||||||
|
var modal_body = $(this).find('.modal-body');
|
||||||
|
modal_title.text(obj);
|
||||||
|
modal_body.empty();
|
||||||
|
$.ajax({
|
||||||
|
url: url,
|
||||||
|
dataType: 'json',
|
||||||
|
success: function(json) {
|
||||||
|
$.each(json, function(i, graph) {
|
||||||
|
// Build in a 500ms delay per graph to avoid hammering the server
|
||||||
|
setTimeout(function() {
|
||||||
|
modal_body.append('<h4 class="text-center">' + graph.name + '</h4>');
|
||||||
|
if (graph.embed_link) {
|
||||||
|
modal_body.append('<a href="' + graph.embed_link + '"><img src="' + graph.embed_url + '" /></a>');
|
||||||
|
} else {
|
||||||
|
modal_body.append('<img src="' + graph.embed_url + '" />');
|
||||||
|
}
|
||||||
|
}, i*500);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
@ -396,32 +396,7 @@ $(".powerport-toggle").click(function() {
|
|||||||
$(".interface-toggle").click(function() {
|
$(".interface-toggle").click(function() {
|
||||||
return toggleConnection($(this), "/api/dcim/interface-connections/");
|
return toggleConnection($(this), "/api/dcim/interface-connections/");
|
||||||
});
|
});
|
||||||
$('#graphs_modal').on('show.bs.modal', function (event) {
|
|
||||||
var button = $(event.relatedTarget);
|
|
||||||
var iface = button.data('interface');
|
|
||||||
var iface_id = button.data('id');
|
|
||||||
var modal_title = $(this).find('.modal-title');
|
|
||||||
var modal_body = $(this).find('.modal-body');
|
|
||||||
modal_title.text('{{ device.name }} - ' + iface);
|
|
||||||
modal_body.empty();
|
|
||||||
$.ajax({
|
|
||||||
url: "/api/dcim/interfaces/" + iface_id + "/graphs/",
|
|
||||||
dataType: 'json',
|
|
||||||
success: function(json) {
|
|
||||||
$.each(json, function(i, graph) {
|
|
||||||
// Build in a 500ms delay per graph to avoid hammering the server
|
|
||||||
setTimeout(function() {
|
|
||||||
modal_body.append('<h4 class="text-center">' + graph.name + '</h4>');
|
|
||||||
if (graph.link) {
|
|
||||||
modal_body.append('<a href="' + graph.link + '"><img src="' + graph.embed_url + '" /></a>');
|
|
||||||
} else {
|
|
||||||
modal_body.append('<img src="' + graph.embed_url + '" />');
|
|
||||||
}
|
|
||||||
}, i*500);
|
|
||||||
})
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
</script>
|
</script>
|
||||||
|
<script src="{% static 'js/graphs.js' %}"></script>
|
||||||
<script src="{% static 'js/secrets.js' %}"></script>
|
<script src="{% static 'js/secrets.js' %}"></script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
@ -25,7 +25,7 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
<td class="text-right">
|
<td class="text-right">
|
||||||
{% if iface.circuit or iface.connection %}
|
{% if iface.circuit or iface.connection %}
|
||||||
<button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#graphs_modal" data-interface="{{ iface.name }}" data-id="{{ iface.id }}" title="Show graphs">
|
<button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#graphs_modal" data-obj="{{ device.name }} - {{ iface.name }}" data-url="{% url 'dcim-api:interface_graphs' pk=iface.pk %}" title="Show graphs">
|
||||||
<i class="glyphicon glyphicon-signal" aria-hidden="true"></i>
|
<i class="glyphicon glyphicon-signal" aria-hidden="true"></i>
|
||||||
</button>
|
</button>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
{% extends '_base.html' %}
|
{% extends '_base.html' %}
|
||||||
|
{% load static from staticfiles %}
|
||||||
{% load render_table from django_tables2 %}
|
{% load render_table from django_tables2 %}
|
||||||
{% load helpers %}
|
{% load helpers %}
|
||||||
|
|
||||||
@ -6,7 +7,7 @@
|
|||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<div class="pull-right">
|
<div class="pull-right">
|
||||||
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#graphs_modal" data-site="{{ site.name }}" data-id="{{ site.id }}" title="Show graphs">
|
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#graphs_modal" data-obj="{{ site.name }}" data-url="{% url 'dcim-api:site_graphs' pk=site.pk %}" title="Show graphs">
|
||||||
<i class="glyphicon glyphicon-signal" aria-hidden="true"></i>
|
<i class="glyphicon glyphicon-signal" aria-hidden="true"></i>
|
||||||
Graphs
|
Graphs
|
||||||
</button>
|
</button>
|
||||||
@ -159,32 +160,5 @@
|
|||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block javascript %}
|
{% block javascript %}
|
||||||
<script type="text/javascript">
|
<script src="{% static 'js/graphs.js' %}"></script>
|
||||||
$('#graphs_modal').on('show.bs.modal', function (event) {
|
|
||||||
var button = $(event.relatedTarget);
|
|
||||||
var site = button.data('site');
|
|
||||||
var site_id = button.data('id');
|
|
||||||
var modal_title = $(this).find('.modal-title');
|
|
||||||
var modal_body = $(this).find('.modal-body');
|
|
||||||
modal_title.text(site);
|
|
||||||
modal_body.empty();
|
|
||||||
$.ajax({
|
|
||||||
url: "/api/dcim/sites/" + site_id + "/graphs/",
|
|
||||||
dataType: 'json',
|
|
||||||
success: function(json) {
|
|
||||||
$.each(json, function(i, graph) {
|
|
||||||
// Build in a 500ms delay per graph to avoid hammering the server
|
|
||||||
setTimeout(function() {
|
|
||||||
modal_body.append('<h4 class="text-center">' + graph.name + '</h4>');
|
|
||||||
if (graph.link) {
|
|
||||||
modal_body.append('<a href="' + graph.link + '"><img src="' + graph.embed_url + '" /></a>');
|
|
||||||
} else {
|
|
||||||
modal_body.append('<img src="' + graph.embed_url + '" />');
|
|
||||||
}
|
|
||||||
}, i*500);
|
|
||||||
})
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
Loading…
Reference in New Issue
Block a user