Skip to content

Commit d806e26

Browse files
committed
[refactor] Consistent functions for create/update
Signed-off-by: DragnEmperor <[email protected]>
1 parent c892a06 commit d806e26

File tree

3 files changed

+26
-25
lines changed

3 files changed

+26
-25
lines changed

openwisp_controller/config/whois/service.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,27 @@ def update_whois_info(self):
279279
if whois_obj and self.is_older(whois_obj.modified):
280280
fetch_whois_details.delay(device_pk=self.device.pk, initial_ip_address=None)
281281

282+
def _create_or_update_whois(self, whois_details, whois_instance=None):
283+
"""
284+
Used to update an existing WHOIS instance; else, creates a new one.
285+
Returns the updated or created WHOIS instance along with update fields.
286+
"""
287+
WHOISInfo = load_model("config", "WHOISInfo")
288+
289+
update_fields = []
290+
if whois_instance:
291+
for attr, value in whois_details.items():
292+
if getattr(whois_instance, attr) != value:
293+
update_fields.append(attr)
294+
setattr(whois_instance, attr, value)
295+
if update_fields:
296+
whois_instance.save(update_fields=update_fields)
297+
else:
298+
whois_instance = WHOISInfo(**whois_details)
299+
whois_instance.full_clean()
300+
whois_instance.save(force_insert=True)
301+
return whois_instance, update_fields
302+
282303
def _create_or_update_estimated_location(
283304
self, location_defaults, attached_devices_exists
284305
):

openwisp_controller/config/whois/tasks.py

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,6 @@ def on_failure(self, exc, task_id, args, kwargs, einfo):
3131
return super().on_failure(exc, task_id, args, kwargs, einfo)
3232

3333

34-
def _manage_whois_record(whois_details, whois_instance=None):
35-
"""
36-
Used to update an existing WHOIS instance; else, creates a new one.
37-
Returns the updated or created WHOIS instance along with update fields.
38-
"""
39-
WHOISInfo = load_model("config", "WHOISInfo")
40-
41-
update_fields = []
42-
if whois_instance:
43-
for attr, value in whois_details.items():
44-
if getattr(whois_instance, attr) != value:
45-
update_fields.append(attr)
46-
setattr(whois_instance, attr, value)
47-
if update_fields:
48-
whois_instance.save(update_fields=update_fields)
49-
else:
50-
whois_instance = WHOISInfo(**whois_details)
51-
whois_instance.full_clean()
52-
whois_instance.save(force_insert=True)
53-
return whois_instance, update_fields
54-
55-
5634
# device_pk is used when task fails to report for which device failure occurred
5735
@shared_task(
5836
bind=True,
@@ -78,7 +56,9 @@ def fetch_whois_details(self, device_pk, initial_ip_address):
7856
return
7957

8058
fetched_details = WHOISService.process_whois_details(new_ip_address)
81-
whois_obj, update_fields = _manage_whois_record(fetched_details, whois_obj)
59+
whois_obj, update_fields = WHOISService._create_or_update_whois(
60+
fetched_details, whois_obj
61+
)
8262
logger.info(f"Successfully fetched WHOIS details for {new_ip_address}.")
8363

8464
if device._get_organization__config_settings().estimated_location_enabled:

openwisp_controller/geo/estimated_location/tasks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ def _handle_attach_existing_location(
3030
)
3131
if existing_device_location:
3232
existing_location = existing_device_location.location
33-
# We need to remove existing estimated location of the device
34-
# if it is not shared
3533
device_location.location = existing_location
3634
device_location.full_clean()
3735
device_location.save()
3836
logger.info(
3937
f"Estimated location saved successfully for {device.pk}"
4038
f" for IP: {ip_address}"
4139
)
40+
# We need to remove existing estimated location of the device
41+
# if it is not shared
4242
if attached_devices_exists is False:
4343
current_location.delete()
4444
send_whois_task_notification(

0 commit comments

Comments
 (0)