Skip to content

Commit 18f67d6

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 18f67d6

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

openwisp_controller/config/who_is/service.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ def is_valid_public_ip_address(ip):
116116
except ValueError:
117117
return False
118118

119+
@staticmethod
120+
def _get_who_is_info_from_db(ip_address):
121+
"""
122+
For getting existing WhoIsInfo for given IP from db if present.
123+
"""
124+
WhoIsInfo = load_model("config", "WhoIsInfo")
125+
126+
return WhoIsInfo.objects.filter(ip_address=ip_address)
127+
119128
@property
120129
def is_who_is_enabled(self):
121130
"""
@@ -145,33 +154,25 @@ def is_who_is_enabled(self):
145154
)
146155
return getattr(org_settings, "who_is_enabled", app_settings.WHO_IS_ENABLED)
147156

148-
def _get_who_is_info_from_db(self, ip_address):
149-
"""
150-
For getting existing WhoIsInfo for given IP from db if present.
151-
"""
152-
WhoIsInfo = load_model("config", "WhoIsInfo")
153-
154-
return WhoIsInfo.objects.filter(ip_address=ip_address).first()
155-
156-
def _need_who_is_lookup(self, initial_ip, new_ip):
157+
def _need_who_is_lookup(self, new_ip):
157158
"""
158159
This is used to determine if the WhoIs lookup should be triggered
159160
when the device is saved.
160161
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)
162+
The lookup is not triggered if:
163+
- The new IP address is None or it is a private IP address.
164+
- The WhoIs information of new ip is already present.
165+
- WhoIs is disabled in the organization settings. (query from db)
167166
"""
168167

169168
# 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-
)
169+
if not self.is_valid_public_ip_address(new_ip):
170+
return False
171+
172+
if self._get_who_is_info_from_db(new_ip).exists():
173+
return False
174+
175+
return self.is_who_is_enabled
175176

176177
def get_device_who_is_info(self):
177178
"""
@@ -182,14 +183,14 @@ def get_device_who_is_info(self):
182183
if not (self.is_valid_public_ip_address(ip_address) and self.is_who_is_enabled):
183184
return None
184185

185-
return self._get_who_is_info_from_db(ip_address=ip_address)
186+
return self._get_who_is_info_from_db(ip_address=ip_address).first()
186187

187188
def trigger_who_is_lookup(self):
188189
"""
189190
Trigger WhoIs lookup based on the conditions of `_need_who_is_lookup`.
190191
Task is triggered on commit to ensure redundant data is not created.
191192
"""
192-
if self._need_who_is_lookup(self.device._initial_last_ip, self.device.last_ip):
193+
if self._need_who_is_lookup(self.device.last_ip):
193194
transaction.on_commit(
194195
lambda: self.fetch_who_is_details.delay(
195196
device_pk=self.device.pk,
@@ -209,17 +210,16 @@ def fetch_who_is_details(self, device_pk, initial_ip_address, new_ip_address):
209210
Fetches the WhoIs details of the given IP address
210211
and creates/updates the WhoIs record.
211212
"""
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.
215-
if self._get_who_is_info_from_db(new_ip_address):
213+
# The task can be triggered for same ip address multiple times
214+
# so we need to return early if WhoIs is already created.
215+
if WhoIsService._get_who_is_info_from_db(new_ip_address).exists():
216216
return
217217

218218
WhoIsInfo = load_model("config", "WhoIsInfo")
219219

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

222+
try:
223223
data = ip_client.city(ip_address=new_ip_address)
224224

225225
# Catching all possible exceptions raised by the geoip2 client

0 commit comments

Comments
 (0)