Skip to content

Commit 7608e78

Browse files
committed
[fix] Fixed bug in preview when device has no name
The get_config method wasn't checking if self.name is defined before using it.
1 parent afbbbdf commit 7608e78

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

openwisp_controller/config/base/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def get_config(self):
8989
return config
9090
c = deepcopy(config)
9191
is_config = not any([self.__template__, self.__vpn__])
92-
if 'hostname' not in c.get('general', {}) and is_config:
92+
if all(('hostname' not in c.get('general', {}), is_config, self.name)):
9393
c.setdefault('general', {})
9494
c['general']['hostname'] = self.name.replace(':', '-')
9595
return c

openwisp_controller/config/tests/test_config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,11 @@ def test_auto_hostname(self):
333333
c.refresh_from_db()
334334
self.assertDictEqual(c.config, {'general': {}})
335335

336+
with self.subTest('missing name shall not raise exception'):
337+
c.device.name = None
338+
del c.backend_instance
339+
self.assertDictEqual(c.backend_instance.config, {'general': {}})
340+
336341
def test_config_context(self):
337342
config = {
338343
'general': {

0 commit comments

Comments
 (0)