|
31 | 31 | from swapper import load_model |
32 | 32 |
|
33 | 33 | from openwisp_controller.config.views import get_default_values, get_relevant_templates |
| 34 | +from openwisp_controller.config.whois.utils import get_whois_info |
34 | 35 | from openwisp_users.admin import OrganizationAdmin |
35 | 36 | from openwisp_users.multitenancy import MultitenantOrgFilter |
36 | 37 | from openwisp_utils.admin import ( |
|
49 | 50 |
|
50 | 51 | logger = logging.getLogger(__name__) |
51 | 52 | prefix = "config/" |
| 53 | +whois_prefix = "whois/" |
52 | 54 | Config = load_model("config", "Config") |
53 | 55 | Device = load_model("config", "Device") |
54 | 56 | DeviceGroup = load_model("config", "DeviceGroup") |
@@ -569,12 +571,21 @@ class DeviceAdmin(MultitenantAdminMixin, BaseConfigAdmin, UUIDAdmin): |
569 | 571 | fields.insert(0, "hardware_id") |
570 | 572 | list_select_related = ("config", "organization") |
571 | 573 |
|
572 | | - class Media(BaseConfigAdmin.Media): |
| 574 | + # overriding media property to allow testing in isolation |
| 575 | + # as class Media is evaluated at import time making the |
| 576 | + # settings not overridable in tests |
| 577 | + @property |
| 578 | + def media(self): |
573 | 579 | js = BaseConfigAdmin.Media.js + [ |
574 | 580 | f"{prefix}js/tabs.js", |
575 | 581 | f"{prefix}js/management_ip.js", |
576 | 582 | f"{prefix}js/relevant_templates.js", |
577 | 583 | ] |
| 584 | + css = BaseConfigAdmin.Media.css["all"] |
| 585 | + if app_settings.WHOIS_CONFIGURED: |
| 586 | + js.append(f"{whois_prefix}js/whois.js") |
| 587 | + css += (f"{whois_prefix}css/whois.css",) |
| 588 | + return super().media + forms.Media(js=js, css={"all": css}) |
578 | 589 |
|
579 | 590 | def has_change_permission(self, request, obj=None): |
580 | 591 | perm = super().has_change_permission(request) |
@@ -928,6 +939,10 @@ def get_extra_context(self, pk=None): |
928 | 939 | ), |
929 | 940 | } |
930 | 941 | ) |
| 942 | + # passing the whois details to the context to avoid |
| 943 | + # the need to make an additional request in the js |
| 944 | + if data := get_whois_info(pk): |
| 945 | + ctx["device_whois_details"] = json.dumps(data) |
931 | 946 | return ctx |
932 | 947 |
|
933 | 948 | def add_view(self, request, form_url="", extra_context=None): |
|
0 commit comments