@@ -116,6 +116,15 @@ def is_valid_public_ip_address(ip):
116
116
except ValueError :
117
117
return False
118
118
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
+
119
128
@property
120
129
def is_who_is_enabled (self ):
121
130
"""
@@ -145,33 +154,25 @@ def is_who_is_enabled(self):
145
154
)
146
155
return getattr (org_settings , "who_is_enabled" , app_settings .WHO_IS_ENABLED )
147
156
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 ):
157
158
"""
158
159
This is used to determine if the WhoIs lookup should be triggered
159
160
when the device is saved.
160
161
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)
167
166
"""
168
167
169
168
# 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
175
176
176
177
def get_device_who_is_info (self ):
177
178
"""
@@ -182,14 +183,14 @@ def get_device_who_is_info(self):
182
183
if not (self .is_valid_public_ip_address (ip_address ) and self .is_who_is_enabled ):
183
184
return None
184
185
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 ()
186
187
187
188
def trigger_who_is_lookup (self ):
188
189
"""
189
190
Trigger WhoIs lookup based on the conditions of `_need_who_is_lookup`.
190
191
Task is triggered on commit to ensure redundant data is not created.
191
192
"""
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 ):
193
194
transaction .on_commit (
194
195
lambda : self .fetch_who_is_details .delay (
195
196
device_pk = self .device .pk ,
@@ -209,17 +210,16 @@ def fetch_who_is_details(self, device_pk, initial_ip_address, new_ip_address):
209
210
Fetches the WhoIs details of the given IP address
210
211
and creates/updates the WhoIs record.
211
212
"""
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 ():
216
216
return
217
217
218
218
WhoIsInfo = load_model ("config" , "WhoIsInfo" )
219
219
220
- try :
221
- ip_client = WhoIsService ._get_geoip2_client ()
220
+ ip_client = WhoIsService ._get_geoip2_client ()
222
221
222
+ try :
223
223
data = ip_client .city (ip_address = new_ip_address )
224
224
225
225
# Catching all possible exceptions raised by the geoip2 client
0 commit comments