@@ -158,20 +158,20 @@ def _need_who_is_lookup(self, initial_ip, new_ip):
158
158
This is used to determine if the WhoIs lookup should be triggered
159
159
when the device is saved.
160
160
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)
167
165
"""
168
166
169
167
# 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
175
175
176
176
def get_device_who_is_info (self ):
177
177
"""
@@ -209,17 +209,16 @@ def fetch_who_is_details(self, device_pk, initial_ip_address, new_ip_address):
209
209
Fetches the WhoIs details of the given IP address
210
210
and creates/updates the WhoIs record.
211
211
"""
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.
215
214
if self ._get_who_is_info_from_db (new_ip_address ):
216
215
return
217
216
218
217
WhoIsInfo = load_model ("config" , "WhoIsInfo" )
219
218
220
- try :
221
- ip_client = WhoIsService ._get_geoip2_client ()
219
+ ip_client = WhoIsService ._get_geoip2_client ()
222
220
221
+ try :
223
222
data = ip_client .city (ip_address = new_ip_address )
224
223
225
224
# Catching all possible exceptions raised by the geoip2 client
0 commit comments