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

Commit e20f2a3

Browse files
authored
Merge pull request #113 from atb00ker/fix-api
[enchancement] Add URLCONF & BASEURL options
2 parents df7605d + 00e3502 commit e20f2a3

File tree

8 files changed

+81
-17
lines changed

8 files changed

+81
-17
lines changed

CHANGES.rst

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

4+
Version 0.6.1 [unreleased]
5+
--------------------------
6+
7+
- Added settings option TOPOLOGY_RECEIVE_URLCONF & TOPOLOGY_RECEIVE_BASEURL
8+
49
Version 0.6.0 [2020-02-07]
510
--------------------------
611

README.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,32 @@ 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+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
297+
298+
+--------------+---------------+
299+
| **type**: | ``string`` |
300+
+--------------+---------------+
301+
| **default**: | ``None`` |
302+
+--------------+---------------+
303+
304+
Use the ``urlconf`` option to change receive api url to point to
305+
another module, example, ``myapp.urls``.
306+
307+
``TOPOLOGY_RECEIVE_BASEURL``
308+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
309+
310+
+--------------+---------------+
311+
| **type**: | ``string`` |
312+
+--------------+---------------+
313+
| **default**: | ``None`` |
314+
+--------------+---------------+
315+
316+
If you have a seperate instanse of django-netjsongraph to
317+
which you want to point your receive url to, you can use
318+
this option to change the base of the url,
319+
example: ``https://mytopology.myapp.com``.
320+
295321
Overriding visualizer templates
296322
-------------------------------
297323

django_netjsongraph/base/admin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from django.utils.translation import ugettext_lazy as _
99
from openwisp_utils.admin import ReceiveUrlAdmin
1010

11+
from .. import settings as app_settings
1112
from ..contextmanagers import log_failure
1213

1314

@@ -46,6 +47,8 @@ class AbstractTopologyAdmin(BaseAdmin, ReceiveUrlAdmin):
4647
'expiration_time', 'receive_url', 'published', 'protocol',
4748
'version', 'revision', 'metric', 'created']
4849
receive_url_name = 'receive_topology'
50+
receive_url_urlconf = app_settings.TOPOLOGY_RECEIVE_URLCONF
51+
receive_url_baseurl = app_settings.TOPOLOGY_RECEIVE_BASEURL
4952

