14438 script model

This commit is contained in:
Arthur 2024-02-05 14:16:24 -08:00
parent 1034ee7380
commit 4cb3b63f79
2 changed files with 19 additions and 5 deletions

View File

@ -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
),
]

View File

@ -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