|
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") |
@@ -560,12 +562,21 @@ class DeviceAdmin(MultitenantAdminMixin, BaseConfigAdmin, UUIDAdmin): |
560 | 562 | fields.insert(0, "hardware_id") |
561 | 563 | list_select_related = ("config", "organization") |
562 | 564 |
|
563 | | - class Media(BaseConfigAdmin.Media): |
| 565 | + # overriding media property to allow testing in isolation |
| 566 | + # as class Media is evaluated at import time making the |
| 567 | + # settings not overridable in tests |
| 568 | + @property |
| 569 | + def media(self): |
564 | 570 | js = BaseConfigAdmin.Media.js + [ |
565 | 571 | f"{prefix}js/tabs.js", |
566 | 572 | f"{prefix}js/management_ip.js", |
567 | 573 | f"{prefix}js/relevant_templates.js", |
568 | 574 | ] |
| 575 | + css = BaseConfigAdmin.Media.css["all"] |
| 576 | + if app_settings.WHOIS_CONFIGURED: |
| 577 | + js.append(f"{whois_prefix}js/whois.js") |
| 578 | + css += (f"{whois_prefix}css/whois.css",) |
| 579 | + return super().media + forms.Media(js=js, css={"all": css}) |
569 | 580 |
|
570 | 581 | def has_change_permission(self, request, obj=None): |
571 | 582 | perm = super().has_change_permission(request) |
@@ -919,6 +930,10 @@ def get_extra_context(self, pk=None): |
919 | 930 | ), |
920 | 931 | } |
921 | 932 | ) |
| 933 | + # passing the whois details to the context to avoid |
| 934 | + # the need to make an additional request in the js |
| 935 | + if data := get_whois_info(pk): |
| 936 | + ctx["device_whois_details"] = json.dumps(data) |
922 | 937 | return ctx |
923 | 938 |
|
924 | 939 | def add_view(self, request, form_url="", extra_context=None): |
|
0 commit comments