Skip to content
This repository has been archived by the owner on Jul 9, 2020. It is now read-only.

Commit

Permalink
Merge 2bc73f3 into 4cd6099
Browse files Browse the repository at this point in the history
  • Loading branch information
R9295 committed Nov 7, 2018
2 parents 4cd6099 + 2bc73f3 commit 82a9e24
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 1 deletion.
4 changes: 3 additions & 1 deletion django_netjsonconfig/base/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ class AbstractDeviceAdmin(BaseConfigAdmin):
'os',
'system',
'notes',
'context',
'created',
'modified']
if app_settings.HARDWARE_ID_ENABLED:
Expand Down Expand Up @@ -317,7 +318,8 @@ def _get_preview_instance(self, request):
c.device = self.model(id=request.POST.get('id'),
name=request.POST.get('name'),
mac_address=request.POST.get('mac_address'),
key=request.POST.get('key'))
key=request.POST.get('key'),
context=request.POST.get('context') or {})
return c


Expand Down
2 changes: 2 additions & 0 deletions django_netjsonconfig/base/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ def get_context(self):
'name': self.name,
'mac_address': self.mac_address
})
if self.device.context:
c.update(self.device.context)
c.update(app_settings.CONTEXT)
if app_settings.HARDWARE_ID_ENABLED and self._has_device():
c.update({'hardware_id': self.device.hardware_id})
Expand Down
7 changes: 7 additions & 0 deletions django_netjsonconfig/base/device.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.db import models
from django.utils.translation import ugettext_lazy as _
from jsonfield import JSONField

from .. import settings as app_settings
from ..utils import get_random_key
Expand Down Expand Up @@ -50,6 +51,12 @@ class AbstractDevice(BaseModel):
help_text=_('ip address of the management interface, '
'if available'))
hardware_id = models.CharField(**(app_settings.HARDWARE_ID_OPTIONS))
context = JSONField(null=True,
blank=True,
help_text=_('Additional '
'<a href="http://netjsonconfig.openwisp.org/'
'en/stable/general/basics.html#context">'
'context</a> for the configuration.'))

class Meta:
abstract = True
Expand Down
19 changes: 19 additions & 0 deletions django_netjsonconfig/migrations/0037_device_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 2.1.2 on 2018-11-07 16:42

from django.db import migrations
import jsonfield.fields


class Migration(migrations.Migration):

dependencies = [
('django_netjsonconfig', '0036_device_hardware_id'),
]

operations = [
migrations.AddField(
model_name='device',
name='context',
field=jsonfield.fields.JSONField(blank=True, help_text='Additional <a href="http://netjsonconfig.openwisp.org/en/stable/general/basics.html#context">context</a> for the configuration.', null=True),
),
]
27 changes: 27 additions & 0 deletions django_netjsonconfig/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,30 @@ def test_error_if_download_config(self):
d = self._create_device()
res = self.client.get(reverse('admin:django_netjsonconfig_device_change', args=[d.pk]))
self.assertNotContains(res, 'Download configuration')

def test_preview_device_with_context(self):
path = reverse('admin:django_netjsonconfig_device_preview')
config = json.dumps({
'openwisp': [
{
"config_name": "controller",
"config_value": "http",
"url": "http://controller.examplewifiservice.com",
"interval": "{{ interval }}",
"verify_ssl": "1",
"uuid": "UUID",
"key": self.TEST_KEY
}
]
})
data = {
'id': 'd60ecd62-5d00-4e7b-bd16-6fc64a95e60c',
'name': 'test-asd',
'mac_address': self.TEST_MAC_ADDRESS,
'backend': 'netjsonconfig.OpenWrt',
'config': config,
'csrfmiddlewaretoken': 'test',
'additional_context': '{"interval": "60"}'
}
response = self.client.post(path, data)
self.assertContains(response, '60')
22 changes: 22 additions & 0 deletions django_netjsonconfig/tests/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,25 @@ def test_bad_hostnames(self):
e.message_dict['name'][0])
else:
self.fail('ValidationError not raised for "{0}"'.format(host))

def test_add_device_with_context(self):
d = self._create_device()
d.context = {
'interval': '60'
}
d.save()
c = self._create_config(device=d, config={
"openwisp": [
{
"config_name": "controller",
"config_value": "http",
"url": "http://controller.examplewifiservice.com",
"interval": "{{ interval }}",
"verify_ssl": "1",
"uuid": "UUID",
"key": self.TEST_KEY
}
]
})
self.assertEqual(c.json(dict=True)['openwisp'][0]['interval'],
'60')

0 comments on commit 82a9e24

Please sign in to comment.