14438 review changes

This commit is contained in:
Arthur 2024-02-13 11:16:10 -08:00
parent 38d2cbbc65
commit c4ffafaca2
2 changed files with 31 additions and 5 deletions

View File

@ -25,18 +25,41 @@ def update_event_rules(apps, schema_editor):
rule.save()
def get_module_scripts(instance):
def _get_name(cls):
# For child objects in submodules use the full import path w/o the root module as the name
return cls.full_name.split(".", maxsplit=1)[1]
try:
module = instance.get_module()
except Exception as e:
logger.debug(f"Failed to load script: {instance.python_name} error: {e}")
module = None
scripts = {}
ordered = getattr(module, 'script_order', [])
for cls in ordered:
scripts[_get_name(cls)] = cls
for name, cls in inspect.getmembers(module, is_script):
if cls not in ordered:
scripts[_get_name(cls)] = cls
return scripts
def update_scripts(apps, schema_editor):
from extras.models import ScriptModule
ScriptModuleNew = apps.get_model('extras', 'ScriptModule')
ScriptModule = apps.get_model('extras', 'ScriptModule')
Script = apps.get_model('extras', 'Script')
ContentType = apps.get_model('contenttypes', 'ContentType')
ct = ContentType.objects.filter(app_label='extras', model='script').first()
for module in ScriptModule.objects.all():
for script in module.get_module_scripts.keys():
for script in get_module_scripts(module).keys():
obj = Script.objects.create(
name=script,
module=ScriptModuleNew.objects.get(file_root=module.file_root, file_path=module.file_path),
module=ScriptModule.objects.get(file_root=module.file_root, file_path=module.file_path),
)
# update all jobs associated with this module/name to point to the new script obj

View File

@ -26,7 +26,7 @@ logger = logging.getLogger('netbox.data_backends')
class Script(EventRulesMixin, JobsMixin, models.Model):
name = models.CharField(
verbose_name=_('name'),
max_length=79,
max_length=79, # Maximum length for a Python class name
)
module = models.ForeignKey(
to='extras.ScriptModule',
@ -57,6 +57,9 @@ class Script(EventRulesMixin, JobsMixin, models.Model):
verbose_name = _('script')
verbose_name_plural = _('scripts')
def get_absolute_url(self):
return reverse('extras:script', args=[self.pk])
@cached_property
def python_class(self):
return self.module.get_module_scripts.get(self.name)