Skip to content

Commit 4c5fac4

Browse files
committed
[req-changes] Improved code
1 parent be67a2a commit 4c5fac4

File tree

3 files changed

+17
-18
lines changed

3 files changed

+17
-18
lines changed

openwisp_controller/config/controller/views.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -402,16 +402,13 @@ def post(self, request, *args, **kwargs):
402402
new = False
403403
try:
404404
device = self.model.objects.get(key=key)
405-
# update hw info
405+
# update device info
406406
for attr in self.UPDATABLE_FIELDS:
407407
if attr in request.POST:
408408
if attr == "name":
409-
if self._should_update_hostname(request, device):
410-
device.skip_push_update_on_save()
411-
else:
412-
# Don't update Device.name
413-
continue
414-
setattr(device, attr, request.POST.get(attr))
409+
self._update_device_name(request, device)
410+
else:
411+
setattr(device, attr, request.POST.get(attr))
415412
config = device.config
416413
# if get queryset fails, instantiate a new Device and Config
417414
except self.model.DoesNotExist:
@@ -463,14 +460,14 @@ def post(self, request, *args, **kwargs):
463460
s.format(**attributes), content_type="text/plain", status=201
464461
)
465462

466-
def _should_update_hostname(self, request, device):
463+
def _update_device_name(self, request, device):
467464
"""
468-
Determines whether the hostname should be updated during re-registration.
465+
Updates the device name during re-registration if the name in the
466+
payload is not equal to the device's MAC address.
469467
470-
Returns False if the hostname equals the MAC address (agents send MAC address
471-
as hostname when hostname is OpenWrt or if default_hostname is set to *).
472-
473-
This prevents overwriting user configured hostnames with default/generic values.
468+
Agents send the MAC address as hostname when the hostname is "OpenWrt"
469+
or if default_hostname is set to *. This method prevents overwriting
470+
user-configured hostnames with default/generic MAC address values.
474471
"""
475472
name = request.POST.get("name")
476473
mac_address = device.mac_address
@@ -480,7 +477,9 @@ def _should_update_hostname(self, request, device):
480477
else None
481478
)
482479
normalized_name = name.replace(":", "").replace("-", "").lower()
483-
return normalized_name != normalized_mac
480+
if normalized_name != normalized_mac:
481+
device.name = name
482+
device.skip_push_update_on_save()
484483

485484

486485
class GetVpnView(SingleObjectMixin, View):

openwisp_controller/config/tests/test_controller.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
Organization = load_model("openwisp_users", "Organization")
3939

4040

41-
class TestControllerMixin:
41+
class TestRegistrationMixin:
4242
register_url = reverse_lazy("controller:device_register")
4343

4444
def _create_org(self, shared_secret=TEST_ORG_SHARED_SECRET, **kwargs):
@@ -63,7 +63,7 @@ def _get_reregistration_payload(self, device, **kwargs):
6363

6464

6565
class TestController(
66-
TestControllerMixin, CreateConfigTemplateMixin, TestVpnX509Mixin, TestCase
66+
TestRegistrationMixin, CreateConfigTemplateMixin, TestVpnX509Mixin, TestCase
6767
):
6868
"""
6969
tests for config.controller

openwisp_controller/connection/tests/test_tasks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from django.test import TestCase, TransactionTestCase
88
from swapper import load_model
99

10-
from ...config.tests.test_controller import TestControllerMixin
10+
from ...config.tests.test_controller import TestRegistrationMixin
1111
from .. import tasks
1212
from .utils import CreateConnectionsMixin
1313

@@ -90,7 +90,7 @@ def test_launch_command_exception(self, *args):
9090

9191

9292
class TestTransactionTasks(
93-
TestControllerMixin, CreateConnectionsMixin, TransactionTestCase
93+
TestRegistrationMixin, CreateConnectionsMixin, TransactionTestCase
9494
):
9595
@mock.patch.object(tasks.update_config, "delay")
9696
def test_update_config_hostname_changed_on_reregister(self, mocked_update_config):

0 commit comments

Comments
 (0)