Skip to content

Commit 07c94fb

Browse files
committed
[change] Simplified checks
The checks in `_need_who_is_lookup` are split for better readability Signed-off-by: DragnEmperor <[email protected]>
1 parent e09b6e1 commit 07c94fb

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

openwisp_controller/config/who_is/service.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -158,20 +158,20 @@ def _need_who_is_lookup(self, initial_ip, new_ip):
158158
This is used to determine if the WhoIs lookup should be triggered
159159
when the device is saved.
160160
161-
The lookup is triggered if:
162-
- The new IP address is not None.
163-
- The WhoIs information is not already present or the initial IP is
164-
different from the new IP.
165-
- The new IP address is a global (public) IP address.
166-
- WhoIs is enabled in the organization settings. (query from db)
161+
The lookup is not triggered if:
162+
- The new IP address is None or it is a private IP address.
163+
- The WhoIs information of new ip is already present.
164+
- WhoIs is disabled in the organization settings. (query from db)
167165
"""
168166

169167
# Check cheap conditions first before hitting the database
170-
return (
171-
self.is_valid_public_ip_address(new_ip)
172-
and (initial_ip != new_ip or not self._get_who_is_info_from_db(new_ip))
173-
and self.is_who_is_enabled
174-
)
168+
if not self.is_valid_public_ip_address(new_ip):
169+
return False
170+
171+
if self._get_who_is_info_from_db(new_ip):
172+
return False
173+
174+
return self.is_who_is_enabled
175175

176176
def get_device_who_is_info(self):
177177
"""
@@ -209,17 +209,16 @@ def fetch_who_is_details(self, device_pk, initial_ip_address, new_ip_address):
209209
Fetches the WhoIs details of the given IP address
210210
and creates/updates the WhoIs record.
211211
"""
212-
# The task is triggered if last_ip is updated irrespective
213-
# if there is WhoIsInfo for the new IP address so we return
214-
# from the task if that is the case.
212+
# The task can be triggered for same ip address multiple times
213+
# so we need to return early if WhoIs is already created.
215214
if self._get_who_is_info_from_db(new_ip_address):
216215
return
217216

218217
WhoIsInfo = load_model("config", "WhoIsInfo")
219218

220-
try:
221-
ip_client = WhoIsService._get_geoip2_client()
219+
ip_client = WhoIsService._get_geoip2_client()
222220

221+
try:
223222
data = ip_client.city(ip_address=new_ip_address)
224223

225224
# Catching all possible exceptions raised by the geoip2 client

0 commit comments

Comments
 (0)