From fc890e36afab0354b9918829bd323cfeddf8a0ce Mon Sep 17 00:00:00 2001 From: Arthur Date: Mon, 5 Feb 2024 15:15:01 -0800 Subject: [PATCH] 14438 script model --- .../migrations/0107_alter_script_options.py | 37 +++++++------------ netbox/extras/models/scripts.py | 7 ++++ netbox/templates/extras/script_list.html | 8 ++-- 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/netbox/extras/migrations/0107_alter_script_options.py b/netbox/extras/migrations/0107_alter_script_options.py index 47612d8f8..b49b9ba5e 100644 --- a/netbox/extras/migrations/0107_alter_script_options.py +++ b/netbox/extras/migrations/0107_alter_script_options.py @@ -5,14 +5,15 @@ import django.db.models.deletion def update_scripts(apps, schema_editor): - ScriptModule = apps.get_model('extras', 'ScriptModule') + from extras.models import ScriptModule + ScriptModuleNew = apps.get_model('extras', 'ScriptModule') Script = apps.get_model('extras', 'Script') for module in ScriptModule.objects.all(): - for script, cls in module.get_module_scripts: + for script in module.get_module_scripts.keys(): Script.objects.create( name=script, - script_module=module, + module=ScriptModuleNew.objects.get(file_root=module.file_root, file_path=module.file_path), ) @@ -23,26 +24,16 @@ class Migration(migrations.Migration): ] operations = [ - migrations.AddField( - model_name='script', - name='name', - field=models.CharField(default=None, max_length=79), - preserve_default=False, - ), - migrations.AddField( - model_name='script', - name='module', - field=models.ForeignKey(default=None, on_delete=django.db.models.deletion.PROTECT, related_name='scripts', to='extras.scriptmodule'), - preserve_default=False, - ), - migrations.AlterField( - model_name='script', - name='id', - field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False), - ), - migrations.AlterModelOptions( - name='script', - options={'ordering': ('name', 'pk')}, + migrations.CreateModel( + name='Script', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False)), + ('name', models.CharField(max_length=79)), + ('module', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='scripts', to='extras.scriptmodule')), + ], + options={ + 'ordering': ('name', 'pk'), + }, ), migrations.RunPython( code=update_scripts, diff --git a/netbox/extras/models/scripts.py b/netbox/extras/models/scripts.py index baaa9b973..fb0be49c6 100644 --- a/netbox/extras/models/scripts.py +++ b/netbox/extras/models/scripts.py @@ -32,9 +32,16 @@ class Script(EventRulesMixin, models.Model): related_name='scripts' ) + def __str__(self): + return self.name + class Meta: ordering = ('name', 'pk') + @cached_property + def python_class(self): + return self.module.get_module_scripts.get(self.name) + class ScriptModuleManager(models.Manager.from_queryset(RestrictedQuerySet)): diff --git a/netbox/templates/extras/script_list.html b/netbox/templates/extras/script_list.html index bb91f7522..d5c8711e5 100644 --- a/netbox/templates/extras/script_list.html +++ b/netbox/templates/extras/script_list.html @@ -53,15 +53,15 @@ {% with jobs=module.get_latest_jobs %} - {% for script_name, script_class in module.scripts.items %} + {% for script in module.scripts.all %} - {{ script_class.name }} + {{ script.python_class.name }} - {{ script_class.Meta.description|markdown|placeholder }} + {{ script.python_class.Meta.description|markdown|placeholder }} - {% with last_result=jobs|get_key:script_class.class_name %} + {% with last_result=jobs|get_key:script.python_class.class_name %} {% if last_result %} {{ last_result.created|annotated_date }}