Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.

Commit bb447d2

Browse files
authored
Merge pull request #118 from atb00ker/fix-api
[improvement] enable visualize api from seperate server
2 parents a554148 + 54d4d5e commit bb447d2

File tree

7 files changed

+58
-20
lines changed

7 files changed

+58
-20
lines changed

CHANGES.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Changelog
22
=========
33

4+
Version 0.6.2 [Unreleased]
5+
--------------------------
6+
7+
- Renamed api setting TOPOLOGY_RECEIVE_URLCONF -> TOPOLOGY_API_URLCONF
8+
- Renamed api setting TOPOLOGY_RECEIVE_BASEURL -> TOPOLOGY_API_BASEURL
9+
410
Version 0.6.1 [2020-02-26]
511
--------------------------
612

README.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ it will be deleted by the ``update_topology`` management command. This depends o
292292
``NETJSONGRAPH_LINK_EXPIRATION`` being enabled.
293293
Replace ``False`` with an integer to enable the feature.
294294

295-
``TOPOLOGY_RECEIVE_URLCONF``
296-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
295+
``TOPOLOGY_API_URLCONF``
296+
^^^^^^^^^^^^^^^^^^^^^^^^
297297

298298
+--------------+---------------+
299299
| **type**: | ``string`` |
@@ -304,8 +304,8 @@ Replace ``False`` with an integer to enable the feature.
304304
Use the ``urlconf`` option to change receive api url to point to
305305
another module, example, ``myapp.urls``.
306306

307-
``TOPOLOGY_RECEIVE_BASEURL``
308-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
307+
``TOPOLOGY_API_BASEURL``
308+
^^^^^^^^^^^^^^^^^^^^^^^^
309309

310310
+--------------+---------------+
311311
| **type**: | ``string`` |

django_netjsongraph/base/admin.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from .. import settings as app_settings
1212
from ..contextmanagers import log_failure
13+
from ..visualizer import GraphVisualizerUrls
1314

1415

1516
class TimeStampedEditableAdmin(ModelAdmin):
@@ -37,7 +38,7 @@ class Media:
3738
static('netjsongraph/js/visualize.js')]
3839

3940

40-
class AbstractTopologyAdmin(BaseAdmin, ReceiveUrlAdmin):
41+
class AbstractTopologyAdmin(BaseAdmin, ReceiveUrlAdmin, GraphVisualizerUrls):
4142
list_display = ['label', 'parser', 'strategy', 'published', 'created', 'modified']
4243
readonly_fields = ['protocol', 'version', 'revision', 'metric', 'receive_url']
4344
list_filter = ['parser', 'strategy']
@@ -47,8 +48,8 @@ class AbstractTopologyAdmin(BaseAdmin, ReceiveUrlAdmin):
4748
'expiration_time', 'receive_url', 'published', 'protocol',
4849
'version', 'revision', 'metric', 'created']
4950
receive_url_name = 'receive_topology'
50-
receive_url_urlconf = app_settings.TOPOLOGY_RECEIVE_URLCONF
51-
receive_url_baseurl = app_settings.TOPOLOGY_RECEIVE_BASEURL
51+
receive_url_urlconf = app_settings.TOPOLOGY_API_URLCONF
52+
receive_url_baseurl = app_settings.TOPOLOGY_API_BASEURL
5253

