Skip to content

Commit c24fec3

Browse files
committed
[change] Migrate signal receivers to WhoIs model
Migrated all receivers related to WhoIs to the model class. Includes minor refactoring as well. Signed-off-by: DragnEmperor <[email protected]>
1 parent 97eceeb commit c24fec3

File tree

4 files changed

+44
-37
lines changed

4 files changed

+44
-37
lines changed

openwisp_controller/config/apps.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def __setmodels__(self):
6464
self.cert_model = load_model("django_x509", "Cert")
6565
self.org_model = load_model("openwisp_users", "Organization")
6666
self.org_config_model = load_model("config", "OrganizationConfigSettings")
67+
self.who_is_model = load_model("config", "WhoIsInfo")
6768

6869
def connect_signals(self):
6970
"""
@@ -75,10 +76,6 @@ def connect_signals(self):
7576
* cache invalidation
7677
"""
7778
from . import handlers # noqa
78-
from .who_is.handlers import (
79-
device_who_is_info_delete_handler,
80-
invalidate_org_settings_cache,
81-
)
8279

8380
m2m_changed.connect(
8481
self.config_model.clean_templates,
@@ -162,17 +159,17 @@ def connect_signals(self):
162159
dispatch_uid="organization_allowed_devices_post_save_handler",
163160
)
164161
post_delete.connect(
165-
device_who_is_info_delete_handler,
162+
self.who_is_model.device_who_is_info_delete_handler,
166163
sender=self.device_model,
167164
dispatch_uid="device.delete_who_is_info",
168165
)
169166
post_save.connect(
170-
invalidate_org_settings_cache,
167+
self.who_is_model.invalidate_org_settings_cache,
171168
self.org_config_model,
172169
dispatch_uid="invalidate_org_config_cache_on_org_config_save",
173170
)
174171
post_delete.connect(
175-
invalidate_org_settings_cache,
172+
self.who_is_model.invalidate_org_settings_cache,
176173
self.org_config_model,
177174
dispatch_uid="invalidate_org_config_cache_on_org_config_delete",
178175
)

openwisp_controller/config/base/who_is.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from ipaddress import ip_address
22

3+
from django.core.cache import cache
34
from django.core.exceptions import ValidationError
4-
from django.db import models
5+
from django.db import models, transaction
56
from django.utils.translation import gettext_lazy as _
67
from jsonfield import JSONField
78

@@ -56,3 +57,30 @@ def clean(self):
5657
_("WhoIs information cannot be created for private IP addresses.")
5758
)
5859
return super().clean()
60+
61+
@staticmethod
62+
def device_who_is_info_delete_handler(instance, **kwargs):
63+
"""
64+
Delete WhoIs information for a device when the last IP address is removed or
65+
when device is deleted.
66+
"""
67+
# importing here to avoid AppRegistryNotReady error
68+
from openwisp_controller.config.who_is.service import WhoIsService
69+
70+
transaction.on_commit(
71+
lambda: WhoIsService.delete_who_is_record.delay(instance.last_ip)
72+
)
73+
74+
# this method is kept here instead of in OrganizationConfigSettings because
75+
# currently the caching is used only for WhoIs feature
76+
@staticmethod
77+
def invalidate_org_settings_cache(instance, **kwargs):
78+
"""
79+
Invalidate the cache for Organization settings on update/delete of
80+
Organization settings instance.
81+
"""
82+
# importing here to avoid AppRegistryNotReady error
83+
from openwisp_controller.config.who_is.service import WhoIsService
84+
85+
org_id = instance.organization_id
86+
cache.delete(WhoIsService.get_cache_key(org_id))

openwisp_controller/config/who_is/handlers.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

openwisp_controller/config/who_is/service.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from celery import shared_task
66
from django.core.cache import cache
77
from django.db import transaction
8-
from django.db.models import Subquery
98
from django.utils.translation import gettext as _
109
from geoip2 import errors
1110
from geoip2 import webservice as geoip2_webservice
@@ -59,11 +58,16 @@ class WhoIsService:
5958
A handler class for managing the WhoIs functionality.
6059
"""
6160

62-
ORG_SETTINGS_CACHE_KEY = "organization_config_{org_pk}"
63-
6461
def __init__(self, device):
6562
self.device = device
6663

64+
@staticmethod
65+
def get_cache_key(org_id):
66+
"""
67+
Used to get cache key for caching org settings of a device.
68+
"""
69+
return f"organization_config_{org_id}"
70+
6771
@staticmethod
6872
def _get_geoip2_client():
6973
"""
@@ -126,22 +130,18 @@ def is_who_is_enabled(self):
126130
is set to the same as the checksum cache timeout for consistency
127131
with DeviceChecksumView.
128132
"""
129-
org_pk = self.device.organization.pk
130-
org_settings = cache.get(self.ORG_SETTINGS_CACHE_KEY.format(org_pk=org_pk))
133+
org_id = self.device.organization.pk
134+
org_settings = cache.get(self.get_cache_key(org_id=org_id))
131135
if org_settings is None:
132136
try:
133137
org_settings = OrganizationConfigSettings.objects.get(
134-
organization=Subquery(
135-
Device.objects.filter(pk=self.device.pk).values(
136-
"organization_id"
137-
)[:1]
138-
)
138+
organization=org_id
139139
)
140140
except OrganizationConfigSettings.DoesNotExist:
141141
# If organization settings do not exist, fall back to global setting
142142
return app_settings.WHO_IS_ENABLED
143143
cache.set(
144-
self.ORG_SETTINGS_CACHE_KEY.format(org_pk=org_pk),
144+
self.get_cache_key(org_id=org_id),
145145
org_settings,
146146
timeout=Config._CHECKSUM_CACHE_TIMEOUT,
147147
)

0 commit comments

Comments
 (0)