mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-18 05:28:16 -06:00
14438 update migration
This commit is contained in:
parent
ecd2712253
commit
e9f28dcd4d
@ -1,10 +1,19 @@
|
|||||||
# Generated by Django 4.2.9 on 2024-02-05 21:37
|
# Generated by Django 4.2.9 on 2024-02-05 21:37
|
||||||
import inspect
|
import inspect
|
||||||
|
import os
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
from django.db import migrations, models
|
from django.db import migrations, models
|
||||||
import django.db.models.deletion
|
import django.db.models.deletion
|
||||||
|
from importlib.machinery import SourceFileLoader
|
||||||
|
|
||||||
|
|
||||||
|
#
|
||||||
|
# Note: This has a couple dependencies on the codebase if doing future modifications:
|
||||||
|
# There are imports from extras.scripts and extras.reports as well as expecting
|
||||||
|
# settings.SCRIPTS_ROOT and settings.REPORTS_ROOT to be in settings
|
||||||
|
#
|
||||||
|
|
||||||
def update_event_rules(apps, schema_editor):
|
def update_event_rules(apps, schema_editor):
|
||||||
Script = apps.get_model('extras', 'Script')
|
Script = apps.get_model('extras', 'Script')
|
||||||
ContentType = apps.get_model('contenttypes', 'ContentType')
|
ContentType = apps.get_model('contenttypes', 'ContentType')
|
||||||
@ -25,10 +34,25 @@ def update_event_rules(apps, schema_editor):
|
|||||||
rule.save()
|
rule.save()
|
||||||
|
|
||||||
|
|
||||||
|
def full_path(instance):
|
||||||
|
root_path = {
|
||||||
|
'scripts': settings.SCRIPTS_ROOT,
|
||||||
|
'reports': settings.REPORTS_ROOT,
|
||||||
|
}[instance.file_root]
|
||||||
|
return os.path.join(root_path, instance.file_path)
|
||||||
|
|
||||||
|
|
||||||
|
def python_name(instance):
|
||||||
|
path, filename = os.path.split(full_path(instance))
|
||||||
|
name = os.path.splitext(filename)[0]
|
||||||
|
if name == '__init__':
|
||||||
|
# File is a package
|
||||||
|
return os.path.basename(path)
|
||||||
|
else:
|
||||||
|
return name
|
||||||
|
|
||||||
|
|
||||||
def is_script(obj):
|
def is_script(obj):
|
||||||
"""
|
|
||||||
Returns True if the object is a Script or Report.
|
|
||||||
"""
|
|
||||||
from extras.scripts import Script
|
from extras.scripts import Script
|
||||||
from extras.reports import Report
|
from extras.reports import Report
|
||||||
|
|
||||||
@ -38,6 +62,12 @@ def is_script(obj):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def get_python_module(instance):
|
||||||
|
loader = SourceFileLoader(python_name(instance), full_path(instance))
|
||||||
|
module = loader.load_module()
|
||||||
|
return module
|
||||||
|
|
||||||
|
|
||||||
def get_module_scripts(apps, instance):
|
def get_module_scripts(apps, instance):
|
||||||
|
|
||||||
def _get_name(cls):
|
def _get_name(cls):
|
||||||
@ -45,9 +75,10 @@ def get_module_scripts(apps, instance):
|
|||||||
return cls.full_name.split(".", maxsplit=1)[1]
|
return cls.full_name.split(".", maxsplit=1)[1]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
module = instance.get_module()
|
module = get_python_module(instance)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
module = None
|
module = None
|
||||||
|
return
|
||||||
|
|
||||||
scripts = {}
|
scripts = {}
|
||||||
ordered = getattr(module, 'script_order', [])
|
ordered = getattr(module, 'script_order', [])
|
||||||
@ -55,7 +86,6 @@ def get_module_scripts(apps, instance):
|
|||||||
for cls in ordered:
|
for cls in ordered:
|
||||||
scripts[_get_name(cls)] = cls
|
scripts[_get_name(cls)] = cls
|
||||||
for name, cls in inspect.getmembers(module, is_script):
|
for name, cls in inspect.getmembers(module, is_script):
|
||||||
breakpoint()
|
|
||||||
if cls not in ordered:
|
if cls not in ordered:
|
||||||
scripts[_get_name(cls)] = cls
|
scripts[_get_name(cls)] = cls
|
||||||
|
|
||||||
@ -66,18 +96,22 @@ def update_scripts(apps, schema_editor):
|
|||||||
ScriptModule = apps.get_model('extras', 'ScriptModule')
|
ScriptModule = apps.get_model('extras', 'ScriptModule')
|
||||||
Script = apps.get_model('extras', 'Script')
|
Script = apps.get_model('extras', 'Script')
|
||||||
ContentType = apps.get_model('contenttypes', 'ContentType')
|
ContentType = apps.get_model('contenttypes', 'ContentType')
|
||||||
|
Job = apps.get_model('core', 'Job')
|
||||||
ct = ContentType.objects.filter(app_label='extras', model='script').first()
|
ct = ContentType.objects.filter(app_label='extras', model='script').first()
|
||||||
|
ct_module = ContentType.objects.filter(app_label='extras', model='scriptmodule').first()
|
||||||
|
|
||||||
for module in ScriptModule.objects.all():
|
for module in ScriptModule.objects.all():
|
||||||
for script in get_module_scripts(apps, module).keys():
|
module_scripts = get_module_scripts(apps, module)
|
||||||
obj = Script.objects.create(
|
if module_scripts:
|
||||||
name=script,
|
for script in module_scripts.keys():
|
||||||
module=ScriptModule.objects.get(file_root=module.file_root, file_path=module.file_path),
|
obj = Script.objects.create(
|
||||||
)
|
name=script,
|
||||||
|
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
|
# update all jobs associated with this module/name to point to the new script obj
|
||||||
if ct:
|
if ct:
|
||||||
module.jobs.filter(name=script).update(object_type=ct, object_id=obj.id)
|
Job.objects.filter(object_type=ct_module, object_id=module.id, name=script).update(object_type=ct, object_id=obj.id)
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
|
Loading…
Reference in New Issue
Block a user