From bfccd6820e64b415286262f5d7c5b3edde70a2db Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Fri, 24 Mar 2023 09:22:25 -0400 Subject: [PATCH] Add deletion links for modules --- netbox/extras/models/models.py | 10 ++++++++++ netbox/extras/urls.py | 8 ++++---- netbox/extras/utils.py | 12 ++++++------ netbox/templates/extras/report_list.html | 11 +++++++++-- netbox/templates/extras/script_list.html | 11 +++++++++-- 5 files changed, 38 insertions(+), 14 deletions(-) diff --git a/netbox/extras/models/models.py b/netbox/extras/models/models.py index 7420c9ab9..02d71538e 100644 --- a/netbox/extras/models/models.py +++ b/netbox/extras/models/models.py @@ -855,6 +855,13 @@ class ScriptModule(JobResultsMixin, WebhooksMixin, PythonModuleMixin, ManagedFil self.file_root = SCRIPTS_ROOT_NAME return super().save(*args, **kwargs) + def get_absolute_url(self): + return reverse('extras:script_list') + + @property + def name(self): + return self.file_path + # # Reports @@ -886,3 +893,6 @@ class ReportModule(JobResultsMixin, WebhooksMixin, PythonModuleMixin, ManagedFil def save(self, *args, **kwargs): self.file_root = REPORTS_ROOT_NAME return super().save(*args, **kwargs) + + def get_absolute_url(self): + return reverse('extras:report_list') diff --git a/netbox/extras/urls.py b/netbox/extras/urls.py index c62b256a5..c838ee759 100644 --- a/netbox/extras/urls.py +++ b/netbox/extras/urls.py @@ -94,17 +94,17 @@ urlpatterns = [ # Reports path('reports/', views.ReportListView.as_view(), name='report_list'), - path('reports/results//', views.ReportResultView.as_view(), name='report_result'), - re_path(r'^reports/(?P.([^.]+)).(?P.(.+))/', views.ReportView.as_view(), name='report'), path('reports/add/', views.ScriptModuleCreateView.as_view(), name='reportmodule_add'), + path('reports/results//', views.ReportResultView.as_view(), name='report_result'), path('reports//', include(get_model_urls('extras', 'reportmodule'))), + path('reports/./', views.ReportView.as_view(), name='report'), # Scripts path('scripts/', views.ScriptListView.as_view(), name='script_list'), - path('scripts/results//', views.ScriptResultView.as_view(), name='script_result'), - re_path(r'^scripts/(?P.([^.]+)).(?P.(.+))/', views.ScriptView.as_view(), name='script'), path('scripts/add/', views.ScriptModuleCreateView.as_view(), name='scriptmodule_add'), + path('scripts/results//', views.ScriptResultView.as_view(), name='script_result'), path('scripts//', include(get_model_urls('extras', 'scriptmodule'))), + path('scripts/./', views.ScriptView.as_view(), name='script'), # Job results path('job-results/', views.JobResultListView.as_view(), name='jobresult_list'), diff --git a/netbox/extras/utils.py b/netbox/extras/utils.py index 3a45d20db..073f1d13f 100644 --- a/netbox/extras/utils.py +++ b/netbox/extras/utils.py @@ -79,15 +79,15 @@ def get_modules(queryset, litmus_func, ordering_attr): Returns a list of tuples: [ - (module_name, (child, child, ...)), - (module_name, (child, child, ...)), + (module, (child, child, ...)), + (module, (child, child, ...)), ... ] """ results = {} - modules = [mf.get_module_info() for mf in queryset] - modules_bases = set([name.split(".")[0] for _, name, _ in modules]) + modules = [(mf, *mf.get_module_info()) for mf in queryset] + modules_bases = set([name.split(".")[0] for _, _, name, _ in modules]) # Deleting from sys.modules needs to done behind a lock to prevent race conditions where a module is # removed from sys.modules while another thread is importing @@ -100,7 +100,7 @@ def get_modules(queryset, litmus_func, ordering_attr): if module_base in ('reports', 'scripts', *modules_bases): del sys.modules[module_name] - for importer, module_name, _ in modules: + for mf, importer, module_name, _ in modules: module = importer.find_module(module_name).load_module(module_name) child_order = getattr(module, ordering_attr, ()) ordered_children = [cls() for cls in child_order if litmus_func(cls)] @@ -114,6 +114,6 @@ def get_modules(queryset, litmus_func, ordering_attr): children[child_name] = cls if children: - results[module_name] = children + results[mf] = children return results diff --git a/netbox/templates/extras/report_list.html b/netbox/templates/extras/report_list.html index 64e99e660..0798455fe 100644 --- a/netbox/templates/extras/report_list.html +++ b/netbox/templates/extras/report_list.html @@ -28,8 +28,15 @@ {% for module, module_reports in reports %}
- - {{ module|bettertitle }} + {% if perms.extras.delete_reportmodule %} + + {% endif %} + + {{ module.name|bettertitle }}
diff --git a/netbox/templates/extras/script_list.html b/netbox/templates/extras/script_list.html index bfa8f0d0f..4df657fec 100644 --- a/netbox/templates/extras/script_list.html +++ b/netbox/templates/extras/script_list.html @@ -27,8 +27,15 @@ {% for module, module_scripts in scripts.items %}
- - {{ module|bettertitle }} + {% if perms.extras.delete_scriptmodule %} + + {% endif %} + + {{ module.name|bettertitle }}