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

Commit d47b251

Browse files
authored
Merge pull request #109 from waleko/master
[django] Generalize uuid/key/receive_url logic (openwisp/openwisp-utils#40)
2 parents bcca2f3 + 110cb71 commit d47b251

File tree

12 files changed

+32
-62
lines changed

12 files changed

+32
-62
lines changed

django_netjsongraph/base/admin.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from django.templatetags.static import static
77
from django.urls import reverse
88
from django.utils.translation import ugettext_lazy as _
9+
from openwisp_utils.admin import ReceiveUrlAdmin
910

1011
from ..contextmanagers import log_failure
1112

@@ -30,13 +31,12 @@ class Media:
3031
js = [static('netjsongraph/js/lib/d3.min.js'),
3132
static('netjsongraph/js/lib/jquery-ui.min.js'),
3233
static('netjsongraph/js/src/netjsongraph.js'),
33-
static('netjsongraph/js/receive-url.js'),
3434
static('netjsongraph/js/strategy-switcher.js'),
3535
static('netjsongraph/js/topology-history.js'),
3636
static('netjsongraph/js/visualize.js')]
3737

3838

39-
class AbstractTopologyAdmin(BaseAdmin):
39+
class AbstractTopologyAdmin(BaseAdmin, ReceiveUrlAdmin):
4040
list_display = ['label', 'parser', 'strategy', 'published', 'created', 'modified']
4141
readonly_fields = ['protocol', 'version', 'revision', 'metric', 'receive_url']
4242
list_filter = ['parser', 'strategy']
@@ -45,12 +45,7 @@ class AbstractTopologyAdmin(BaseAdmin):
4545
fields = ['label', 'parser', 'strategy', 'url', 'key',
4646
'expiration_time', 'receive_url', 'published', 'protocol',
4747
'version', 'revision', 'metric', 'created']
48-
49-
def receive_url(self, obj):
50-
url = reverse('receive_topology', kwargs={'pk': obj.pk})
51-
return '{0}?key={1}'.format(url, obj.key)
52-
53-
receive_url.short_description = _('receive url')
48+
receive_url_name = 'receive_topology'
5449

5550
def get_actions(self, request):
5651
"""

django_netjsongraph/base/base.py

-18
This file was deleted.

django_netjsongraph/base/link.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
from jsonfield import JSONField
1111
from model_utils import Choices
1212
from model_utils.fields import StatusField
13+
from openwisp_utils.base import TimeStampedEditableModel
1314
from rest_framework.utils.encoders import JSONEncoder
1415

1516
from .. import settings
1617
from ..utils import link_status_changed, print_info
17-
from .base import TimeStampedEditableModel
1818

1919

2020
class AbstractLink(TimeStampedEditableModel):

django_netjsongraph/base/node.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
from django.utils.functional import cached_property
77
from django.utils.timezone import now
88
from jsonfield import JSONField
9+
from openwisp_utils.base import TimeStampedEditableModel
910
from rest_framework.utils.encoders import JSONEncoder
1011

1112
from .. import settings
1213
from ..utils import print_info
13-
from .base import TimeStampedEditableModel
1414

1515

1616
class AbstractNode(TimeStampedEditableModel):

django_netjsongraph/base/snapshot.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from django.db import models
22
from django.utils.translation import ugettext_lazy as _
3-
4-
from .base import TimeStampedEditableModel
3+
from openwisp_utils.base import TimeStampedEditableModel
54

65

76
class AbstractSnapshot(TimeStampedEditableModel):

django_netjsongraph/base/topology.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
from django.utils.timezone import now
1111
from django.utils.translation import ugettext_lazy as _
1212
from netdiff import NetJsonParser, diff
13+
from openwisp_utils.base import KeyField, TimeStampedEditableModel
1314
from rest_framework.utils.encoders import JSONEncoder
1415

1516
from ..contextmanagers import log_failure
1617
from ..settings import PARSERS, TIMEOUT
17-
from ..utils import get_random_key, print_info
18-
from .base import TimeStampedEditableModel
18+
from ..utils import print_info
1919

2020
STRATEGIES = (
2121
('fetch', _('FETCH')),
@@ -42,13 +42,11 @@ class AbstractTopology(TimeStampedEditableModel):
4242
' (FETCH strategy)')
4343
)
4444
# receive strategy
45-
key = models.CharField(
46-
_('key'),
47-
blank=True,
48-
max_length=64,
49-
default=get_random_key,
50-
help_text=_('key needed to update topology from nodes ')
51-
)
45+
key = KeyField(unique=False,
46+
db_index=False,
47+
help_text=_('key needed to update topology from nodes '),
48+
verbose_name=_('key'),
49+
blank=True)
5250
# receive strategy
5351
expiration_time = models.PositiveIntegerField(
5452
_('expiration time'),
@@ -275,7 +273,7 @@ def link_status_changed(self, link, status):
275273
return False
276274
# if using fetch strategy or
277275
# using receive strategy and link is coming back up or
278-
# receive strategy and ``expiration_time is 0``
276+
# receive strategy and ``expiration_time == 0``
279277
elif self.strategy == 'fetch' or status == 'up' or self.expiration_time == 0:
280278
return True
281279
# if using receive strategy and expiration_time of link has expired

django_netjsongraph/migrations/0003_topology_receive_strategy.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# -*- coding: utf-8 -*-
22
# Generated by Django 1.9 on 2016-01-24 14:35
3-
import django_netjsongraph.models
3+
from __future__ import unicode_literals
4+
5+
import re
6+
7+
import django.core.validators
8+
import openwisp_utils.base
9+
import openwisp_utils.utils
410
from django.db import migrations, models
511

612

@@ -24,6 +30,6 @@ class Migration(migrations.Migration):
2430
migrations.AddField(
2531
model_name='topology',
2632
name='key',
27-
field=models.CharField(blank=True, default=django_netjsongraph.utils.get_random_key, help_text='key needed to update topology from nodes ', max_length=64, verbose_name='key'),
28-
)
33+
field=openwisp_utils.base.KeyField(blank=True, default=openwisp_utils.utils.get_random_key, help_text='key needed to update topology from nodes ', max_length=64, validators=[django.core.validators.RegexValidator(re.compile('^[^\\s/\\.]+$'), code='invalid', message='This value must not contain spaces, dots or slashes.')], verbose_name='key'),
34+
),
2935
]

django_netjsongraph/static/netjsongraph/js/receive-url.js

-12
This file was deleted.

django_netjsongraph/utils.py

-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from django.dispatch import Signal
66
from django.http import Http404
77
from django.shortcuts import get_object_or_404 as get_obj_or_404
8-
from django.utils.crypto import get_random_string
98

109
link_status_changed = Signal(providing_args=["link"])
1110

@@ -32,10 +31,6 @@ def get_object_or_404(model, pk, **kwargs):
3231
raise Http404()
3332

3433

35-
def get_random_key():
36-
return get_random_string(length=32)
37-
38-
3934
def get_api_urls(views_module):
4035
"""
4136
used by third party apps to reduce boilerplate

requirements-test.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
coveralls
22
responses
33
freezegun
4-
openwisp-utils[qa]>=0.3.0
4+
openwisp-utils[qa]>=0.4.1

requirements.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
django>=2.2,<3.1
22
djangorestframework>=3.3,<3.12
33
django-model-utils
4+
45
netdiff>=0.7.0,<0.8.0
5-
openwisp-utils>=0.4.0,<0.5.0
6+
openwisp-utils>=0.4.1,<0.5.0
67
jsonfield2

tests/settings.py

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
'rest_framework'
3030
]
3131

32+
STATICFILES_FINDERS = [
33+
'django.contrib.staticfiles.finders.FileSystemFinder',
34+
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
35+
'openwisp_utils.staticfiles.DependencyFinder',
36+
]
37+
3238
MIDDLEWARE = [
3339
'django.middleware.security.SecurityMiddleware',
3440
'django.contrib.sessions.middleware.SessionMiddleware',

0 commit comments

Comments
 (0)