5354
def get_actions(self, request):
5455
"""
@@ -135,8 +136,7 @@ def unpublish_selected(self, request, queryset):
135136
unpublish_selected.short_description = _('Unpublish selected items')
136137

137138
def visualize_view(self, request, pk):
138-
graph_url = reverse('network_graph', args=[pk])
139-
history_url = reverse('network_graph_history', args=[pk])
139+
graph_url, history_url = self.get_graph_urls(request, pk)
140140
context = self.admin_site.each_context(request)
141141
opts = self.model._meta
142142
context.update({

django_netjsongraph/settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
LINK_EXPIRATION = getattr(settings, 'NETJSONGRAPH_LINK_EXPIRATION', 60)
1616
NODE_EXPIRATION = getattr(settings, 'NETJSONGRAPH_NODE_EXPIRATION', False)
1717
VISUALIZER_CSS = getattr(settings, 'NETJSONGRAPH_VISUALIZER_CSS', 'netjsongraph/css/style.css')
18-
TOPOLOGY_RECEIVE_URLCONF = getattr(settings, 'TOPOLOGY_RECEIVE_URLCONF', None)
19-
TOPOLOGY_RECEIVE_BASEURL = getattr(settings, 'TOPOLOGY_RECEIVE_BASEURL', None)
18+
TOPOLOGY_API_URLCONF = getattr(settings, 'TOPOLOGY_API_URLCONF', None)
19+
TOPOLOGY_API_BASEURL = getattr(settings, 'TOPOLOGY_API_BASEURL', None)

django_netjsongraph/static/netjsongraph/js/topology-history.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ window.initTopologyHistory = function ($) {
44
today = new Date(),
55
apiUrl = datepicker.attr('data-history-api');
66
today.setHours(0, 0, 0, 0);
7-
datepicker.datepicker({dateFormat: 'dd/mm/yy'});
7+
datepicker.datepicker({ dateFormat: 'dd/mm/yy' });
88
datepicker.datepicker('setDate', today);
99
datepicker.change(function () {
1010
var date = datepicker.val().split('/').reverse().join('-'),
@@ -13,10 +13,15 @@ window.initTopologyHistory = function ($) {
1313
if (datepicker.datepicker('getDate').getTime() === today.getTime()) {
1414
url = window.__njg_default_url__;
1515
}
16-
$.getJSON(url).done(function (data) {
17-
window.graph = window.loadNetJsonGraph(data);
18-
}).error(function (xhr) {
19-
alert(xhr.responseJSON.detail);
16+
$.ajax({
17+
url: url,
18+
dataType: 'json',
19+
success: function (data) {
20+
window.graph = window.loadNetJsonGraph(data);
21+
},
22+
error: function (xhr) {
23+
alert(xhr.responseJSON.detail);
24+
}
2025
});
2126
});
2227
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from django.urls import reverse
2+
3+
from ..settings import TOPOLOGY_API_BASEURL, TOPOLOGY_API_URLCONF
4+
5+
6+
class GraphVisualizerUrls:
7+
8+
def get_graph_urls(self, request, pk):
9+
graph_path = reverse('network_graph',
10+
urlconf=TOPOLOGY_API_URLCONF,
11+
args=[pk])
12+
history_path = reverse('network_graph_history',
13+
urlconf=TOPOLOGY_API_URLCONF,
14+
args=[pk])
15+
if TOPOLOGY_API_BASEURL:
16+
graph_url = '{}{}'.format(TOPOLOGY_API_BASEURL, graph_path)
17+
history_url = '{}{}'.format(TOPOLOGY_API_BASEURL, history_path)
18+
else:
19+
graph_url = '{0}://{1}{2}'.format(request.scheme,
20+
request.get_host(),
21+
graph_path)
22+
history_url = '{0}://{1}{2}'.format(request.scheme,
23+
request.get_host(),
24+
history_path)
25+
return graph_url, history_url
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from django.shortcuts import render
2-
from django.urls import reverse
32
from django.views import View
43

54
from ..settings import VISUALIZER_CSS
65
from ..utils import get_object_or_404
6+
from . import GraphVisualizerUrls
77

88

99
class BaseTopologyListView(View):
@@ -14,12 +14,14 @@ def get(self, request):
1414
'VISUALIZER_CSS': VISUALIZER_CSS})
1515

1616

17-
class BaseTopologyDetailView(View):
17+
class BaseTopologyDetailView(View, GraphVisualizerUrls):
18+
1819
def get(self, request, pk):
1920
topology = get_object_or_404(self.topology_model, pk)
21+
graph_url, history_url = self.get_graph_urls(request, pk)
2022
return render(request, 'netjsongraph/detail.html', {
2123
'topology': topology,
22-
'graph_url': reverse('network_graph', args=[topology.pk]),
23-
'history_url': reverse('network_graph_history', args=[topology.pk]),
24+
'graph_url': graph_url,
25+
'history_url': history_url,
2426
'VISUALIZER_CSS': VISUALIZER_CSS
2527
})

0 commit comments

Comments
 (0)