From 4cb3b63f79ea9814bc4627bcad4f8be683591cca Mon Sep 17 00:00:00 2001 From: Arthur Date: Mon, 5 Feb 2024 14:16:24 -0800 Subject: [PATCH] 14438 script model --- .../migrations/0107_alter_script_options.py | 18 +++++++++++++++++- netbox/extras/models/scripts.py | 6 ++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/netbox/extras/migrations/0107_alter_script_options.py b/netbox/extras/migrations/0107_alter_script_options.py index 6829761f3..47612d8f8 100644 --- a/netbox/extras/migrations/0107_alter_script_options.py +++ b/netbox/extras/migrations/0107_alter_script_options.py @@ -4,6 +4,18 @@ from django.db import migrations, models import django.db.models.deletion +def update_scripts(apps, schema_editor): + ScriptModule = 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: + Script.objects.create( + name=script, + script_module=module, + ) + + class Migration(migrations.Migration): dependencies = [ @@ -19,7 +31,7 @@ class Migration(migrations.Migration): ), migrations.AddField( model_name='script', - name='script_module', + name='module', field=models.ForeignKey(default=None, on_delete=django.db.models.deletion.PROTECT, related_name='scripts', to='extras.scriptmodule'), preserve_default=False, ), @@ -32,4 +44,8 @@ class Migration(migrations.Migration): name='script', options={'ordering': ('name', 'pk')}, ), + migrations.RunPython( + code=update_scripts, + reverse_code=migrations.RunPython.noop + ), ] diff --git a/netbox/extras/models/scripts.py b/netbox/extras/models/scripts.py index 2f1454e2a..baaa9b973 100644 --- a/netbox/extras/models/scripts.py +++ b/netbox/extras/models/scripts.py @@ -26,7 +26,7 @@ class Script(EventRulesMixin, models.Model): verbose_name=_('name'), max_length=79, ) - script_module = models.ForeignKey( + module = models.ForeignKey( to='extras.ScriptModule', on_delete=models.PROTECT, related_name='scripts' @@ -59,9 +59,8 @@ class ScriptModule(PythonModuleMixin, JobsMixin, ManagedFile): def __str__(self): return self.python_name - ''' @cached_property - def scripts(self): + def get_module_scripts(self): def _get_name(cls): # For child objects in submodules use the full import path w/o the root module as the name @@ -83,7 +82,6 @@ class ScriptModule(PythonModuleMixin, JobsMixin, ManagedFile): scripts[_get_name(cls)] = cls return scripts - ''' def save(self, *args, **kwargs): self.file_root = ManagedFileRootPathChoices.SCRIPTS