Просмотр исходного кода

Only use module.name for human-facing display

Jeremy Stretch 6 лет назад
Родитель
Сommit
0f65cf23a5
3 измененных файлов с 10 добавлено и 5 удалено
  1. 8 3
      netbox/extras/scripts.py
  2. 1 1
      netbox/extras/views.py
  3. 1 1
      netbox/templates/extras/script_list.html

+ 8 - 3
netbox/extras/scripts.py

@@ -220,6 +220,10 @@ class BaseScript:
     def __str__(self):
         return getattr(self.Meta, 'name', self.__class__.__name__)
 
+    @classmethod
+    def module(cls):
+        return cls.__module__
+
     @classmethod
     def _get_vars(cls):
         vars = OrderedDict()
@@ -361,9 +365,10 @@ def run_script(script, data, files, commit=True):
     return output, execution_time
 
 
-def get_scripts():
+def get_scripts(use_names=False):
     """
-    Return a dict of dicts mapping all scripts to their modules.
+    Return a dict of dicts mapping all scripts to their modules. Set use_names to True to use each module's human-
+    defined name in place of the actual module name.
     """
     scripts = OrderedDict()
 
@@ -371,7 +376,7 @@ def get_scripts():
     # defined.
     for importer, module_name, _ in pkgutil.iter_modules([settings.SCRIPTS_ROOT]):
         module = importer.find_module(module_name).load_module(module_name)
-        if hasattr(module, 'name'):
+        if use_names and hasattr(module, 'name'):
             module_name = module.name
         module_scripts = OrderedDict()
         for name, cls in inspect.getmembers(module, is_script):

+ 1 - 1
netbox/extras/views.py

@@ -375,7 +375,7 @@ class ScriptListView(PermissionRequiredMixin, View):
     def get(self, request):
 
         return render(request, 'extras/script_list.html', {
-            'scripts': get_scripts(),
+            'scripts': get_scripts(use_names=True),
         })
 
 

+ 1 - 1
netbox/templates/extras/script_list.html

@@ -19,7 +19,7 @@
                             {% for class_name, script in module_scripts.items %}
                                 <tr>
                                     <td>
-                                        <a href="{% url 'extras:script' module=module name=class_name %}" name="script.{{ class_name }}"><strong>{{ script }}</strong></a>
+                                        <a href="{% url 'extras:script' module=script.module name=class_name %}" name="script.{{ class_name }}"><strong>{{ script }}</strong></a>
                                     </td>
                                     <td>{{ script.Meta.description }}</td>
                                 </tr>