5053
def get_actions(self, request):
5154
"""

django_netjsongraph/base/link.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from openwisp_utils.base import TimeStampedEditableModel
1414
from rest_framework.utils.encoders import JSONEncoder
1515

16-
from .. import settings
16+
from .. import settings as app_settings
1717
from ..utils import link_status_changed, print_info
1818

1919

@@ -115,7 +115,7 @@ def delete_expired_links(cls):
115115
deletes links that have been down for more than
116116
``NETJSONGRAPH_LINK_EXPIRATION`` days
117117
"""
118-
LINK_EXPIRATION = settings.LINK_EXPIRATION
118+
LINK_EXPIRATION = app_settings.LINK_EXPIRATION
119119
if LINK_EXPIRATION not in [False, None]:
120120
expiration_date = now() - timedelta(days=int(LINK_EXPIRATION))
121121
expired_links = cls.objects.filter(status='down',

django_netjsongraph/base/node.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from openwisp_utils.base import TimeStampedEditableModel
1010
from rest_framework.utils.encoders import JSONEncoder
1111

12-
from .. import settings
12+
from .. import settings as app_settings
1313
from ..utils import print_info
1414

1515

@@ -101,8 +101,8 @@ def delete_expired_nodes(cls):
101101
deletes nodes that have not been connected to the network
102102
for more than ``NETJSONGRAPH__EXPIRATION`` days
103103
"""
104-
NODE_EXPIRATION = settings.NODE_EXPIRATION
105-
LINK_EXPIRATION = settings.LINK_EXPIRATION
104+
NODE_EXPIRATION = app_settings.NODE_EXPIRATION
105+
LINK_EXPIRATION = app_settings.LINK_EXPIRATION
106106
if NODE_EXPIRATION not in [False, None] and LINK_EXPIRATION not in [False, None]:
107107
expiration_date = now() - timedelta(days=int(NODE_EXPIRATION))
108108
expired_nodes = cls.objects.filter(modified__lt=expiration_date,

django_netjsongraph/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +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)

django_netjsongraph/tests/base/test_admin.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
from django.urls import reverse
44

55
from ...apps import DjangoNetjsongraphConfig as appconfig
6+
from ...base.admin import AbstractTopologyAdmin
67
from ..utils import LoadMixin
78

89

910
class TestAdminMixin(LoadMixin):
11+
module = 'django_netjsongraph'
1012
fixtures = [
1113
'test_users.json'
1214
]
@@ -95,6 +97,32 @@ def test_topology_receive_url(self):
9597
self.assertEqual(response.status_code, 200)
9698
self.assertContains(response, 'field-receive_url')
9799

100+
def test_custom_topology_receive_url(self):
101+
t = self.topology_model.objects.first()
102+
t.strategy = 'receive'
103+
t.save()
104+
path = reverse('{0}_topology_change'.format(self.prefix), args=[t.pk])
105+
# No change in URL Test
106+
response = self.client.get(path)
107+
self.assertEqual(response.status_code, 200)
108+
self.assertContains(response, 'field-receive_url')
109+
self.assertContains(response, 'http://testserver/api/receive')
110+
# Change URL Test
111+
AbstractTopologyAdmin.receive_url_baseurl = 'http://changedurlbase'
112+
response = self.client.get(path)
113+
self.assertEqual(response.status_code, 200)
114+
self.assertContains(response, 'field-receive_url')
115+
self.assertContains(response, 'http://changedurlbase/api/receive')
116+
# Change URLConf Test
117+
AbstractTopologyAdmin.receive_url_urlconf = '{}.api.urls'.format(self.module)
118+
response = self.client.get(path)
119+
self.assertEqual(response.status_code, 200)
120+
self.assertContains(response, 'field-receive_url')
121+
self.assertContains(response, 'http://changedurlbase/receive')
122+
# Reset test options
123+
AbstractTopologyAdmin.receive_url_baseurl = None
124+
AbstractTopologyAdmin.receive_url_urlconf = None
125+
98126
def test_node_change_form(self):
99127
n = self.node_model.objects.first()
100128
path = reverse('{0}_node_change'.format(self.prefix), args=[n.pk])

django_netjsongraph/tests/utils.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from django.test.runner import DiscoverRunner
1212
from django.utils.timezone import now
1313

14-
from .. import settings
14+
from .. import settings as app_settings
1515

1616

1717
@contextmanager
@@ -134,7 +134,7 @@ def test_delete_expired_links(self):
134134
t.parser = 'netdiff.NetJsonParser'
135135
t.save()
136136
# should not delete
137-
almost_expired_date = now() - timedelta(days=settings.LINK_EXPIRATION-10)
137+
almost_expired_date = now() - timedelta(days=app_settings.LINK_EXPIRATION-10)
138138
n1 = self.node_model.objects.all()[0]
139139
n2 = self.node_model.objects.all()[1]
140140
link = self._create_link(source=n1,
@@ -160,7 +160,7 @@ def test_delete_expired_links(self):
160160
self.assertEqual(self.node_model.objects.count(), 2)
161161
self.assertEqual(self.link_model.objects.count(), 1)
162162
# should delete
163-
expired_date = now() - timedelta(days=settings.LINK_EXPIRATION+10)
163+
expired_date = now() - timedelta(days=app_settings.LINK_EXPIRATION+10)
164164
self.link_model.objects.filter(pk=link.pk).update(created=expired_date,
165165
modified=expired_date)
166166
self.topology_model.update_all('testnetwork')
@@ -169,10 +169,10 @@ def test_delete_expired_links(self):
169169

170170
@responses.activate
171171
def test_delete_expired_nodes(self):
172-
NODE_EXPIRATION = getattr(settings, 'NODE_EXPIRATION')
172+
NODE_EXPIRATION = getattr(app_settings, 'NODE_EXPIRATION')
173173
# Test with the default value(False)
174174
# Should not delete
175-
setattr(settings, 'NODE_EXPIRATION', False)
175+
setattr(app_settings, 'NODE_EXPIRATION', False)
176176
t = self.topology_model.objects.first()
177177
t.parser = 'netdiff.NetJsonParser'
178178
t.save()
@@ -200,8 +200,8 @@ def test_delete_expired_nodes(self):
200200

201201
# Test with a custom value
202202
# Should delete
203-
setattr(settings, 'NODE_EXPIRATION', 60)
204-
expired_date = now() - timedelta(days=settings.NODE_EXPIRATION+10)
203+
setattr(app_settings, 'NODE_EXPIRATION', 60)
204+
expired_date = now() - timedelta(days=app_settings.NODE_EXPIRATION+10)
205205
self.node_model.objects.filter(pk=n1.pk).update(created=expired_date,
206206
modified=expired_date)
207207
self.node_model.objects.filter(pk=n2.pk).update(created=expired_date,
@@ -210,7 +210,7 @@ def test_delete_expired_nodes(self):
210210
self.assertEqual(self.node_model.objects.count(), 0)
211211
self.assertEqual(self.link_model.objects.count(), 0)
212212
# Set the setting to it's original value
213-
setattr(settings, 'NODE_EXPIRATION', NODE_EXPIRATION)
213+
setattr(app_settings, 'NODE_EXPIRATION', NODE_EXPIRATION)
214214

215215
@responses.activate
216216
def test_delete_expired_disabled(self):
@@ -224,7 +224,7 @@ def test_delete_expired_disabled(self):
224224
cost=1,
225225
status='down',
226226
topology=t)
227-
expired_date = now() - timedelta(days=settings.LINK_EXPIRATION+10)
227+
expired_date = now() - timedelta(days=app_settings.LINK_EXPIRATION+10)
228228
self.link_model.objects.filter(pk=link.pk).update(created=expired_date,
229229
modified=expired_date)
230230
empty_topology = json.dumps({
@@ -239,12 +239,12 @@ def test_delete_expired_disabled(self):
239239
'http://127.0.0.1:9090',
240240
body=empty_topology,
241241
content_type='application/json')
242-
ORIGINAL_LINK_EXPIRATION = int(settings.LINK_EXPIRATION)
243-
settings.LINK_EXPIRATION = False
242+
ORIGINAL_LINK_EXPIRATION = int(app_settings.LINK_EXPIRATION)
243+
app_settings.LINK_EXPIRATION = False
244244
self.topology_model.update_all('testnetwork')
245245
self.assertEqual(self.node_model.objects.count(), 2)
246246
self.assertEqual(self.link_model.objects.count(), 1)
247-
settings.LINK_EXPIRATION = ORIGINAL_LINK_EXPIRATION
247+
app_settings.LINK_EXPIRATION = ORIGINAL_LINK_EXPIRATION
248248

249249
def test_save_snapshot_all_method(self, **kwargs):
250250
self.assertEqual(self.snapshot_model.objects.count(), 0)

0 commit comments

Comments
 (0)