From 19eb4c510c4737110e9944ddbc3d842ddb5ae70c Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Mon, 12 Aug 2019 13:16:18 -0400 Subject: [PATCH] Move script attributes under a Meta class --- docs/additional-features/custom-scripts.md | 20 ++++++++++++-------- netbox/extras/scripts.py | 15 ++++++++------- netbox/templates/extras/script.html | 2 +- netbox/templates/extras/script_list.html | 2 +- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/docs/additional-features/custom-scripts.md b/docs/additional-features/custom-scripts.md index ebec6b92159..5caeb66a385 100644 --- a/docs/additional-features/custom-scripts.md +++ b/docs/additional-features/custom-scripts.md @@ -39,20 +39,22 @@ Returning output from your script is optional. Any raw output generated by the s ## Script Attributes -### script_name +Script attributes are defined under a class named `Meta` within the script. These are optional, but encouraged. + +### `name` This is the human-friendly names of your script. If omitted, the class name will be used. -### script_description +### `description` -A human-friendly description of what your script does (optional). +A human-friendly description of what your script does. -### script_fields +### `fields` The order in which the variable fields should appear. This is optional, however on Python 3.5 and earlier the fields will appear in random order. (Declarative ordering is preserved on Python 3.6 and above.) For example: ``` -script_fields = ['var1', 'var2', 'var3'] +fields = ['var1', 'var2', 'var3'] ``` ## Logging @@ -124,9 +126,11 @@ from extras.scripts import * class NewBranchScript(Script): - script_name = "New Branch" - script_description = "Provision a new branch site" - script_fields = ['site_name', 'switch_count', 'switch_model'] + + class Meta: + name = "New Branch" + description = "Provision a new branch site" + fields = ['site_name', 'switch_count', 'switch_model'] site_name = StringVar( description="Name of the new site" diff --git a/netbox/extras/scripts.py b/netbox/extras/scripts.py index 39624067742..489ee76330c 100644 --- a/netbox/extras/scripts.py +++ b/netbox/extras/scripts.py @@ -118,6 +118,9 @@ class Script: """ Custom scripts inherit this object. """ + class Meta: + pass + def __init__(self): # Initiate the log @@ -128,17 +131,15 @@ class Script: self.source = inspect.getsource(self.__class__) def __str__(self): - if hasattr(self, 'script_name'): - return self.script_name - return self.__class__.__name__ + return getattr(self.Meta, 'name', self.__class__.__name__) def _get_vars(self): vars = OrderedDict() - # Infer order from script_fields (Python 3.5 and lower) - if hasattr(self, 'script_fields'): - for name in self.script_fields: - vars[name] = getattr(self, name) + # Infer order from Meta.fields (Python 3.5 and lower) + fields = getattr(self.Meta, 'fields') + for name in fields: + vars[name] = getattr(self, name) # Default to order of declaration on class for name, attr in self.__class__.__dict__.items(): diff --git a/netbox/templates/extras/script.html b/netbox/templates/extras/script.html index 240e54a5116..87f389c75a9 100644 --- a/netbox/templates/extras/script.html +++ b/netbox/templates/extras/script.html @@ -16,7 +16,7 @@

{{ script }}

-

{{ script.script_description }}

+

{{ script.Meta.description }}