diff --git a/openwisp_controller/config/base/base.py b/openwisp_controller/config/base/base.py index db0ae3bad..292144279 100644 --- a/openwisp_controller/config/base/base.py +++ b/openwisp_controller/config/base/base.py @@ -89,7 +89,7 @@ def get_config(self): return config c = deepcopy(config) is_config = not any([self.__template__, self.__vpn__]) - if 'hostname' not in c.get('general', {}) and is_config: + if all(('hostname' not in c.get('general', {}), is_config, self.name)): c.setdefault('general', {}) c['general']['hostname'] = self.name.replace(':', '-') return c diff --git a/openwisp_controller/config/base/config.py b/openwisp_controller/config/base/config.py index 331f594e8..797d38977 100644 --- a/openwisp_controller/config/base/config.py +++ b/openwisp_controller/config/base/config.py @@ -423,7 +423,7 @@ def _should_use_dsa(self): # Check if the device is using stock OpenWrt. openwrt_match = re.search( - '[oO][pP][eE][nN][wW][rR][tT]\s*([\d.]+)', self.device.os + r'[oO][pP][eE][nN][wW][rR][tT]\s*([\d.]+)', self.device.os ) if openwrt_match: if version.parse(openwrt_match.group(1)) >= version.parse('21'): diff --git a/openwisp_controller/config/tests/test_config.py b/openwisp_controller/config/tests/test_config.py index d6d8b86e3..4118b54b6 100644 --- a/openwisp_controller/config/tests/test_config.py +++ b/openwisp_controller/config/tests/test_config.py @@ -333,6 +333,11 @@ def test_auto_hostname(self): c.refresh_from_db() self.assertDictEqual(c.config, {'general': {}}) + with self.subTest('missing name shall not raise exception'): + c.device.name = None + del c.backend_instance + self.assertDictEqual(c.backend_instance.config, {'general': {}}) + def test_config_context(self): config = { 'general': { diff --git a/openwisp_controller/config/validators.py b/openwisp_controller/config/validators.py index 837361a70..d01bc25bd 100644 --- a/openwisp_controller/config/validators.py +++ b/openwisp_controller/config/validators.py @@ -2,7 +2,7 @@ from django.utils.translation import gettext_lazy as _ key_validator = RegexValidator( - _lazy_re_compile('^[^\s/\.]+$'), + _lazy_re_compile(r'^[^\s/\.]+$'), message=_('Key must not contain spaces, dots or slashes.'), code='invalid', ) @@ -16,9 +16,9 @@ # device name must either be a hostname or a valid mac address hostname_regex = ( - '^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}' - '[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9]' - '[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*$' + r'^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}' + r'[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9]' + r'[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*$' ) device_name_validator = RegexValidator( _lazy_re_compile('{0}|{1}'.format(hostname_regex, mac_address_regex)), diff --git a/openwisp_controller/connection/commands.py b/openwisp_controller/connection/commands.py index cfd2e7a41..6c48d6794 100644 --- a/openwisp_controller/connection/commands.py +++ b/openwisp_controller/connection/commands.py @@ -65,7 +65,7 @@ 'type': 'string', 'minLength': 6, 'maxLength': 30, - 'pattern': '[\S]', + 'pattern': r'[\S]', } }, },