From 87442f2a4fb671aafa6e82601c0a6291136f75d1 Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Fri, 24 Mar 2023 15:46:45 -0400 Subject: [PATCH] Fix module child ordering --- netbox/extras/models/models.py | 34 ++++++++++++++++++++++++---------- netbox/extras/views.py | 2 ++ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/netbox/extras/models/models.py b/netbox/extras/models/models.py index a24a473e2..9a27c5c7d 100644 --- a/netbox/extras/models/models.py +++ b/netbox/extras/models/models.py @@ -874,13 +874,20 @@ class ScriptModule(PythonModuleMixin, ManagedFile): @cached_property def scripts(self): - module = self.get_module() - scripts = {} - for name, cls in inspect.getmembers(module, is_script): + def _get_name(cls): # For child objects in submodules use the full import path w/o the root module as the name - child_name = cls.full_name.split(".", maxsplit=1)[1] - scripts[child_name] = cls + return cls.full_name.split(".", maxsplit=1)[1] + + module = self.get_module() + scripts = {} + ordered = getattr(module, 'script_order', []) + + for cls in ordered: + scripts[_get_name(cls)] = cls + for name, cls in inspect.getmembers(module, is_script): + if cls not in ordered: + scripts[_get_name(cls)] = cls return scripts @@ -928,13 +935,20 @@ class ReportModule(PythonModuleMixin, ManagedFile): @cached_property def reports(self): - module = self.get_module() - reports = {} - for name, cls in inspect.getmembers(module, is_report): + def _get_name(cls): # For child objects in submodules use the full import path w/o the root module as the name - child_name = cls().full_name.split(".", maxsplit=1)[1] - reports[child_name] = cls + return cls.full_name.split(".", maxsplit=1)[1] + + module = self.get_module() + reports = {} + ordered = getattr(module, 'report_order', []) + + for cls in ordered: + reports[_get_name(cls)] = cls + for name, cls in inspect.getmembers(module, is_report): + if cls not in ordered: + reports[_get_name(cls)] = cls return reports diff --git a/netbox/extras/views.py b/netbox/extras/views.py index d58d3716f..23b46530b 100644 --- a/netbox/extras/views.py +++ b/netbox/extras/views.py @@ -805,6 +805,7 @@ class ReportModuleCreateView(generic.ObjectEditView): @register_model_view(ReportModule, 'delete') class ReportModuleDeleteView(generic.ObjectDeleteView): queryset = ReportModule.objects.all() + default_return_url = 'extras:report_list' class ReportListView(ContentTypePermissionRequiredMixin, View): @@ -943,6 +944,7 @@ class ScriptModuleCreateView(generic.ObjectEditView): @register_model_view(ScriptModule, 'delete') class ScriptModuleDeleteView(generic.ObjectDeleteView): queryset = ScriptModule.objects.all() + default_return_url = 'extras:script_list' class ScriptListView(ContentTypePermissionRequiredMixin, View):