@@ -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
486485class GetVpnView (SingleObjectMixin , View ):
0 commit comments